从iOS8.0开始推送功能的实现在不断改变,功能也在不断增加,iOS10又出来了一个推送插件的开发(见最后图),废话不多说直接上代码:

在开始之前需要打开一个推送开关,不然无法获取deviceToken,老项目或者出现deviceToken无效的情况:如图:

打开后会生成entitlements文件,需要有APS Environment

或许还应该打开这个

#import <UserNotifications/UserNotifications.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {// Override point for customization after application launch./* APP未启动,点击推送消息的情况下 iOS10遗弃UIApplicationLaunchOptionsLocalNotificationKey,使用代理UNUserNotificationCenterDelegate方法didReceiveNotificationResponse:withCompletionHandler:获取本地推送*/
//    NSDictionary *localUserInfo = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
//    if (localUserInfo) {
//        NSLog(@"localUserInfo:%@",localUserInfo);
//        //APP未启动,点击推送消息
//    }NSDictionary *remoteUserInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];if (remoteUserInfo) {NSLog(@"remoteUserInfo:%@",remoteUserInfo);//APP未启动,点击推送消息,iOS10下还是跟以前一样在此获取}[self registerNotification];return YES;
}

注册推送方法的改变:

新增库 #import <UserNotifications/UserNotifications.h>  推送单列UNUserNotificationCenter 等API

- (void)registerNotification{/*identifier:行为标识符,用于调用代理方法时识别是哪种行为。title:行为名称。UIUserNotificationActivationMode:即行为是否打开APP。authenticationRequired:是否需要解锁。destructive:这个决定按钮显示颜色,YES的话按钮会是红色。behavior:点击按钮文字输入,是否弹出键盘*/UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"action1" title:@"策略1行为1" options:UNNotificationActionOptionForeground];/*iOS9实现方法UIMutableUserNotificationAction * action1 = [[UIMutableUserNotificationAction alloc] init];action1.identifier = @"action1";action1.title=@"策略1行为1";action1.activationMode = UIUserNotificationActivationModeForeground;action1.destructive = YES;*/UNTextInputNotificationAction *action2 = [UNTextInputNotificationAction actionWithIdentifier:@"action2" title:@"策略1行为2" options:UNNotificationActionOptionDestructive textInputButtonTitle:@"textInputButtonTitle" textInputPlaceholder:@"textInputPlaceholder"];/*iOS9实现方法UIMutableUserNotificationAction * action2 = [[UIMutableUserNotificationAction alloc] init];action2.identifier = @"action2";action2.title=@"策略1行为2";action2.activationMode = UIUserNotificationActivationModeBackground;action2.authenticationRequired = NO;action2.destructive = NO;action2.behavior = UIUserNotificationActionBehaviorTextInput;//点击按钮文字输入,是否弹出键盘*/UNNotificationCategory *category1 = [UNNotificationCategory categoryWithIdentifier:@"Category1" actions:@[action2,action1]  intentIdentifiers:@[@"action1",@"action2"] options:UNNotificationCategoryOptionCustomDismissAction];//        UIMutableUserNotificationCategory * category1 = [[UIMutableUserNotificationCategory alloc] init];//        category1.identifier = @"Category1";//        [category1 setActions:@[action2,action1] forContext:(UIUserNotificationActionContextDefault)];UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"action3" title:@"策略2行为1" options:UNNotificationActionOptionForeground];//        UIMutableUserNotificationAction * action3 = [[UIMutableUserNotificationAction alloc] init];//        action3.identifier = @"action3";//        action3.title=@"策略2行为1";//        action3.activationMode = UIUserNotificationActivationModeForeground;//        action3.destructive = YES;UNNotificationAction *action4 = [UNNotificationAction actionWithIdentifier:@"action4" title:@"策略2行为2" options:UNNotificationActionOptionForeground];//        UIMutableUserNotificationAction * action4 = [[UIMutableUserNotificationAction alloc] init];//        action4.identifier = @"action4";//        action4.title=@"策略2行为2";//        action4.activationMode = UIUserNotificationActivationModeBackground;//        action4.authenticationRequired = NO;//        action4.destructive = NO;UNNotificationCategory *category2 = [UNNotificationCategory categoryWithIdentifier:@"Category2" actions:@[action3,action4]  intentIdentifiers:@[@"action3",@"action4"] options:UNNotificationCategoryOptionCustomDismissAction];//        UIMutableUserNotificationCategory * category2 = [[UIMutableUserNotificationCategory alloc] init];//        category2.identifier = @"Category2";//        [category2 setActions:@[action4,action3] forContext:(UIUserNotificationActionContextDefault)];[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObjects:category1,category2, nil]];[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) {NSLog(@"completionHandler");}];/*iOS9实现方法UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObjects: category1,category2, nil]];[[UIApplication sharedApplication] registerUserNotificationSettings:settings];*/[[UIApplication sharedApplication] registerForRemoteNotifications];[UNUserNotificationCenter currentNotificationCenter].delegate = self;
}

