【问题标题】:iOS push notifications - didReceiveRemoteNotification:fetchCompletionHandler: never called in foregroundiOS 推送通知 - didReceiveRemoteNotification:fetchCompletionHandler: 从未在前台调用
【发布时间】:2017-08-08 11:31:08
【问题描述】:

我已经在我的应用中实现了远程通知,但是我遇到了didReceiveRemoteNotification:fetchCompletionHandler: 的问题。

当手机屏幕被锁定时,我会收到通知。如果用户滑动通知,应用返回前台并调用didReceiveRemoteNotification:fetchCompletionHandler:

但如果应用程序在前台运行,则永远不会调用 didReceiveRemoteNotification:fetchCompletionHandler:。可能是什么原因?

我正在使用 iOS 10.3.2

【问题讨论】:

  • 尝试在其中放置警报并在前台模式下检查。
  • @mag_zbc 假设您在 iOS9 上工作以使用此方法,而不是在 ios10 和 UNUserNotificationCenter 上

标签: ios objective-c push-notification


【解决方案1】:

您正在使用哪个 iOS 版本?

iOS 9 和 iOS 10 有不同的方法。

以下是适用于 iOS 10 的版本

    //Called when a notification is delivered to a foreground app.
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    }


    //Called to let your app know which action was selected by the user for a given notification.
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

    }

对于 iOS 9,请务必在 didBecomeActive

中注册您的通知
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
UIApplication.shared.registerForRemoteNotifications()

在你的 didReceiveRemoteNotification

func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) {

if application.applicationState == .active {
//foreground state
 }

}

最好的方法是在 didReceiveRemoteNotification 中放置一个调试器并进一步检查。

【讨论】:

  • 确保导入 UserNotifications 检查这个答案stackoverflow.com/q/38940193/2570153
  • 虽然我使用的是 Objective-C,而不是 Swift,但这是正确的答案。我忘了实现 UNUserNotificationCenterDelegate 方法,这些方法在前台调用,而不是 iOS 10 中的 didReceiveRemoteNotification:fetchCompletionHandler:
  • @mag_zbc 太棒了!
猜你喜欢
  • 1970-01-01
  • 2019-06-26
  • 1970-01-01
  • 1970-01-01
  • 2016-01-23
  • 1970-01-01
  • 2019-06-24
  • 1970-01-01
  • 2020-06-14
相关资源
最近更新 更多