【问题标题】:Receive Firebase push notification接收 Firebase 推送通知
【发布时间】: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


    【解决方案1】:

    您必须将以下代码添加到应用程序部分内的 android 清单文件中:

    <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false" />
        <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="${applicationId}" />
            </intent-filter>
        </receiver>
    

    在你的课堂上你必须像这样添加

    [Service]
    [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
    public class MessagingService : FirebaseMessagingService
    

    这是获取令牌的代码

    [Service]
    [IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
    public class IDService : FirebaseInstanceIdService
    

    您还必须使用构建操作 GoogleServicesJson 将 google-services.json 文件添加到您的项目中

    【讨论】:

      猜你喜欢
      • 2021-11-10
      • 1970-01-01
      • 1970-01-01
      • 2016-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-25
      • 1970-01-01
      相关资源
      最近更新 更多