【问题标题】:user to user notification message with ionic app using Frirebase cloud Messaging使用 Firebase 云消息传递的 ionic 应用程序的用户到用户通知消息
【发布时间】:2018-06-10 12:15:02
【问题描述】:

我正在尝试使用 Firebase 云消息从我的 ionic 应用发送通知。我按照以下步骤安装了 FCM 插件:

cordova plugin add cordova-plugin-fcm

npm install --save @ionic-native/fcm

到目前为止,一切正常。我正在使用 http plugin of ionic native 通过 FCM rest api 使用 post request 发送通知。所以我使用以下命令安装了 http 插件

cordova plugin add cordova-plugin-advanced-http

npm install --save @ionic-native/http

我在 app.modules.ts 中包含了 FCM 和 HTTP 模块 在我的一个控制器中,这是我发送发布请求的方式:

this.http.post('https://fcm.googleapis.com/fcm/send',
    {
        "notification": {
            "title": "Notification title",
            "body": "Notification body"
        },
        "data": {
            "key1": "value1",
            "key2": "value2"
        },
        "to": "/topics/all",
        "priority": "high"
    },
    {
        "Content-type": "application/json",
        "Authorization": "key=serverkey"
    }
).catch(error=>{
  alert(error.error);
})

我从 firebase 控制台获取了 serverkey 值。因此,当我发送 post 请求时,不会显示错误消息,但不会发送通知。

有什么帮助吗?谢谢。

【问题讨论】:

  • 我正在尝试完全相同的事情,但我确实收到一条错误消息,即:JSON_PARSING_ERROR: Unexpected character (d) at position 0.
  • @macduff 据我所知,使用 firebase 发送通知的最佳方式是使用 firebase 云功能。它比使用 http 插件和 FCM rest api 非常简单和高效。它是我最终使用的解决方案。并且通知发送正确。

标签: cordova ionic-framework push-notification http-post firebase-cloud-messaging


【解决方案1】:

尝试在您的帖子之前添加以下内容,以便将对象参数编码为 JSON

this.http.setDataSerializer("json")

这解决了我的问题(见评论)

【讨论】:

    猜你喜欢
    • 2021-05-10
    • 2021-07-22
    • 2020-11-07
    • 1970-01-01
    • 1970-01-01
    • 2016-10-05
    • 1970-01-01
    • 2023-03-15
    • 2017-02-07
    相关资源
    最近更新 更多