代理方法的改变:

一些本地和远程推送的回调放在了同一个代理方法

#pragma mark -- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED{NSLog(@"didRegisterUserNotificationSettings");
}- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken NS_AVAILABLE_IOS(3_0){NSLog(@"deviceToken:%@",deviceToken);NSString *deviceTokenSt = [[[[deviceToken description]stringByReplacingOccurrencesOfString:@"<" withString:@""]stringByReplacingOccurrencesOfString:@">" withString:@""]stringByReplacingOccurrencesOfString:@" " withString:@""];NSLog(@"deviceTokenSt:%@",deviceTokenSt);
}- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error NS_AVAILABLE_IOS(3_0){NSLog(@"didFailToRegisterForRemoteNotificationsWithError:%@",error);
}/*iOS9使用方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo NS_DEPRECATED_IOS(3_0, 10_0, "Use UserNotifications Framework's -[UNUserNotificationCenterDelegate willPresentNotification:withCompletionHandler:] or -[UNUserNotificationCenterDelegate didReceiveNotificationResponse:withCompletionHandler:] for user visible notifications and -[UIApplicationDelegate application:didReceiveRemoteNotification:fetchCompletionHandler:] for silent remote notifications"){}
*/- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{NSLog(@"willPresentNotification:%@",notification.request.content.title);// 这里真实需要处理交互的地方// 获取通知所带的数据NSString *notMess = [notification.request.content.userInfo objectForKey:@"aps"];}- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{//在没有启动本App时,收到服务器推送消息,下拉消息会有快捷回复的按钮,点击按钮后调用的方法,根据identifier来判断点击的哪个按钮NSString *notMess = [response.notification.request.content.userInfo objectForKey:@"aps"];NSLog(@"didReceiveNotificationResponse:%@",response.notification.request.content.title);
//    response.notification.request.identifier
}//远程推送APP在前台
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{NSLog(@"didReceiveRemoteNotification:%@",userInfo);
}/*
- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler NS_DEPRECATED_IOS(8_0, 10_0, "Use UserNotifications Framework's -[UNUserNotificationCenterDelegate didReceiveNotificationResponse:withCompletionHandler:]") __TVOS_PROHIBITED
{}
*/
/*
// 本地通知回调函数,当应用程序在前台时调用
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification NS_DEPRECATED_IOS(4_0, 10_0, "Use UserNotifications Framework's -[UNUserNotificationCenterDelegate willPresentNotification:withCompletionHandler:] or -[UNUserNotificationCenterDelegate didReceiveNotificationResponse:withCompletionHandler:]") __TVOS_PROHIBITED{NSLog(@"didReceiveLocalNotification:%@",notification.userInfo);// 这里真实需要处理交互的地方// 获取通知所带的数据NSString *notMess = [notification.userInfo objectForKey:@"aps"];UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"本地通知(前台)"message:notMessdelegate:nilcancelButtonTitle:@"OK"otherButtonTitles:nil];[alert show];// 更新显示的徽章个数NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;badge--;badge = badge >= 0 ? badge : 0;[UIApplication sharedApplication].applicationIconBadgeNumber = badge;// 在不需要再推送时,可以取消推送[FirstViewController cancelLocalNotificationWithKey:@"key"];}- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler NS_DEPRECATED_IOS(8_0, 10_0, "Use UserNotifications Framework's -[UNUserNotificationCenterDelegate didReceiveNotificationResponse:withCompletionHandler:]") __TVOS_PROHIBITED
{//在非本App界面时收到本地消息,下拉消息会有快捷回复的按钮,点击按钮后调用的方法,根据identifier来判断点击的哪个按钮,notification为消息内容NSLog(@"%@----%@",identifier,notification);completionHandler();//处理完消息,最后一定要调用这个代码块
}
*/

还有推送插件开发: 类似iOS tody widget插件开发

这里我们要注意一定要有"mutable-content": "1",以及一定要有Alert的字段,否则可能会拦截通知失败。(苹果文档说的)。除此之外,我们还可以添加自定义字段,比如,图片地址,图片类型
{"aps": {"alert": "message.","badge": 1,"sound": "default","mutable-content": "1","imageAbsoluteString": "http://xxxx"}
}

