【问题标题】:How to get notification payload data when app is in received notification in Background当应用程序在后台收到通知时如何获取通知有效负载数据
【发布时间】:2018-05-08 12:21:43
【问题描述】:

当应用程序处于后台时,我已成功收到通知,但在 iOS 11 中未调用通知回调方法 didReceiveRemoteNotification 以使用 Swift4。当应用程序在后台收到通知时,我想获取通知有效负载数据。我正在使用 firebase 控制台发送通知。当应用打开时,会调用 willPresent。但是在后台没有调用任何方法 我正在使用代码:-

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any])
    {
        print(userInfo)
         saveNotificationDetail(userInfo: userInfo)
    }
func notificationIntegrate(application: UIApplication)
    {
        FirebaseApp.configure()
        Messaging.messaging().shouldEstablishDirectChannel = true
        Messaging.messaging().delegate=self
        // [START register_for_notifications]
        if #available(iOS 10.0, *) {
            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.current().delegate = self

            let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
            UNUserNotificationCenter.current().requestAuthorization(
                options: authOptions,
                completionHandler: {_, _ in })
        } else {
            let settings: UIUserNotificationSettings =
                UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }

        application.registerForRemoteNotifications()

        // [END register_for_notifications]
        // Add observer for InstanceID token refresh callback.
        NotificationCenter.default
            .addObserver(self, selector: #selector(AppDelegate.tokenRefreshNotification),
                         name: NSNotification.Name.InstanceIDTokenRefresh, object: nil)
    }
@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {

    // Receive displayed notifications for iOS 10 devices.
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        let userInfo = notification.request.content.userInfo

        // Print full message.
        print(userInfo)
        saveNotificationDetail(userInfo: userInfo)
        completionHandler(UNNotificationPresentationOptions.alert)
        // Change this to your preferred presentation option
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void)
    {
        let userInfo = response.notification.request.content.userInfo
        print(userInfo)

    }
}

【问题讨论】:

标签: ios swift firebase notifications firebase-cloud-messaging


【解决方案1】:

推送通知中的配置设置可能存在问题。验证以下步骤:

1.检查您是否在Capabilities 选项卡中为您的应用程序启用了remote-notifcationbackground-fetch

2。推送通知负载由content-available 键组成:

key content-available 是一个新特性,正是这个key使无声推送成为可能。

您可以通过 content-available=1 启用。但是,使用 content-available=0 禁用是错误的。要禁用,您必须删除有效负载中的密钥。

另外,验证您的有效负载中是否存在以下密钥

{
  "aps": {
      "alert": {
          "title": "", (“notification_title” will be here)
          "subtitle": "",
          "body": “”(“description” will be here)
      },
      "badge": 1,
      "sound": "default",
      "content-available": 1
  }
}

3.您的通知行为将如下所示:

应用在前台

  • 未显示系统警报
  • application:didReceiveRemoteNotification:fetchCompletionHandler: 被调用

应用在后台

  • 显示系统警报
  • application:didReceiveRemoteNotification:fetchCompletionHandler: 被调用

应用处于暂停状态

  • 应用状态更改为背景
  • 显示系统警报
  • application:didReceiveRemoteNotification:fetchCompletionHandler: 被调用

应用未运行,因为被用户杀死

  • 显示系统警报
  • 不调用回调

【讨论】:

  • 这解决了一个不同的问题,不是吗?有效负载密钥已在 APN 中。后台获取是当您的内容 > 4kb(VOIP APN 为 5kb)时——在这种情况下,您不发送有效负载,而是发送 url。他正在设置一个名为“payload”的密钥,之后它不在UserInfo中(虽然apn密钥在那里)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-11
  • 1970-01-01
相关资源
最近更新 更多