iOS 推送

Apple Push Notification Service(APNs)

*********************************************

Notification 是一个由 APNs 发来的消息,它包括两个部分:device token 和 payload。device token 是一个识别码,由 APNs 给出。device token 在一台设备上是唯一的。但不建议用于设备识别,因为这个码仍然可能随着 OS 的升级等原因发生变化。最重要的是,如果用户拒绝接受推送就没有这个码。 payload 是一个 JSON 文件,里面描述用户如何接收推送 (an alert message to display to the user,a number to badge the app icon with a sound to play)。

 

*********************************************

AppDelegate 里的回调函数的调用研究 - 1 消息注册:

每次应用启动时注册 notifications (注意:以下代码我只在程序启动时调用)

UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:notificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
[application registerForRemoteNotifications];

1. 首次安装:

  1. didRegisterForRemoteNotificationsWithDeviceToken 被调用

  2. 系统询问用户是否同意接收 Notifications

  3. 不管用户选择同意或拒绝,didRegisterUserNotificationSettings 被调用

2. 应用启动时:

  1. 如果 notifications 处于拒绝状态

    didRegisterUserNotificationSettings 被调用

  2. 如果 notifications 处于允许状态

    1. didRegisterForRemoteNotificationsWithDeviceToken 被调用

    2. didRegisterUserNotificationSettings 被调用

3. 应用运行过程中用户修改 notifications 设置:

  1. 从拒绝变为允许:

    didRegisterForRemoteNotificationsWithDeviceToken 被调用

  2. 从允许变为拒绝:

    什么也不发生

 

*********************************************

AppDelegate 里的回调函数的调用研究 - 2 消息接收:

接收到消息的时机有三种情况:1. 应用没启动 2. 应用在后台 3. 应用在前台

1. 应用没启动

 

2. 应用在后台  

  声音提醒,消息中心收到消息,给应用图标加一个 badge。

  如果 aps 中不包含 "content-available" : 1

    需要运行程序 didReceiveRemoteNotification 才会被调用。

3. 应用在前台

  didReceiveRemoteNotification 被调用。

  信息中心不显示消息。

  已经在前台一般就不用加 badge 了。

 

 

4. 用模拟器测试推送

https://github.com/acoomans/SimulatorRemoteNotifications

 

*********************************************

参考文章:

http://segmentfault.com/a/1190000000520755  

http://samwize.com/2015/08/07/how-to-handle-remote-notification-with-background-mode-enabled/

posted @ 2015-11-11 02:04  davesuen  阅读(320)  评论(0编辑  收藏  举报