iOS 10 Request Notification Permission fires twice

前端 未结 1 1262
盖世英雄少女心
盖世英雄少女心 2021-01-24 18:22

When I launch my app on iOS 10, I get the request notification permission twice. The first one briefly appears and disappears immediately without allow

相关标签:
1条回答
  • 2021-01-24 19:26

    For iOS 10 we need to call the UNUserNotificationCenter in appDelegate didFinishLaunchingWithOptions method.

    First we must import the UserNotifications framework and add the UNUserNotificationCenterDelegate in Appdelegate

    AppDelegate.h

    #import <UIKit/UIKit.h>
    #import <UserNotifications/UserNotifications.h>
    
    @interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
    
    @property (strong, nonatomic) UIWindow *window;
    
    @end
    

    AppDelegate.m

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
       if([[[UIDevice currentDevice]systemVersion]floatValue]<10.0)
       {
          [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound |    UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
          [[UIApplication sharedApplication] registerForRemoteNotifications];
       }
       else
       {
          UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
          center.delegate = self;
          [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error)
          {
             if( !error )
             {
                 [[UIApplication sharedApplication] registerForRemoteNotifications];
                 NSLog( @"Push registration success." );
             }
             else
             {
                 NSLog( @"Push registration FAILED" );
                 NSLog( @"ERROR: %@ - %@", error.localizedFailureReason, error.localizedDescription );
                 NSLog( @"SUGGESTIONS: %@ - %@", error.localizedRecoveryOptions, error.localizedRecoverySuggestion );  
             }  
         }];  
       }
      return YES;
    }
    

    For more details

    0 讨论(0)
提交回复
热议问题