欢迎来到代码驿站!

iOS代码

当前位置:首页 > 移动开发 > iOS代码

IOS10 远程推送适配详细介绍

时间:2021-06-19 08:18:14|栏目:iOS代码|点击:

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

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

上一篇:IOS中UIWebView的使用详解

栏    目:iOS代码

下一篇:iOS的CoreAnimation开发框架中的Layer层动画制作解析

本文标题:IOS10 远程推送适配详细介绍

本文地址:http://www.codeinn.net/misctech/144608.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有