当前位置 主页 > 服务器问题 > Linux/apache问题 >
4.在接收远程推送的DeviceToken
方法中, 获取Token
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { //将来需要将此Token上传给后台服务器 NSLog(@"token:%@", deviceToken); }
二、 iOS10远程推送通知的处理方法
当点击了推送后, 如果你希望进行处理. 那么在iOS10中, 还需要设置UNUserNotificationCenter
的delegate
, 并遵守UNUserNotificationCenterDelegate
协议.
以及实现下面实现3个方法, 用于处理点击通知时的不同情况的处理
willPresentNotification:withCompletionHandler
用于前台运行
didReceiveNotificationResponse:withCompletionHandler
用于后台及程序退出
didReceiveRemoteNotification:fetchCompletionHandler
用于静默推送
//设置通知的代理 center.delegate = self;
1.前台运行 会调用的方法
前台运行: 指的是程序正在运行中, 用户能看见程序的界面.
iOS10会出现通知横幅, 而在以前的框架中, 前台运行时, 不会出现通知的横幅.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { NSDictionary *userInfo = notification.request.content.userInfo; //前台运行推送 显示红色Label [self showLabelWithUserInfo:userInfo color:[UIColor redColor]]; //可以设置当收到通知后, 有哪些效果呈现(声音/提醒/数字角标) completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert); }
2.后台运行及程序退出 会调用的方法
后台运行: 指的是程序已经打开, 用户看不见程序的界面, 如锁屏和按Home键.
程序退出: 指的是程序没有运行, 或者通过双击Home键,关闭了程序.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler { NSDictionary *userInfo = response.notification.request.content.userInfo; //后台及退出推送 显示绿色Label [self showLabelWithUserInfo:userInfo color:[UIColor greenColor]]; completionHandler(); }
3.静默推送通知 会调用的方法
静默推送: iOS7以后出现, 不会出现提醒及声音.
要求:
推送的payload
中不能包含alert
及sound
字段
需要添加content-available
字段, 并设置值为1
例如: {"aps":{"content-available":"1"},"PageKey”":"2"}
//如果是以前的旧框架, 此方法 前台/后台/退出/静默推送都可以处理 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { //静默推送 显示蓝色Label [self showLabelWithUserInfo:userInfo color:[UIColor blueColor]]; completionHandler(UIBackgroundFetchResultNewData); }