【问题标题】:RemoteMessage.GetNotification() returns null when request is not from Firebase Console?当请求不是来自 Firebase 控制台时,RemoteMessage.GetNotification() 返回 null?
【发布时间】:2017-01-31 00:42:42
【问题描述】:

我正在测试 Firebase 云消息传递。

如果我使用 Firebase 控制台发送消息 - 在我的代码中设置了值的通知对象 - 没问题:

public override void OnMessageReceived(RemoteMessage message)
    {
        base.OnMessageReceived(message);

        Firebase.Messaging.RemoteMessage.Notification oNotification;

        oNotification = message.GetNotification();

我正在使用http://requestmaker.com/ 发送通知 - 一切正常 - 它被发送并且我取回了 messageID。

POST /fcm/send HTTP/1.1
Host: fcm.googleapis.com
Accept: */*
Authorization: key=not-a-real-key0omClYhniZaIAercMVzeFHEG508USi8lud9pxC-SzxAAbR2mflAfVNsfrbrsPJxoFYTr15ytRn9aqWSQXm5x00AOwu2Wl6mWwTcm9l6G
Content-Length: 188
Content-Type: application/x-www-form-urlencoded

但是 oNotification = message.GetNotification(); 返回 null。 问题是 - 为什么?

【问题讨论】:

  • 嗨。您可能会发送data 有效负载而不是notification 有效负载。使用 Firebase 控制台时,发送的消息始终被视为 notification 有效负载消息。您可以将您发送给 requestmaker 的 请求数据 发布出来吗?
  • 请求URL:fcm.googleapis.com/fcm/send - 授权密钥= AAAAAXu6AHE:APA9 ....请求数据registration_id = eMM1bnJVxT8:APA91bEiVop08dRKLTSF9LdW4mGql8EFw9CwXjLqdAqxs7D8A_GF_D6DU7QPyy5JahzZoQVraJmxTQiV0UTUet_hbr1G_9xNv87Vpu4yCgN751hVF07hdRx-hlJRcfUEC82IlvaV-9AD 跨度>
  • @AL。这是一个好点! firebase.google.com/docs/cloud-messaging/…
  • @AL。请随时发布它作为答案!这个链接确实描述得最好——但这里要提的一件快速的事情是——如果你没有设置通知对象(无论你是使用 Json 还是纯 HTTP 请求)——你不能指望有一个对象存在.这就是为什么我在尝试访问它时看到 null 的原因。
  • 嗨@AlexeyFShevelyov。我在答案中添加了。随意添加一些您认为有用的细节。干杯! :)

标签: android firebase xamarin.android httprequest firebase-cloud-messaging


【解决方案1】:

您似乎发送的是data 有效负载,而不是notification 有效负载。使用 Firebase 控制台时,发送的消息始终被视为 notification 有效负载消息(或者如果您在高级选项中添加了自定义键值对,则两者的组合)。见FCM Message Types

在使用 requestmaker 时,您可能只发送了 data-only 有效负载。在您的代码中,您只处理具有notification 有效负载类型的消息:

message.GetNotification();

我并不完全熟悉 Android Xamarin,但通常使用 FCM,如果您决定发送 data-only 有效负载,还有一种方法可以通过调用 RemoteMessage.getData() 来获取 data 有效负载。

【讨论】:

  • 我们在 Vero 上遇到了同样的问题,它的默认模板使用 data 对象而不是 notification
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-25
  • 1970-01-01
  • 2016-04-16
相关资源
最近更新 更多