【问题标题】:didReceiveLocalNotification not getting called after using UNUserNotificationCenter使用 UNUserNotificationCenter 后没有调用 didReceiveLocalNotification
【发布时间】:2016-09-02 21:20:39
【问题描述】:

我正在安排本地通知。它适用于 iOS 9.x,但从 iOS 10 开始

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

当应用在 iOS 10 上运行时不会被调用。

我知道 iOS 引入了新的 UserNotifications 框架,但这不应该停止运行 iOS 9 API。

我该如何解决这个问题?

【问题讨论】:

标签: ios cocoa-touch uilocalnotification usernotifications


【解决方案1】:

如您所知,iOS 10 引入了UNUserNotifications 框架来处理本地和远程通知。使用此框架,您可以设置一个委托来检测何时呈现或点击通知。

[UNUserNotificationCenter currentNotificationCenter].delegate = yourDelegate;

...

// In your delegate ...

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {

    // Notification arrived while the app was in foreground

    completionHandler(UNNotificationPresentationOptionAlert);
    // This argument will make the notification appear in foreground
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void (^)())completionHandler {

    // Notification was tapped.

    completionHandler();
}

现在,如果您仍想使用旧的(已弃用)application:didReceiveLocalNotificationapplication:didReceiveRemoteNotification:fetchCompletionHandler,解决方案很简单:只需不要将任何委托设置为 UNUserNotificationCenter

请注意,即使您设置了委托,静默远程通知(包含content-available 键且不包含alertsoundbadge 的通知)始终由application:didReceiveRemoteNotification:fetchCompletionHandler 处理。

【讨论】:

  • 我会试一试,让你知道这是否有效。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多