iOS SDK 常见问题解答

1. 如何实现 iOS 应用退出或者处于后台时可以收到推送消息?

答:

  • 需要生成 APNs 证书;
  • 在 App 注册 remoteNotification 通知,获取 Device Token,并通过storeDeviceToken()函数保存 Device Token 到云巴服务端;
  • 通过带有 ApnOption 的publish2()publish2ToAlias()或者默认的publish()publishToAlias()发送 APNs 消息,该参数设置详见云巴知识库的 Payload 一文,以及 iOS 官方文档

publish2()需要带有 ApnOption 参数才能成功发送 APNs 消息;而publish()会发送默认的 APNs 消息。


2. 接收的消息,除了基本的内容(Topic 和 Message)还可以传递一些参数信息吗?

答: 开发者可以封装多个数据到 data.msg。


3. ApnOption 的 sound 和 badge 有什么作用?

答: 可在publish2ToAlias()publish2()的 ApnOption 参数设置消息通知的方式。 alert 设置消息通知栏的内容;badge 设置角标;sound 设置通知的铃声。

具体参考云巴知识库的 Payload 一文,以及 iOS 官方文档,或参考 iOS demo 中有关 ApnOption 的设置方法。


4. 如何自定义 iOS 推送的铃声?

答: 先将自定义的铃声文件加入你的 Xcode 工程里,然后,在推送的时候指定音频的名称即可。注意使用系统支持的音频格式。

...
{
    "aps": {
        "alert":"yunba",
        "badge":3,
        "sound":"bingbong.aiff"
    }
}

如使用默认铃声,则为:

...
 "sound":"default"

5. 如何处理 badge?

答: 在推送的时候可通过指定 badge 的值来改变当前的 badge(如上例);通过[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];可清除 badge 的值。


6. iOS SDKsubscribe()的 qosLevel 参数,和 YBPublish2Option 的 qos 这两个参数有什么区别?

答:subscribe()的 qos Level 限制该话题下接收到 message 的最大 qos 等级。 例如:当设置subscribe()的 qosLevel 为 0,则 qos 为 1 的接收消息会降级到 qos 为 0。详见 MQTT V3.1 Protocol Specification QoS 的说明。


7. iOS 端怎样设置不接收任何消息?

答: 设置别名为空,并unsubscribe()所有 Topic。


8. 当同时定义了publish()的 data 和 ApnOption 参数中 alert 的 message,消息内容将以哪个为准?

答: 以 alert 的 message 为准。未定义 alert 时,则默认显示publish()的 data。


9. iOS 端如何设置通知方式?

答: 上传 APNs 证书;通过 YBPublish2Option 参数的 alert 设置通知栏内容、角标和声音等,具体参考 sdk 中关于pushlish2()的介绍,也可以下载并参考 iOS demo 中 YBPublish2Option 的设置。

完整的设置方法请参考 iOS 官方文档


10. 使用苹果电脑来生成 APNs 证书,在双击打开证书文件(*.cer)时,遇到“不能修改“System Roots”钥匙串”的提示

答:在钥匙串访问的界面左侧,选择“登录”(login),将证书文件(*.cer)拖进钥匙列表中即可。


11. 使用 iOS SDK,上传了 APNs 证书,但应用在后台时却收不到 APNs 通知?

答:

  • 请确认一下是否在云巴 Portal 的该应用页面下正确地上传了 APNs 证书;
  • 确保上传到云巴的证书是和你的 App 关联的有效证书,而且需要带上证书的 RSA 密钥;
  • 确保 App 的 provisioning profile 包含这个证书和这个设备;
  • Xcode 8 及以上的版本需要在项目的 Capabilities 页面中打开 Push Notification 的按钮(如下图所示);
  • 请确认应用是否开启了接收 APNs;
  • 请通过查看日志打印或 store Device Token 回调的返回值的方式来确认 Device Token 是否上传成功了;
  • 集成 SDK 所使用的 AppKey 是否是 Portal 中该应用的 AppKey;
  • 请确认 Xcode 里填入的 bundle id 与在 Apple Portal 上的创建的证书的 bundle id 是否一致;
  • 另外还需注意:publish()publish_to_alias() 会发送默认的 APNs 通知, 而 publish2()publish2_to_alias(),则必须在 opts 里填写 apn_json,才会发送 APNs 通知

iospng_xcode_capabilities_enable_push.png

最后,如果问题依然没有得到解决,请将您的 Appkey 用私聊或邮件(support@yunba.io)的方式发给我们,我们会尽快协助您找出原因。


12. 为什么升级 iOS 后,收不到 APNs 了?

答:可能是升级后 Device Token 改变造成的。建议按照苹果官方文档的建议,每次启动应用时检查 Device Token 并发送到服务器。

苹果官方文档:The device token is your key to sending push notifications to your app on a specific device. Device tokens can change, so your app needs to reregister every time it is launched and pass the received token back to your server. If you fail to update the device token, remote notifications might not make their way to the user’s device. Device tokens always change when the user restores backup data to a new device or computer or reinstalls the operating system. When migrating data to a new device or computer, the user must launch your app once before remote notifications can be delivered to that device.