【发布时间】:2019-02-25 11:16:18
【问题描述】:
目前,我在 Xamarin 应用程序中发送和接收推送通知。当我发送推送通知时,当应用程序位于前台和后台时,会显示潜行高峰对话。当应用程序在后台时,我试图仅视觉显示推送通知。当用户在前台时,通知只是潜入 HUD 而不会显示。
这是我的代码:
Xamarin Android
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class FCMMessagingService : FirebaseMessagingService
{
const string TAG = "MyFirebaseMsgService";
public override void OnMessageReceived(RemoteMessage message)
{
try
{
var _message = message.GetNotification().Body;
var _title = message.GetNotification().Title;
MainActivity.SendNotification(_title, _message);
}
catch (Exception e)
{
//Was NotificationType null? this should never be null anyways
}
}
}
API - Android 负载
return new JObject(
new JObject(
new JProperty("notification",
new JObject(
new JProperty("body", message),
new JProperty("title", title),
new JProperty("icon","toasticon"),
new JProperty("user", fromId != null ? fromId.ToString() : "")
)
),
new JProperty("data",
new JObject(
new JProperty("notificationtype", notificationType)
)
)
)
).ToString();
【问题讨论】:
-
从您的有效负载中仅发送
data密钥而不是notification。 -
@Arvindraja 你赢得了鸡肉晚餐!这就是我一直在寻找的答案。如果您可以将您的评论重新发布为答案,我可以奖励您获胜。作为额外的奖励,您能否解释我应该如何根据显示/隐藏通知检测用户是在前台还是后台的最佳方法?我可以用 SignalR 做到这一点,但我想知道 FCM 是否已经具备一些能力。
-
@XamDev89-您可以提出单独的问题,以获取更好的其他说明。 :)
标签: firebase xamarin xamarin.android firebase-cloud-messaging