如何在应用程序处于后台接收通知时获取通知有效负载数据

时间:2018-05-08 12:21:43

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

当应用程序处于后台时,我已成功收到通知但是在iOS 11中未使用Swift4调用回复方法 didReceiveRemoteNotification 。我想在应用程序处于后台收到的通知中时获取通知有效负载数据。我正在使用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)

    }
}

1 个答案:

答案 0 :(得分:1)

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

<强> 1。检查您是否在remote-notifcation标签中为您的应用启用了background-fetchCapabilities

enter image description here

<强> 2。推送通知有效负载由content-available密钥组成:

  

关键内容 - 可用是一项新功能,正是这一关键使得无声推送成为可能。

     

您可以使用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。您的通知行为将如下所示:

App位于前景

  • 未显示系统提醒
  • application:didReceiveRemoteNotification:fetchCompletionHandler:被称为

应用在背景中

  • 显示系统警报
  • application:didReceiveRemoteNotification:fetchCompletionHandler:被称为

应用已暂停

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

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

  • 显示系统警报
  • 没有回调被召唤