【发布时间】:2019-04-23 03:42:29
【问题描述】:
在我的 Android 应用程序中,我需要处理 Firebase 推送通知消息的接收,以便我可以操纵它们的 ID(以便显示所有通知而不是相互替换),以便我可以显示应用程序图标(将其设置为Android 清单中的元数据元素不起作用。)
我正在创建一个消息服务来处理接收消息:
[Service (Name = "com.rpr.mobile.droid.LocalyticsFirebaseMessagingService")]
[IntentFilter (new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class LocalyticsFirebaseMessagingService : FirebaseMessagingService {
private static int notificationId = 0;
public override void OnMessageReceived (RemoteMessage message) {
if (!message.Data.ContainsKey ("ll")) {
base.OnMessageReceived (message);
} else {
var body = message.GetNotification ().Body;
if (!String.IsNullOrEmpty (body)) {
var mainIntent = new Intent (this, typeof (IntentActivity));
var deepLinkUrl = "";
if (message.Data.TryGetValue ("ll_deep_link_url", out deepLinkUrl))
mainIntent.SetData (Android.Net.Uri.Parse (deepLinkUrl));
var launchIntent = PendingIntent.GetActivity (this, 1, mainIntent, PendingIntentFlags.UpdateCurrent);
var builder = new NotificationCompat.Builder (this)
.SetSmallIcon (Resource.Drawable.logo_blue_small)
.SetContentTitle (GetString (Resource.String.application_name))
.SetContentText (body)
.SetStyle (new NotificationCompat.BigTextStyle ().BigText (body))
.SetContentIntent (launchIntent)
.SetDefaults (-1)
.SetAutoCancel (true);
var notificationManager = NotificationManagerCompat.From (this);
notificationManager.Notify (notificationId++, builder.Build ());
}
}
}
}
我已经在我的 Android 清单的应用程序部分定义了它:
<service android:name="com.rpr.mobile.droid.LocalyticsFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
但是,当我收到推送通知时,即使应用程序在前台,也不会调用此代码。我有类似的代码可以很好地用于 GCM 推送通知,但我对 Firebase 没有运气。我错过了什么?
【问题讨论】:
标签: android firebase xamarin push-notification xamarin.android