源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2020-03-17 18:18 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:iOS10 适配远程推送功能实现代码
iOS10正式版发布之后,网上各种适配XCode8以及iOS10的文章满天飞。但对于iOS10适配远程推送的文章却不多。iOS10对于推送的修改还是非常大的,新增了UserNotifications Framework,今天就结合自己的项目,说一说实际适配的情况。  [b]一、Capabilities中打开Push Notifications 开关[/b] 在XCode7中这里的开关不打卡,推送也是可以正常使用的,但是在XCode8中,这里的开关必须要打开,不然会报错: Error Domain=NSCocoaErrorDomain Code=3000 "未找到应用程序的“aps-environment”的授权字符串" UserInfo={NSLocalizedDescription=未找到应用程序的“aps-environment”的授权字符串} 打开后会生成entitlements文件,在这里可以设置APS Environment  [b]二、推送的注册[/b] 首先引入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];
  }
 }
[b]三、UNUserNotificationCenterDelegate代理实现[/b] 在iOS10中处理推送消息需要实现UNUserNotificationCenterDelegate的两个方法: 
[url=http://www.1sucai.cn/Special/888.htm]iOS推送教程[/url]》,欢迎大家学习阅读。 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部