【问题标题】:Cordova Android app do not receive FCM notifications from server sideCordova Android 应用程序不接收来自服务器端的 FCM 通知
【发布时间】:2017-04-07 10:46:57
【问题描述】:

我遇到了 Cordova Android 应用程序问题,我尝试通过适用于 Cordova 的 Firebase 插件接收 FCM 通知,每次我通过 Firebase 控制台发送通知时,设备都会收到消息,但是当我尝试使用 Firebase API 时我的服务器端,它说它已发送通知(状态 200),但设备没有收到任何通知。

即使我尝试通过 Postman 测试发送通知,也会发生同样的情况,出现发送状态 200,但设备上没有消息。

我的服务器端是基于 .NET 构建的。请,如果有人可以帮助我,我会很感激。

Public Function SendNotificationToFirebaseCloud() As [String]
    Dim result = "-1"
    Dim webAddr = "https://fcm.googleapis.com/fcm/send"

    Dim httpWebRequest = DirectCast(WebRequest.Create(webAddr), HttpWebRequest)
    httpWebRequest.ContentType = "application/json"
    httpWebRequest.Headers.Add("Authorization:key=MY_AUTHORIZATION_KEY")
    httpWebRequest.Method = "POST"

    Using streamWriter = New StreamWriter(httpWebRequest.GetRequestStream())
        Dim json As String = "{""to"" : ""MY_FIREBASE_TOKEN"", ""priority"": ""high"", ""data"" : { ""name"" : ""VIP"", ""othername"" : ""Leiloes"" }}"
        streamWriter.Write(json)
        streamWriter.Flush()
    End Using

    Dim httpResponse = DirectCast(httpWebRequest.GetResponse(), HttpWebResponse)
    Using streamReader = New StreamReader(httpResponse.GetResponseStream())
        result = streamReader.ReadToEnd()
    End Using

    Return result
End Function

【问题讨论】:

  • 你找到解决办法了吗?

标签: android asp.net cordova firebase firebase-cloud-messaging


【解决方案1】:

您的 JSON 中应该有一个“通知”字段,因为结构需要:

{
    "to" : "xxxx", 
    "priority" : "high", 
    "notification" : {
        "title" : "Your title",
        "body" : "Your body",
        "click_action" : "FCM_PLUGIN_ACTIVITY", //(This one is important as it's used to trigger the event when you click on the notification)
        "icon" : "myicon",
        "sound" : "default"
    },
    "data" : {
        "name" : "VIP", 
        "othername" : "Leiloes"
    }
}

【讨论】:

  • 我尝试了@NicolasAlric 的解决方案,但情况仍然相同。
  • 尝试将"title" : "Your title", "body" : "Your body"添加到您的数据字段
  • 仍然无法正常工作。我认为 json 格式是正确的,但是,将消息从 Firebase 发送到设备时会出现问题。 post请求的状态是200,在firebase之前一切正常,但是消息没有传递。
  • 您可以尝试使用主题来完成这项工作,'to' => "/topics/yourtopic" 在您的 JSON 中 FCMPlugin.subscribeToTopic("yourtopic"'); 在您的应用代码中
猜你喜欢
  • 2019-01-18
  • 2017-04-16
  • 2022-11-18
  • 2017-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多