当使用新的通知机制点击本地通知时,如何打开应用程序?
How do I open the app when local notification is tapped with the new notification mechanisms?
application:didReceiveLocalNotification:
从 iOS10 开始被弃用,并且 developer page 没有指向任何替代方案。
UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"localnotification"
content:content
trigger:trigger];
UNUserNotificationCenter* notificationCenter = [UNUserNotificationCenter currentNotificationCenter];
[notificationCenter requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(@"request authorization succeeded!");
}
}];
[notificationCenter addNotificationRequest:request
withCompletionHandler:nil];
我已经创建了一个通知,如上所示,包含适当的内容和触发器。现在我希望应用程序在我点击通知时打开,但我似乎无法弄清楚如何。
userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:
如果您想执行自定义操作,则需要实施 UNUserNotificationCenterDelegate
中的此方法。
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
if ([response.actionIdentifier isEqualToString:UNNotificationDefaultActionIdentifier]) {
NSLog(@"app opened");
}
}
application:didReceiveLocalNotification:
从 iOS10 开始被弃用,并且 developer page 没有指向任何替代方案。
UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"localnotification"
content:content
trigger:trigger];
UNUserNotificationCenter* notificationCenter = [UNUserNotificationCenter currentNotificationCenter];
[notificationCenter requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(@"request authorization succeeded!");
}
}];
[notificationCenter addNotificationRequest:request
withCompletionHandler:nil];
我已经创建了一个通知,如上所示,包含适当的内容和触发器。现在我希望应用程序在我点击通知时打开,但我似乎无法弄清楚如何。
userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:
如果您想执行自定义操作,则需要实施 UNUserNotificationCenterDelegate
中的此方法。
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
if ([response.actionIdentifier isEqualToString:UNNotificationDefaultActionIdentifier]) {
NSLog(@"app opened");
}
}