接收Firebase推送通知

时间:2018-11-20 16:27:31

标签: android firebase xamarin push-notification xamarin.android

在我的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来说我没有运气。我想念什么?

1 个答案:

答案 0 :(得分:0)

您必须在应用程序部分的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文件添加到您的项目中