1

根据react-native-fcm软件包,您可以在数据对象中为FCM消息传递有效负载包含自定义嵌套对象。FCM消息有效负载错误,数据对象内的嵌套对象错误输出

according to this post by the package author

像这样:

var payload = { 
    data: { 
     custom_notification: { 
      title: 'title', 
      body: 'body', 
      priority: 'high', 
      id: 'id', 
      group: 'group' 
     } 
    } 
}; 

这是在所有的应用程序的状态,如果你只是一个通知有效载荷或数据载荷,这将不会发生接收抬头通知的目的。

当我实现这个在我的云功能,我得到以下错误:

Error: Messaging payload contains an invalid value for the "data.custom_notification" property. Values must be strings. 

所以我在亏损为其他人如何成功地使用呢?

我不知道是否有一些问题,我的环境什么的,将其通过火力支持,给我的(并且是文档)下面的测试有效载荷事情出现了错误:

var payload = { 
"to":"FCM_TOKEN", 
"data": { 
"type":"MEASURE_CHANGE", 
"body": "test body", 
"title": "test title", 
"color":"#00ACD4", 
"priority":"high", 
"id": "id", 
"show_in_foreground": true 
} 
}; 

我得到的以下错误:

Error sending message stringify: {"code":"messaging/invalid-payload","message":"Messaging payload contains an invalid \"to\" property. Valid properties are \"data\" and \"notification\"."} 

在这里呆了几天,所以希望我能在这方面得到一些帮助。

在此先感谢!

+0

尝试使用到字符串化的有效载荷: 体:JSON.stringify({ 您的JSON对象 }) –

回答

1

因此,我刚刚意识到(经过几天的搜索)包react-native-fcm使用与admin.messaging().sendToDevice(token, payload, options)不同的发送方法。我已经使用了一段时间了,并没有意识到它实际上并不打算用于这个库或至少在这种情况下。主要是因为使用admin.messaging()直到我想要在所有应用程序状态中提醒通知,所有事情都运行良好。

另一种方法是这样

sendData(token) { 
    let body = { 
     "to": token, 
     "data":{ 
      "title": "Simple FCM Client", 
      "body": "This is a notification with only DATA.", 
      "sound": "default", 
      "click_action": "fcm.ACTION.HELLO", 
      "remote": true 
     }, 
     "priority": "normal" 
    } 

    this._send(JSON.stringify(body), "data"); 
    } 

    _send(body, type) { 
    let headers = new Headers({ 
     "Content-Type": "application/json", 
     "Content-Length": parseInt(body.length), 
     "Authorization": "key=" + FirebaseConstants.KEY 
    }); 

    fetch(API_URL, { method: "POST", headers, body }) 
     .then(response => console.log("Send " + type + " response", response)) 
     .catch(error => console.log("Error sending " + type, error)); 
    } 

可以使用这种方法的数据对象内使用嵌套的对象。不幸的是,这个文档并不是很清楚,直到现在我才意识到有一个例子。当然,这可能只是我。

1

当使用data消息负载,更说明若要使用字符串键值对,所以你可以做的是通过在" "括起来有你custom_notification为JSON字符串的值。

对于提供的样本有效负载,您实际上是否在to参数中使用FCM_TOKEN?你应该用一个实际的标记代替它。