简体   繁体   中英

How do I open the app when local notification is tapped with the new notification mechanisms?

application:didReceiveLocalNotification: is deprecated starting iOS10, and the developer page doesn't point to any alternatives.

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];

I have created a notification as seen above with the appropriate content and trigger. Now I want the application to open when I tap said notification, but I can't seem to figure out how.

userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:

This method from UNUserNotificationCenterDelegate needs to be implemented if you want to perform a custom action.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM