Xcode 8和iOS10正式版出来后,这个方法做了修改,也是之前也没发现这个参数有啥用:
UNNotificationCategory *category1 = [UNNotificationCategory categoryWithIdentifier:@"Category1" actions:@[action2,action1] minimalActions:@[action2,action1] intentIdentifiers:@[@"action1",@"action2"] options:UNNotificationCategoryOptionCustomDismissAction];
改成了:去掉了minimalActions:
UNNotificationCategory *category1 = [UNNotificationCategory categoryWithIdentifier:@"Category1" actions:@[action2,action1] intentIdentifiers:@[@"action1",@"action2"] options:UNNotificationCategoryOptionCustomDismissAction];

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

或许还应该打开这个

新增了UserNotifications Framework

?
1
#import <UserNotifications/UserNotifications.h>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- (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下还是跟以前一样在此获取
    }
  
 return YES;
}

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
- (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] minimalActions:@[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] minimalActions:@[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;
}

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#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);
}

还有推送插件开发

转:http://www.cocoachina.com/bbs/read.php?tid=1686948

iOS10全新方法实现推送+deviceToken无法获取或无效的解决相关推荐

  1. 划重点|iOS15正式发布, 全新的通知推送系统,你必须要知道

    简介: 今年友盟+联合达摩院决策智能实验室讲算法技术,推出国内首个智能推送功能,帮助产品运营人员实现一键式触达的精细化运营.通过精心打磨的在线学习与优化算法,对推送人群与推送文案进行精准匹配,最大化用 ...

  2. 划重点 iOS15正式发布, 全新的通知推送系统,你必须要知道

    作者:友盟+ 技术团队 越来越多的APP频繁推送信息已经导致用户不堪重负,友盟+ 数据显示:45%的iOS用户主动关闭了通知权限. 为了鼓励开发者更合理的使用推送通知.近日,苹果iOS 15发布了全新 ...

  3. 如何向百度站长用post方法主动推送链接

    原文链接:如何向百度站长用post方法主动推送链接 想必用百度站长的都知道,百度连接提交有三种方式: 主动推送(实时):最为快速的提交方式,建议您将站点当天新产出链接立即通过此方式推送给百度,以保证新 ...

  4. SSE(服务器推送事件)的介绍、问题及解决

    文章目录 [隐藏] SSE(服务器推送事件)的介绍.问题及解决 SSE(服务器推送事件)的介绍.问题及解决 seymour1996 关注 2018.01.13 22:16* 字数 988 阅读 85 ...

  5. 【HMS Core】华为登录后返回错误码 8 、账号服务如何授权、推送服务端获取用户信息异常

    1.[HMS core][游戏登陆][问题描述] 调用华为登录后返回错误码 8 [解决方案] 错误码8的话一般在定义为内部错误(引起该错误码的原因很多),但是一般重试基本可以解决该问题(错误码).如果 ...

  6. git本地仓库 推送到远程 完整流程 及 错误解决方法

    git推送本地仓库到远程 以下为通过IDEA亲测可完整实现本地仓库推送到远程的流程 完整流程 git init //初始化本地仓库 git remote rm origin //删除与远程仓库连接关系 ...

  7. android极光推送no bit-32,iOS极光推送详解以及碰到问题和解决方式

    前言: 极光推送作为较成熟的推送平台,通过集成基础操作.为客户端以及服务端节省了大量时间来实现推送效果. ​ 就iOS端来说要注意整体流程,首先,iOS端推送时分为两个状态,开发环境与生产环境,每个环 ...

  8. JxlExcelUtil生成Excel供SFTP推送Java实现获取目录下最新的excel

    外汇交易中心,某版本需求变更:交易后将每天定时生成的全量黄金成交编号映射关系文件推送至金交所. 设计: 定时任务跑批实现. 1.根据传入的参数是否是给黄金交易所判断 isSFTP.传参, 以下方法组装 ...

  9. APP推送中首次获取 ios clientid的值获取不到。

    在APP做推送中Android和ios在获取时用到clientid(cid),Android的会在第一次登陆时就能获取到,但是在ios中会在第二次登陆中才能获取到. 经过多次的实验有一种方法能快速获取 ...

最新文章

  1. 应用监控CAT之cat-client源码阅读(一)
  2. LSTM之父发文抨击本田奖:请尊重原创!
  3. python学习笔记之socket(第七天)
  4. linux运维必学python吗_linux运维一定要学python吗?
  5. poj 1679 判断最小生成树是否唯一
  6. jax-ws实现WebService
  7. 《敏捷敬业度》作者访谈
  8. linux安装dev命令,Linux安装与基础命令
  9. 组态王授权产品id和授权锁id_软件加密授权工具推荐
  10. .NET使用存储过程实现对数据库的增删改查
  11. 《动手学深度学习》网页版
  12. PetaLinux学习笔记 1
  13. html页面去边框线,html中怎么去掉表格边框线的?
  14. 准备安装Cent OS需要的配件
  15. shell两行输出为一行
  16. python 下如何播放mp3
  17. Anaconda Navigator卡logo打不开闪退问题处理方案-更换阿里云镜像源
  18. 为何晶振并联一个1MΩ电阻?晶振低温不起振如何解决?
  19. 又爆作弊丑闻?AAAI 2023审稿人被作者找上门,才发现rebuttal阶段审稿人互相可见...
  20. 【数据结构】图-图的遍历_深度优先遍历(动态图解、c++、java)

热门文章

  1. autocad显卡驱动文件hdi_AUTOCAD启动提示显示驱动文件丢失怎么办?hdi是什么文件?...
  2. 【bzoj 3680】吊打XXX
  3. SIGCOMM‘2021 Congestion Detection in Lossless Networks 阅读笔记
  4. 题目:对给定10个国家名,按字母顺序输出
  5. 谈谈第一次Web项目的感想(上)
  6. 香港计算机专业硕士学制几年,想读香港一年计算机硕士,懂行老哥来指导一下我这个迷茫的five...
  7. Julia ---- 为Julia做一下辩解
  8. 萨尔大学计算机受限,萨尔大学的申请条件
  9. linux 终端隐藏光标,如何在gnome-terminal中禁用闪烁的光标?
  10. 看最强大脑中英PK小记