如何使用Firebase获取通知响应数据?

Ayu*_*sal 5 ios firebase swift firebase-cloud-messaging

我已经实施了FCM通知来发送通知.当我从firebase控制台发送通知时,我收到通知.但是当我从服务器发送通知时,我获得了成功,但通知没有出现..

其次,如何获取通知响应数据?

我的didReceiveRemoteNotification功能也不起作用.这是我的代码:

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

 print("Recived: \(userInfo)")
    print("success")
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

    print("Recived: \(userInfo)")
    print("success")
}

func application(application: UIApplication,  didReceiveRemoteNotification userInfo: [NSObject : AnyObject],  fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {

    print("Recived: \(userInfo)")
    print("success")
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {

    self.application(application, didReceiveRemoteNotification: userInfo) { (UIBackgroundFetchResult) in

       print("Recived: \(userInfo)")
    print("success")
    }

   print("Recived: \(userInfo)")
    print("success")

    let state: UIApplicationState = application.applicationState
    // user tapped notification while app was in background
    if state == .inactive || state == .background {
        print("Inactive")
    }
    else {
        print("Active")
    }
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*oni 0

我发现下面的消息最适合我在 Android 和 iOS 上使用(我使用 firebase 函数来发送它,但这并不重要)。请注意,要实现此功能,您需要使用 send 方法,而不是已弃用的方法之一,因此类似于:admin.messaging().send(message)

发送至以下的消息send

const message = {
    token: addresseePushToken,
    /*
       Apple Push Notification Service
       https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns
    */
    apns: {
      headers: {
        'apns-priority': 10,        // defaults to 10 (send immediately), can be set to 5 (consider device power)
        'apns-collapse-id': chatId  // group multiple notifications into a single notification (must not exceed 64 bytes)
      },
      payload: {
        aps: {
          alert: {
            title: title,
            body: doc.message,
          },
          badge: unseenCount+1,
        }
      }
    },
    /*
       Android specifics
       https://firebase.google.com/docs/cloud-messaging/admin/send-messages#android_specific_fields
    */
    android: {
      priority: 'high',               // either 'normal' or 'high' - high send immediately & waking sleeping device
      notification: {                 // notification object creates status bar notification if app in background
        tag: chatId,                  // key for grouping notifications (Android only)
        title: title,
        body: doc.message,
        color: '#ffffff',             // notification's icon color
        icon: thumbUrl,               // if not specified FCM displays the launcher icon as defined in app manifest
      }
    },
    // data object is available to the app both when received in foreground and background
    data: {
      type: 'chat',
      senderId: senderId,
      title: title,
      body: doc.message,
    },
  }
Run Code Online (Sandbox Code Playgroud)