iOS10全新方法實現推送+deviceToken沒法獲取或無效的解決

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 Frameworkphp

?服務器

1app

#import <UserNotifications/UserNotifications.h>ide


 

?fetch

1ui

2spa

3.net

4插件

53d

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

相關文章
相關標籤/搜索