点击本地通知未触发

Posted

技术标签:

【中文标题】点击本地通知未触发【英文标题】:Tap Local Notifications isn't triggered 【发布时间】:2017-08-07 10:27:13 【问题描述】:

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注册通知导入UserNotifications/UserNotifications.h框架,并在收到通知时使用通知代理UNUserNotificationCenterDelegate响应通知。

@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   #########//

【讨论】:

以上是关于点击本地通知未触发的主要内容,如果未能解决你的问题,请参考以下文章

为啥本地通知未针对 UNCalendarNotificationTrigger 触发

工作日本地通知未触发

本地通知未在 Android 10 中触发

UNUserNotificationCenter Swift - 在特定情况下未触发本地通知

Swift 3 本地通知未触发

应用程序在后台时未触发 iOS 本地通知