【问题标题】:FCM iOS: Push notifications throw invalid argumentFCM iOS:推送通知抛出无效参数
【发布时间】:2018-10-31 04:02:36
【问题描述】:

我正在尝试使用 Cloud Functions 和 FCM for iOS 实现 pushNotifications,但我一直抛出此错误:

2018-05-21T13:04:00.087Z 我发送推送通知:发送错误 消息:{ 错误:请求包含无效参数。 在 FirebaseMessagingError.Error (本机) 在 FirebaseMessagingError.FirebaseError [作为构造函数] (/user_code/node_modules/firebase-admin/lib/utils/error.js:39:28) 在 FirebaseMessagingError.PrefixedFirebaseError [作为构造函数] (/user_code/node_modules/firebase-admin/lib/utils/error.js:85:28) 在新的 FirebaseMessagingError (/user_code/node_modules/firebase-admin/lib/utils/error.js:241:16) 在 Function.FirebaseMessagingError.fromServerError (/user_code/node_modules/firebase-admin/lib/utils/error.js:271:16) 在 /user_code/node_modules/firebase-admin/lib/messaging/messaging-api-request.js:149:50 在 process._tickDomainCallback (internal/process/next_tick.js:135:7) errorInfo: { 代码: '消息/无效参数', 消息:“请求包含无效参数。” }, codePrefix: '消息' }

我在云函数中的实现如下:

exports.sendPushNotifications = functions.database.ref('/conversations/{userUid}/').onWrite((snap, context) => {
    const userUid = context.params.userUid

    console.log("Triggered user ", userUid)

    return admin.database().ref('/fcmToken/' + userUid).once('value', snapshot => {
        const values = snapshot.val()
        const fcmToken = values.fcmToken

        var message = {
            "token": fcmToken,

            "notification": {
                "body": "New message"
            },
            "apns": {
                "headers": {
                    "apns-priority": "5"
                },
                "payload": {
                    "aps": {
                        "alert": {
                            "body": "New message"
                        },
                        "badge": "1",
                        "sound": "default"
                    }
                }
            }
        };

        return admin.messaging().send(message)
          .then((response) => {
            return console.log('Successfully sent message:', response);
          })
          .catch((error) => {
            return console.log('Error sending message:', error);
          });
    })
})

令人沮丧的是,当我删除整个"apns" 节点时,代码实际上可以工作,即我可以接收推送通知。我想这意味着我的设置都已正确完成。一旦我包含了"apns",它就会开始抛出上述错误。我还参考了这三个帖子,thisthisthis,并确保我已仔细遵循代码和说明。由于某些原因,我无法让它工作。

我还尝试删除 "notification" 节点,因为文档确实提到在针对所有平台时只使用公共键。由于我现在只针对 iOS,我想我应该删除 "notification" 键。但同样它也会引发同样的错误。

【问题讨论】:

    标签: node.js firebase firebase-cloud-messaging


    【解决方案1】:

    好的,这是一个新手错误。如果我只针对 iOS,不应该使用公共键是正确的。除此之外,徽章应该是Int 而不是String

    此代码有效:

    var message = {
        "token": fcmToken,
    
        "apns": {
            "headers": {
                "apns-priority": "5"
            },
            "payload": {
                "aps": {
                    "alert": {
                        "body": "New message"
                    },
                    "badge": 1,
                    "sound": "default"
                }
            }
        }
    }
    

    希望对遇到同样问题的人有所帮助。

    【讨论】:

    • 你在开玩笑,apns 有效负载对象有一个 aps 对象。感谢@Koh,终于得到了正确的结构:)
    【解决方案2】:

    只是为了补充这个答案。如果您同时使用 IOS 和 android 并为两者自定义声音,则下面的代码将跨平台工作并避免此问题。

     const payload = {
                    token,
                    notification: {
                      title: `title text`,
                      body: `body text`,
                    },
                    android: {
                      // android
                      priority: "high", // legacy HTTP protocol (this can also be set to 10)
                      notification: {
                        channel_id: "call1",
                        priority: "high", // HTTP v1 protocol
                        notification_priority: "PRIORITY_MAX",
                        sound: "sound",
                        default_sound: true,
                        visibility: "PUBLIC",
                      },
                    },
                    apns: {
                      // apple
                      headers: { "apns-priority": "10" },
                      payload: {
                        aps: {
                          // If present with notification: {...}, this will create errors
                          // "alert": {
                          //   "title": `title text`,
                          //   "body": `body text`,
                          // },
                          badge: 1,
                          sound: {
                            critical: 1,
                            name: "sound.aiff",
                            volume: 1,
                          },
                          category: "call1",
                        },
                      },
                    },
    

    【讨论】:

      猜你喜欢
      • 2021-07-07
      • 1970-01-01
      • 1970-01-01
      • 2018-04-22
      • 2017-06-13
      • 1970-01-01
      • 2017-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多