【问题标题】:didReceiveRemoteNotification not being called when sending message from a custom service从自定义服务发送消息时未调用 didReceiveRemoteNotification
【发布时间】:2017-07-31 02:08:35
【问题描述】:

我有一个自定义通知服务可以成功发送通知,但是当用户点击通知时,didReceiveRemoteNotification 永远不会被调用。该应用程序将打开到最后一个状态,我正在尝试深度链接到应用程序中的特定场景。

在我的 didFinishLaunchingWithOptions 中,我正在注册通知类型 通过调用这个函数”

func registerForPushNotifications(应用程序:UIApplication){ 让 notificationSettings = UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil) UIApplication.shared.registerUserNotificationSettings(notificationSettings) }

我正在将我的设备注册到后端服务并调用 didRegisterForRemoteNotificationsWithDeviceToken 函数

接下来我有我的 didregister 函数

func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) { UIApplication.shared.registerForRemoteNotifications() }

最后,我得到了 didReceiveRemoteNotification,我希望在点击通知时调用它。

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

发布到我的后端服务的 xml 相当简单,似乎按预期工作: { “观众”:{ “ios_channel”:“e06b9r8r-ffce-4fa6-92ec-123456789” }, “通知”:{ “ios”:{ "alert":"测试声音发送", "title":"测试警报", “声音”:“默认” } }, “设备类型”:[ “ios” ] }

如果有人对为什么不调用 didReceiveRemoteNotification 函数有意见,我们将不胜感激。

【问题讨论】:

  • 我在发布后不久就解决了这个问题。我正在使用 Urban Airship v1.0,UA 库将自动集成到应用程序中,该应用程序拦截任何返回到应用程序的调用。要解决此问题,请在 AirshipConfig.plist 文件或 UAConfig 中将“automaticSetupEnabled”添加到 NO。 UA 在这里解释了这一变化:docs.urbanairship.com/platform/ios.html#automatic-integration

标签: swift3 ios9.3


【解决方案1】:

didReceiveRemoteNotification 仅在您的应用打开时触发。要响应用户点击通知,您需要检查didFinishLaunchinglaunchOptions 参数中的UIApplicationLaunchOptionsRemoteNotificationKey 值。

【讨论】:

  • 仅供将来遇到此问题的任何人使用。 didReceiveRemoteNotification 这里的意思是application(_:didReceiveRemoteNotification:) 而不是application(_:didReceiveRemoteNotification:fetchCompletionHandler:),即使应用在后台也会被调用。
【解决方案2】:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  if let userInfo = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] {
            NSLog("[RemoteNotification] applicationState: \(applicationStateString) didFinishLaunchingWithOptions for iOS9: \(userInfo)")
            //TODO: Handle background notification


        }
        return true
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-13
    • 2011-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多