未触发点击本地通知
Tap Local Notifications isn't triggered
在 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
对于注册通知导入 UserNotifications/UserNotifications.h
框架 AppDelegate
并使用通知委托 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 #########//
在 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
对于注册通知导入 UserNotifications/UserNotifications.h
框架 AppDelegate
并使用通知委托 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 #########//