繁体   English   中英

接收Firebase推送通知

[英]Receive Firebase push notification

在我的Android应用中,我需要处理Firebase推送通知消息的接收,以便我可以操纵它们的ID(以便显示所有通知,而不是彼此替换),并可以显示应用图标(使用meta- Android清单中的data元素无效。)

我已经创建了一个消息传递服务来处理接收消息:

[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清单文件中添加以下代码:

<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文件添加到您的项目中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM