【问题标题】:How do I open the app when local notification is tapped with the new notification mechanisms?当使用新的通知机制点击本地通知时,如何打开应用程序?
【发布时间】:2017-01-12 07:40:16
【问题描述】:

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

我已经创建了一个如上所示的通知,其中包含适当的内容和触发器。现在我希望应用程序在我点击通知时打开,但我似乎无法弄清楚如何。

【问题讨论】:

标签: ios objective-c iphone ios10 uilocalnotification


【解决方案1】:

userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-29
    相关资源
    最近更新 更多