2017-08-07 49 views
0

在iOS的10.3我加入的代码,到AppDelegate点击本地通知不会被触发

@interface AppDelegate() { 
    UNUserNotificationCenter *center; 
} 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

    center = [UNUserNotificationCenter currentNotificationCenter]; 
    UNAuthorizationOptions options = UNAuthorizationOptionAlert + UNAuthorizationOptionSound; 
    [center requestAuthorizationWithOptions:options 
          completionHandler:^(BOOL granted, NSError * _Nullable error) { 
           if (!granted) { 
            DLog(@"Something went wrong"); 
           } else { 
            DLog(@"Access Granted") 
           } 
          }]; 

    return YES; 
} 

我轻按通知,但该方法波纹管不叫。

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 

回答

1

对于注册在AppDelegate和使用的通知委托UNUserNotificationCenterDelegate通知进口UserNotifications/UserNotifications.h框架,以响应通知时,通知领取。

@interface AppDelegate()<UNUserNotificationCenterDelegate> 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    // Override point for customization after application launch. 

    //Notification Setup. 
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) { 
     UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; 
     center.delegate = self; 
     [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){ 
      if(!error){ 
       [[UIApplication sharedApplication] registerForRemoteNotifications]; 
      } 
     }]; 
    } else { 

     //register to receive notifications 
     UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:nil]; 
     [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; 

    } 


    return YES; 
} 

#ifdef __IPHONE_10_0 
////#### iOS 10 #########// 
//This is call when application is open. 
- (void)userNotificationCenter:(UNUserNotificationCenter *)center 
     willPresentNotification:(UNNotification *)notification 
     withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler 
{ 
    NSLog(@"Userinfo %@",notification.request.content.userInfo); 
} 


//This is call when application is in background. 
- (void)userNotificationCenter:(UNUserNotificationCenter *)center 
didReceiveNotificationResponse:(UNNotificationResponse *)response 
     withCompletionHandler:(void(^)())completionHandler 
{ 
    NSLog(@"Userinfo %@",response.notification.request.content.userInfo); 

    // Must be called when finished 
    completionHandler(); 
} 
#endif 
////#### iOS 10 End #########//