使用新通知机制点击本地通知时如何打开应用程序?

时间:2017-01-12 07:40:16

标签: ios objective-c iphone ios10 uilocalnotification

从iOS10开始不推荐使用

application:didReceiveLocalNotification:,而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];

我已经使用适当的内容和触发器创建了上面的通知。现在我想在我点击通知时打开应用程序,但我似乎无法弄清楚如何。

1 个答案:

答案 0 :(得分:0)

userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:

如果要执行自定义操作,则需要实现UNUserNotificationCenterDelegate中的此方法。

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
    if ([response.actionIdentifier isEqualToString:UNNotificationDefaultActionIdentifier]) {
        NSLog(@"app opened");
    }
}