【问题标题】:FCM - Only 'display' push notification when app is in backgroundFCM - 仅在应用程序处于后台时“显示”推送通知
【发布时间】: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


【解决方案1】:

如果你关注了firebase setup for xamarin android

你可能已经读到前台通知必须手动显示,

怎么做?

就是这样

在设置中,您将拥有一个继承自 FirebaseMessagingService 的类

在那个类中,会有一段类似this()的代码:

 var pendingIntent = PendingIntent.GetActivity(this,
                                              MainActivity.NOTIFICATION_ID,
                                              intent,
                                              PendingIntentFlags.OneShot);

var notificationBuilder = new  NotificationCompat.Builder(this, MainActivity.CHANNEL_ID)
                          .SetSmallIcon(Resource.Drawable.ic_stat_ic_notification)
                          .SetContentTitle("FCM Message")
                          .SetContentText(messageBody)
                          .SetAutoCancel(true)
                          .SetContentIntent(pendingIntent);

 var notificationManager = NotificationManagerCompat.From(this);
 notificationManager.Notify(MainActivity.NOTIFICATION_ID, 
 notificationBuilder.Build());

这段代码将通知发送到 Android 托盘,当在前台时,因此如果您对此进行评论,您将获得所需的输出。

祝你好运!

在查询恢复的情况下。

【讨论】:

  • 哇,成功了。谢谢@g.hakim
  • 当然没问题,请将答案标记为正确,以便其他人知道相同的问题!
猜你喜欢
  • 2022-12-22
  • 1970-01-01
  • 2018-08-04
  • 2017-06-25
  • 2020-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多