通过 Firebase 的推送通知未出现在 iOS 上且应用程序处于后台

Push notifications via Firebase not appearing on iOS with app on background

我们正在为 AndroidiOS 开发具有 Push NotificationsIonic 应用程序。我们通过 Firebase Cloud Messaging

发送这些通知

iOS 我们遇到了这个问题(我们已经接受了该应用程序的通知权限):

我们有下一个配置:

  • Xcode: Capabilities -> Background Modes -> Remote Notifications (shows one check)
  • Xcode: Capabilities -> Push Notifications (shows two checks)
  • Apple Developer: Identifiers -> my app -> Push Notifications (with certificates for both dev and prod)
  • Apple Developer: Keys -> .p8 key with "Apple Push Notifications service (APNs)"
  • Firebase: my app -> Configuration -> Cloud Messaging (.p8 file uploaded with key and team IDs)

这是我们尝试通过 Firebase POST API 发送的几个通知之一,它结合了我们在研究这个问题时阅读的几乎所有内容。 (我们还发送所需的 Firebase HTTP headers)

{
   "to": "<firebase_token>",
   "notification": {
      "body": "NOTIFICATION BODY",
      "title": "NOTIFICATION TITLE",
   },
   "apns": {
      "headers": {
        "apns-push-type": "alert",
        "apns-expiration": 0,
        "apns-priority": 5,
        "apns-topic": "<my_app>"
      },
      "payload": {
         "alert": {
            "title": "NOTIFICATION TITLE",
            "body": "NOTIFICATION BODY"
         },
         "aps": {
            "content-available": 1,
            "alert": {
                "title": "NOTIFICATION TITLE",
                "body": "NOTIFICATION BODY"
             }
         },
         "sound": "default",
         "content-available": 1
      }
   },
   "data": {
      "field": "1",
      "type": "CHAR"
   }
}

尽管此 POST 请求对于免费通知可能不正确,但我们认为这不是问题,因为 Firebase 测试通知在后台时也无法显示。

我不确定,但是你的payload结构好像不对。

请参考以下示例

{
     "to" : "FCM TOKEN",
     "notification" : {
         "body" : "Body of Your Notification",
         "title": "Title of Your Notification"
     },
     "data" : {
         "body" : "Body of Your Notification in Data",
         "title": "Title of Your Notification in Title",
         "key_1" : "Value for key_1",
         "key_2" : "Value for key_2"
     }
}

如果您使用的是普通 APNS,则无需通过 content-available

我们终于可以在后台和前台获取应用程序的通知了。

Firebase 的负载是:

{
   "to": "<firebase token>",
   "notification": {
      "title": "TITLE",
      "body": "BODY"
   },
   "data": {
      "title": "TITLE",
      "body": "BODY",
      "extraField1": "extra value 1",
      "extraField2": "extra value 2"
   },
   "apns": {
      "headers": {
         "apns-topic": "<my-app>",
         "apns-push-type": "background",
         "apns-priority": 10
      }
   }
}

更新到 iOS 13 后,.p12 证书似乎有问题,我们使用 .p8 证书解决了这个问题。在 Firebase 更改证书后,开始收到后台通知,但前几个小时前台停止。

在 XCode 日志中查找消息。这是我得到的,见下文:

在访问body的js代码中,使用data.aps.alert.body

this.fcm.onNotification().subscribe(data => {
            console.log("notifiation data", data);
            alert(data.aps.alert.body);


    Message ID 1: 1593974546767029
    {
        aps =     {
            alert =         {
                body = "Hi, this is ios and android test 6!!!";
                title = " iOS and Android Test";
            };
            badge = 1;
            sound = default;
        };
        "gcm.message_id" = 1593974546767029;
        "gcm.n.e" = 1;
        "gcm.notification.sound2" = default;
        "google.c.a.c_id" = 2904766990309581961;
        "google.c.a.e" = 1;
        "google.c.a.ts" = 1593974546;
        "google.c.a.udt" = 0;
        "google.c.sender.id" = 291488852090;
    }