文档章节

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

豆花饭烧土豆
 豆花饭烧土豆
发布于 2016/11/15 00:03
字数 991
阅读 101
收藏 0

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://blog.csdn.net/sharpyl/article/details/53135952

© 著作权归作者所有

共有 人打赏支持
豆花饭烧土豆
粉丝 15
博文 355
码字总数 93005
作品 0
深圳
私信 提问
iOS10推送通知(本地&远程)/Swift

iOS10本地通知 一.发送一个简单的本地通知 1.注册通知

秦无炎
2016/12/22
247
0
iOS Push详述,了解一下?

欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由WeTest质量开放平台团队发表于云+社区专栏 作者:陈裕发, 腾讯系统测试工程师 商业转载请联系腾讯WeTest获得授权,非商业...

腾讯云加社区
10/26
0
0
《小印记》iOS源码分享--极光推送实践篇

笔者这几天刚完成《小印记》的推送功能,今天特分享一下在做的过程中实际解决的问题。如果读者学到了有用的东西,希望能前往App Store下载《小印记》支持一下笔者,谢谢! 《小印记》iOS源码...

_子墨
2017/03/21
0
2
react-native 使用leanclound消息推送

iOS消息推送的基本流程 1.注册:为应用程序申请消息推送服务。此时你的设备会向APNs服务器发送注册请求。2. APNs服务器接受请求,并将deviceToken返给你设备上的应用程序 3.客户端应用程序将...

ding_ios
08/05
0
0
个推推送iOS版 常见问题详解

1、提交了.p12文件后多久可以测试? 提交后10分钟左右才可以测试,并不是立即生效的。 2、应用在后台时接收不到消息,即APNS消息接收不到? 1. 先去查看CID和APPID绑定是否正确。 2. 根据CID...

个推
2015/05/05
12.6K
2

没有更多内容

加载失败,请刷新页面

加载更多

CentOS配置Tomcat监听80端口,虚拟主机

Tomcat更改默认端口为80 更改的配置文件是: /usr/local/tomcat/conf/server.xml [root@test-a ~]# vim /usr/local/tomcat/conf/server.xml # 找到 Connector port="8080" protocol="HTTP/1......

野雪球
今天
5
0
《稻盛和夫经营学》读后感心得体会3180字范文

《稻盛和夫经营学》读后感心得体会3180字范文: 一代日本经营之圣稻盛和夫凭借刻苦勤奋的精神以及深植于佛教的商业道德准则,成为了“佛系”企业家的代表人物。在《稻盛和夫经营学》“领导人...

原创小博客
今天
3
0
java框架学习日志-5(常见的依赖注入)

依赖注入(dependency injection) 之前提到控制反转(Inversion of Control)也叫依赖注入,它们其实是一个东西,只是看的角度不同,这章详细说一下依赖注入。 依赖——指bean对象创建依赖于...

白话
今天
4
0
红外接收器驱动开发

背景:使用系统的红外遥控软件没有反应,然后以为自己接线错误,反复测试,结果烧坏了一个红外接收器,信号主板没有问题。所以自己开发了一个红外接收器的python驱动。接线参见https://my.os...

mbzhong
今天
2
0
ActiveMQ消息传送机制以及ACK机制详解

AcitveMQ是作为一种消息存储和分发组件,涉及到client与broker端数据交互的方方面面,它不仅要担保消息的存储安全性,还要提供额外的手段来确保消息的分发是可靠的。 一. ActiveMQ消息传送机...

watermelon11
今天
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部