当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS10 适配远程推送功能实现代码

iOS10 适配远程推送功能实现代码

2019年07月24日  | 移动技术网移动技术  | 我要评论
ios10正式版发布之后,网上各种适配xcode8以及ios10的文章满天飞。但对于ios10适配远程推送的文章却不多。ios10对于推送的修改还是非常大的,新增了user

ios10正式版发布之后,网上各种适配xcode8以及ios10的文章满天飞。但对于ios10适配远程推送的文章却不多。ios10对于推送的修改还是非常大的,新增了usernotifications framework,今天就结合自己的项目,说一说实际适配的情况。 

一、capabilities中打开push notifications 开关

在xcode7中这里的开关不打卡,推送也是可以正常使用的,但是在xcode8中,这里的开关必须要打开,不然会报错:

error domain=nscocoaerrordomain code=3000 "未找到应用程序的“aps-environment”的授权字符串" userinfo={nslocalizeddescription=未找到应用程序的“aps-environment”的授权字符串}

打开后会生成entitlements文件,在这里可以设置aps environment 

二、推送的注册

首先引入usernotifications framework,

import <usernotifications/usernotifications.h>
ios10修改了注册推送的方法,这里我们又要对不同版本分别进行设置了。在application didfinishlaunchingwithoptions方法中修改以前的推送设置(我只实现了ios8以上的设置) 

if (ios_version >= 10.0) {
  unusernotificationcenter *center = [unusernotificationcenter currentnotificationcenter];
  center.delegate = self;
  [center requestauthorizationwithoptions:(unauthorizationoptionbadge | unauthorizationoptionsound | unauthorizationoptionalert) completionhandler:^(bool granted, nserror * _nullable error) {
   if (!error) {
    dlog(@"request authorization succeeded!");
   }
  }];
 } else {
  if ([application respondstoselector:@selector(registerusernotificationsettings:)]) {
   //ios8,创建uiusernotificationsettings,并设置消息的显示类类型
   uiusernotificationsettings *notisettings = [uiusernotificationsettings settingsfortypes:(uiusernotificationtypebadge | uiusernotificationtypealert | uiusernotificationtypesound) categories:nil];
 
   [application registerusernotificationsettings:notisettings];
  }
 }

三、unusernotificationcenterdelegate代理实现

在ios10中处理推送消息需要实现unusernotificationcenterdelegate的两个方法: 

复制代码 代码如下:
- (void)usernotificationcenter:(unusernotificationcenter *)center willpresentnotification:(unnotification *)notification withcompletionhandler:(void (^)(unnotificationpresentationoptions))completionhandler  
- (void)usernotificationcenter:(unusernotificationcenter *)center didreceivenotificationresponse:(unnotificationresponse *)response withcompletionhandler:(void (^)())completionhandler

其中第一个方法为app在前台的时候收到推送执行的回调方法,第二个为app在后台的时候,点击推送信息,进入app后执行的 回调方法。

以前处理推送,信息是在userinfo参数中,而新方法中表明上看并没有这个参数,其实我们一样可以获取到userinfo,如下: 

/// app在前台时候回调
- (void)usernotificationcenter:(unusernotificationcenter *)center willpresentnotification:(unnotification *)notification withcompletionhandler:(void (^)(unnotificationpresentationoptions))completionhandler {
 nsdictionary *userinfo = notification.request.content.userinfo;
 [self handleremotenotificationforcegroundwithuserinfo:userinfo];
}
 
/// app在后台时候点击推送调用
- (void)usernotificationcenter:(unusernotificationcenter *)center didreceivenotificationresponse:(unnotificationresponse *)response withcompletionhandler:(void (^)())completionhandler {
 nsdictionary *userinfo = response.notification.request.content.userinfo;
 [self handleremotenotificationbackgroundwithuserinfo:userinfo];
}

完成上面三个步骤的设置,对于ios10的推送设置基本就适配了。要想自定义notification content或者实现其他notificationaction请参考其他文章。这里只是做了对ios10的适配。

本文已被整理到了《ios推送教程》,欢迎大家学习阅读。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网