簡體   English   中英

Firebase 雲消息傳遞未以正確格式為 iOS 通知內容和服務擴展發送 aps 負載

[英]Firebase Cloud Messaging not sending aps payload in correct format for iOS Notification Content & Service Extensions

我正在嘗試使用 Firebase 實現通知。 當應用程序在后台或前台時,可以正確接收通知。 因此,基本機制正在發揮作用。

現在我已經向應用程序添加了內容擴展和服務擴展。 當我使用本地通知時,內容擴展有效,但就可選字段而言,Firebase 消息負載似乎不正確。 這是我的控制台圖像的鏈接:

帶有可變內容和類別選項的 Firebase 控制台圖像

這是遇到的 Firebase 遠程通知有效負載(為了匿名編輯了一些長谷歌數字:

{
    aps = 
    {
        alert = 
        {
        body = "Eureka! 11";
        title = "Patient is not doing well";
        };
    };
    category = provider-body-panel;
    gcm.message_id = 0:149073;
    gcm.n.e = 1;
    google.c.a.c_id = 2825604;
    google.c.a.e = 1;
    google.c.a.ts = 149073;
    google.c.a.udt = 0;
    mutable-content = 1;
}

似乎“類別”和“可變內容”不在正確的位置。 它們應該在 aps 負載中。

如何將這些選項包含在有效負載中,以便我的應用程序可以正確解析它並將其與內容和服務擴展連接?

首先,我要提到 FCM 有兩種類型的消息有效負載。 notificationdata 請參閱此處的文檔

通過 Firebase 通知控制台發送通知時,它將被視為notification負載。 但是,如果您添加Custom Data ,它會將其作為自定義鍵值對添加到有效負載中。

例如,在您的帖子中,FCM 負載應如下所示:

{
    "notification": {
        "body" : "Eureka!",
        "title": "Patient is not doing well"
    },

    "data": {
        "category": "provider-body-panel",
        "mutable-content" : true,
        "click_action" : "provider-body-panel"
    }
}

怎么了?

  • click_action應該在內部notification
  • mutable-content應該是mutable_content (注意下划線)並且應該與notification處於同一級別
  • 這個我可能誤解了,但是) FCM 沒有category參數, click_action已經對應它。

請參閱此處的參數文檔。

使用 Firebase 通知控制台時,目前無法設置click_actionmutable_content的值。 您必須自己構建有效負載,如下所示:

{
    "to": "<REGISTRATION_TOKEN_HERE>",
    "mutable_content" : true,
    "notification": {
        "body" : "Eureka!",
        "title": "Patient is not doing well",
        "click_action" : "provider-body-panel"
    }
}

然后從您自己的應用服務器發送它。 您也可以使用 PostmancURL 執行此操作

“mutable-content 應該是”mutable_content”(firebase 服務器的關鍵字作為 IOS 的可變內容發送),正如您在帖子中提到的,我認為您沒有進行編輯。

下面是一個示例,其中還包含發送到 FCM 服務器的 json 中數據部分的更正格式。 所以更新將是:

{ 
  "to" : "YOUR firebase messaging registration id here",
  "mutable_content":true,
  "notification": {
    "title": "Its about time",
     "body": "To go online Amigo",
     "click_action": "NotificationCategoryIdentifier ForYourNotificationActions"
  },
  "data":{
    "customKey":"custom data you want to appear in the message payload"
    "media-attachment":"mycustom image url",
    "catalogID":"mycustom catalog for my custom app"
  }
}

更新 Firebase Admin SDK 並使用sendMulticast(payload)方法

var admin = require("firebase-admin")

admin.initializeApp({
    credential: admin.credential.applicationDefault(),
});

// Create a list containing up to 500 registration tokens.
// These registration tokens come from the client FCM SDKs.
const registrationTokens = [
    'YOUR_REGISTRATION_TOKEN_1',
    // …
    'YOUR_REGISTRATION_TOKEN_N',
];

// See documentation on defining a message payload.
var message = {
    notification: {
        title: '$FooCorp up 1.43% on the day',
        body: '$FooCorp gained 11.80 points to close at 835.67, up 1.43% on the day.'
    },
    tokens: registrationTokens,
    apns: {
        payload: {
            aps: {
                'mutable-content': true,      // use single quote  
                'category': 'INVITE_CATEGORY' // use single quote   
            }
        },
    },
};

// Send a message to the device corresponding to the provided
// registration tokens.
admin.messaging().sendMulticast(message)
  .then((response) => {
    if (response.failureCount > 0) {
      const failedTokens = [];
      response.responses.forEach((resp, idx) => {
        if (!resp.success) {
          failedTokens.push(registrationTokens[idx]);
        }
      });
      console.log('List of tokens that caused failures: ' + failedTokens);
    }
  });

參考: https : //firebase.google.com/docs/cloud-messaging/send-message#send_messages_to_specific_devices

這對我使用 Node.js 的 Cloud 函數有用

const payload = {
    notification: {
        title: name,
        body: messageText,
        badge: "1",
        mutable_content: "true"
    },
    data: {
        type: "MESSAGE",
        fromUserId: name,
        attachmentUrl: imageUrl
    }};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM