推送通知

2016-06-26  本文已影响30人  英雄出少年

本地 推送通知

设置一些必要属性
// 推送通知的触发时间(何时发出推送通知)
@property(nonatomic,copy) NSDate *fireDate;
// 推送通知的具体内容
@property(nonatomic,copy) NSString *alertBody;
// 在锁屏时显示的动作标题(完整标题:“滑动来” + alertAction)
@property(nonatomic,copy) NSString *alertAction;
// 音效文件名
@property(nonatomic,copy) NSString *soundName;
// app图标数字
@property(nonatomic) NSInteger applicationIconBadgeNumber;

一、 开始推送通知

- 立即推送
```objc         
presentLocalNotificationNow:(UILocalNotification *)notification;

二、 监听用户点击通知

application: didReceiveLocalNotification: 
//launchOptions参数通过UIApplicationLaunchOptionsLocalNotificationKey取出本地推送通知对象
application: didFinishLaunchingWithOptions: 

三、额外设置

        // 每隔多久重复发一次推送通知
        @property(nonatomic) NSCalendarUnit repeatInterval;
        // 点击推送通知打开app时显示的启动图片
        @property(nonatomic,copy) NSString *alertLaunchImage;
         // 附加的额外信息
        @property(nonatomic,copy) NSDictionary *userInfo;
        // 时区
        @property(nonatomic,copy) NSTimeZone *timeZone;
        (一般设置为[NSTimeZone defaultTimeZone] ,跟随手机的时区)

四、 其他操作

@property(nonatomic,copy) NSArray *scheduledLocalNotifications;
//已经发出且过期的推送通知就算调度结束,会自动从这个数组中移除

- (void)cancelLocalNotification:(UILocalNotification *)notification;
- (void)cancelAllLocalNotifications;

五、 注意事项

 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
 [application registerUserNotificationSettings:settings];

六、额外补充

注册通知.png 基本操作.png 监听本地通知的状态.png 设置通知的快捷操作组.png 跳转不同界面.png

远程 推送通知
所有苹果设备, 在联网状态下,都会与苹果服务器建立一个长连接

  1. 创建支持远程推送功能的App ID
  2. 申请开发者证书,并选中刚刚创建的App ID
  3. 下载CER文件,并导入钥匙串管理
  4. 申请发布证书,并选中刚刚创建的App ID
  5. 下载CER文件,并导入钥匙串管理
  6. 检查App ID,确认证书已经指定
格式:{"aps":{"alert":"This is some fancy message.","badge":1,"sound":"default"}}

远程推送应用程序开发过程
1.新建应用程序

  1. 指定AppID,在developer.apple.com上设置的AppID
#ifdef __IPHONE_8_0
    // 注册接收通知的类型
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
    [application registerUserNotificationSettings:settings];
    
    // 注册允许接收远程推送通知
    [application registerForRemoteNotifications];
#else
    // 如果是iOS7.0,使用以下方法注册
    [application registerForRemoteNotificationTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound];
#endif


// 当得到苹果的APNs服务器返回的DeviceToken就会被调用
// 7040f7d5 5a974598 c5cf31b5 3e340b39 68affd25 122f0ce1 3f315226 396c2e5b
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"deviceToken是:%@", deviceToken);
}

// 接收到远程通知,触发方法和本地通知一致
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    NSLog(@"%@", userInfo);
}

** 使用后台的远程消息推送**

1、在Capabilities中打开远程推送通知
2、实现代理方法

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

远程消息数据格式:
{"aps" : {"content-available" : 1},"content-id" : 42}
执行completionHandler有两个目的
1、统会估量App消耗的电量,并根据传递的UIBackgroundFetchResult 参数记录新数据是否可用
2、 调用完成的处理代码时,应用的界面缩略图会自动更新

注意:接收到远程通知到执行完网络请求之间的时间不能超过30秒

if (userInfo) {
    int contentId = [userInfo[@"content-id"] intValue];
    
    ViewController *vc = (ViewController *)application.keyWindow.rootViewController;
    [vc loadDataWithContentID:contentId completion:^(NSArray *dataList) {
        vc.dataList = dataList;
        
        NSLog(@"刷新数据结束");
        
        completionHandler(UIBackgroundFetchResultNewData);
    }];
} else {
    completionHandler(UIBackgroundFetchResultNoData);
}

上一篇 下一篇

猜你喜欢

热点阅读