欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

IOS10 远程推送适配详细介绍

程序员文章站 2024-02-18 20:39:04
ios10 远程推送适配 ios10推送新增了usernotifications framework,使用起来其实很简单。 建议看看极光推送的demo,里面写的更详细。...

ios10 远程推送适配

ios10推送新增了usernotifications framework,使用起来其实很简单。

建议看看极光推送的demo,里面写的更详细。

只是在ios10以上系统上点击通知栏,回调方法不再走原来的这两个方法

- (void)application:(uiapplication *)application 
didreceiveremotenotification:(nsdictionary *)userinfo {}
- (void)application:(uiapplication *)application
 didreceiveremotenotification:(nsdictionary *)userinfo 
fetchcompletionhandler:(void (^)(uibackgroundfetchresult))completionhandler {}

而是在前台的时候回调

- (void)usernotificationcenter:(unusernotificationcenter *)center 
willpresentnotification:(unnotification *)notification 
withcompletionhandler:(void (^)
(unnotificationpresentationoptions))completionhandler

从后台进入的时候回调

- (void)usernotificationcenter:(unusernotificationcenter *)center
 didreceivenotificationresponse:(unnotificationresponse *)response 
withcompletionhandler:(void (^)())completionhandler

直接说怎么用吧:

1,导入头文件

#ifdef nsfoundationversionnumber_ios_9_x_max
#import <usernotifications/usernotifications.h>
#endif

2,注册通知

- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions 方法中

if (ios10) {
  unusernotificationcenter *center = [unusernotificationcenter currentnotificationcenter];
  center.delegate = self;
  [center requestauthorizationwithoptions:(unauthorizationoptionbadge 
| unauthorizationoptionsound | unauthorizationoptionalert) 
completionhandler:^(bool granted, nserror * _nullable error) {
   if (!error) {
    nslog(@"succeeded!");
   }
  }];
 } else if (ios8_9){//ios8-ios9
  uiusernotificationsettings *settings = [uiusernotificationsettings 
settingsfortypes:(uiusernotificationtypebadge | uiusernotificationtypealert 
| uiusernotificationtypesound) categories:nil];
  [application registerusernotificationsettings:settings];
  [application registerforremotenotifications];
 } else {//ios8以下
  [application registerforremotenotificationtypes: uiremotenotificationtypebadge
 | uiremotenotificationtypealert | uiremotenotificationtypesound];
 }

3,回调方法中,获取通知数据

- (void)usernotificationcenter:(unusernotificationcenter *)center 
didreceivenotificationresponse:(unnotificationresponse *)response 
withcompletionhandler:(void (^)())completionhandler {
 nsdictionary *userinfo = response.notification.request.content.userinfo;
   //消息处理
 if([request.trigger iskindofclass:[unpushnotificationtrigger class]]) {
  //判断为远程推送
 }else {
  // 判断为本地通知 
 }
}

4,对于本地通知没有什么变化依然会回调

-(void)application:(uiapplication *)application 
didreceivelocalnotification:(uilocalnotification *)notification

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!