iOS10全新推送功能的实现相关推荐

  1. 推荐一款 Flutter Push 推送功能插件

    又到了推荐好插件的时候了.开发 APP 避免不了使用「推送」功能.比如,新上架一个商品,或者最新的一条体育新闻,实时推送给用户. 比较了几家推送平台,貌似「极光」出了 Flutter 插件,所以就拿它 ...

  2. 在 Asp.NET MVC 中使用 SignalR 实现推送功能

    一,简介 Signal 是微软支持的一个运行在 Dot NET 平台上的 html websocket 框架.它出现的主要目的是实现服务器主动推送(Push)消息到客户端页面,这样客户端就不必重新发送 ...

  3. php 企业号文本消息推送,Python如何实现微信企业号文本消息推送功能的示例

    这篇文章主要介绍了Python编程实现微信企业号文本消息推送功能,结合实例形式分析了Python微信企业号文本消息推送接口的调用相关操作技巧,需要的朋友可以参考下 本文实例讲述了Python微信企业号 ...

  4. tornado服务器动态文件,tornado 实现服务器消息推送功能

    这篇文章介绍tornado 服务器消息推送功能服务器端与客户端实现的的方法. 消息推送的过程: 客户端1 连接请求,服务器先hold 住,别返回: 客户端2 发送消息,服务器把信息返回给 客户端1. ...

  5. Android之集成友盟推送功能

    友盟是中国最大的移动开发者服务平台,为移动开发者提供免费的应用统计分析.社交分享.消息推送.自动更新.在线参数.移动推广效果分析.微社区等app开发和运营解决方案. 如何快速集成友盟推送功能: 1. ...

  6. nodejs android 推送,利用Nodejs怎么实现一个微信小程序消息推送功能

    利用Nodejs怎么实现一个微信小程序消息推送功能 发布时间:2021-01-20 13:55:29 来源:亿速云 阅读:92 作者:Leah 今天就跟大家聊聊有关利用Nodejs怎么实现一个微信小程 ...

  7. openfire消息通知推送_APP消息推送功能之前端后台设计

    APP消息推送功能之前端后台设计 最近有不少小伙伴问APP消息推送功能,前端.后台如何设计的?消息系统的架构是什么样的?最近刚好做到后台消息推送这块,简单谈谈个人心得,欢迎拍砖. 消息推送是让自己的用 ...

  8. 百度自动推送html5,百度暂停 JS 代码自动推送功能,代码是否需要删除?

    本月上旬末的时候百度站长平台就推送了一则关于:搜索资源平台自动推送暂停使用通知.说到由于升级维护,搜索资源平台链接提交-自动提交-自动推送(JS 代码推送)功能暂时停止使用.并且会在恢复使用时再次通知 ...

  9. java消息推送怎么实现_PHP实现的消息实时推送功能

    本文实例讲述了PHP实现的消息实时推送功能.分享给大家供大家参考,具体如下: 入口文件index.html <!DOCTYPE HTML> <html> <head> ...

最新文章

  1. c++和python的区别、javascript_python和c++的区别
  2. shell脚本字符串截取的8种方法
  3. Win32ASM学习[11]:逻辑运算
  4. linux的8小时差问题解决
  5. 写了 300000 行基础设施代码,我学到了这五条经验
  6. 曾辉机器人_工业机器人市占率5年来首现下滑 “春寒”中国产机器人如何突围?...
  7. SSH/SSH客户端介绍、利用SSH访问linux、SSH跟telnet区别
  8. html开源flash视频播放器代码下载
  9. 衡量测试的充分性和完整性-测试覆盖率
  10. 使用nginx配置二级域名
  11. 基于支持向量机的新闻分类
  12. 2010年中国十大最赚钱职业
  13. python读取.nii.gz文件并使用nibabel展示医学图片
  14. 聚名:怎样批量查询50个域名的注册信息?
  15. 有故事,带项目的flask教程(2)---flask的路由管理
  16. Realview MDK 链接脚本文件详细解析(一)–链接符号
  17. Docker_03_彻底搞懂Dockerfile文件
  18. There is no getter for property named ‘xxx‘ in ‘class com.test.project.entity.
  19. android获取机主手机号,Android中 获取手机通讯录和手机机主手机号码——诺诺涂鸦记忆...
  20. 试题 历届真题 带分数【第四届】【省赛】【A组】

热门文章

  1. Android内存优化 1
  2. 【C/C++main函数返回值为空、return 0、return a的意义是什么】
  3. 单引号、双引号 Html转义符
  4. 基于jsp+mysql+Spring+mybatis+Springboot的SpringBoot婚纱影楼摄影预约网站
  5. python adb模块,python + adb 实现控制手机,,主要步骤:1、USB
  6. dsploit超强黑客软件
  7. linux通过什么命令查看中断,查看linux 中断
  8. Photoshop插件-色调-颜色/明度-脚本开发-PS插件
  9. BSSID,SSID,ESSID区别
  10. Matlab/Simulink自动生成C代码实验