如何处理Firebase通知,即Android中的通知消息和数据消息

问题描述 投票:0回答:1

使用Xamarin Android处理firebase中的通知消息和数据消息的最佳方法是什么,而用户处于Foreground和Background?

另外,如何获取通知数据,例如特定通知的文本?

PS:我访问了以下主题并且没有人真正帮助过:

When device screen off then how to handle firebase notification?

Firebase Notification and Data

Display firebase notification data-message on Android tray

firebase xamarin xamarin.android firebase-cloud-messaging
1个回答
2
投票

好吧,我找到了自己问题的答案,所以我发布了一个寻找xamarin中firebase集成的人的答案。

  • Xamarin.Firebase.Messaging软件包安装到您的项目中。
  • 将以下代码添加到manifest.xml以接收firebase通知。 <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>
  • 现在从firebase获取注册令牌添加一个类文件并向其添加以下代码: [Service] [IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })] public class MyFirebaseIIDService : FirebaseInstanceIdService { public override void OnTokenRefresh() { const string TAG = "MyFirebaseIIDService"; var refreshedToken = FirebaseInstanceId.Instance.Token; Log.Debug(TAG, "Refreshed token: " + refreshedToken); SendRegistrationToServer(refreshedToken); } void SendRegistrationToServer(string token) { // Add custom implementation, as needed. } } 这里FirebaseInstanceId.Instance.Token获取当前设备的实例令牌。此外,方法SendRegistrationToServercan用于发送令牌以将令牌发送到服务器。
  • 现在添加另一个类来处理前台的通知 [Service] [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })] public class MyFirebaseMessagingService : FirebaseMessagingService { // private string TAG = "MyFirebaseMsgService"; public override void OnMessageReceived(RemoteMessage message) { base.OnMessageReceived(message); string messageFrom = message.From; string getMessageBody = message.GetNotification().Body; SendNotification(message.GetNotification().Body); } void SendNotification(string messageBody) { try { var intent = new Intent(this, typeof(MainActivity)); intent.AddFlags(ActivityFlags.ClearTop); var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .SetSmallIcon(Resource.Drawable.ic_stat_ic_notification) .SetContentTitle("Title") .SetContentText(messageBody) .SetAutoCancel(true) .SetContentIntent(pendingIntent); NotificationManagerCompat notificationManager = NotificationManagerCompat.From(this); notificationManager.Notify(0, notificationBuilder.Build()); } catch (Exception ex) { } } }

这里使用方法SendNotification显式向系统托盘发送通知,因为设备在前台时的推送通知不会自动显示在系统托盘中。

  • 当设备处于后台或自动生成死亡通知时,默认情况下会加载主启动器活动,要从后台通知中获取数据,您需要按如下方式使用意图(在主启动器活动上): if (Intent.Extras != null) { foreach (var key in Intent.Extras.KeySet()) { var value = Intent.Extras.GetString(key); Log.Debug(TAG, "Key: {0} Value: {1}", key, value); } }
  • 此外,如果Google Play服务不是最新的,此代码可能会使您的应用程序崩溃,以便检查Google Play服务是否可用,请执行以下操作: public bool IsPlayServicesAvailable() { int resultCode = GoogleApiAvailability.Instance.IsGooglePlayServicesAvailable(this); if (resultCode != ConnectionResult.Success) { if (GoogleApiAvailability.Instance.IsUserResolvableError(resultCode)) msgText.Text = GoogleApiAvailability.Instance.GetErrorString(resultCode); else { msgText.Text = "This device is not supported"; Finish(); } return false; } else { msgText.Text = "Google Play Services is available."; return true; } }

有关如何将项目添加到firebase控制台的信息,请查看以下链接:

https://developer.xamarin.com/guides/android/data-and-cloud-services/google-messaging/remote-notifications-with-fcm/ \

更新

  • 在Android Oreo最近更改后,您必须将通知添加到频道中,以便在MainActivity中创建通知频道,如下所示: void CreateNotificationChannel() { if (Build.VERSION.SdkInt < BuildVersionCodes.O) { // Notification channels are new in API 26 (and not a part of the // support library). There is no need to create a notification // channel on older versions of Android. return; } var channel = new NotificationChannel(CHANNEL_ID, "FCM Notifications", NotificationImportance.Default) { Description = "Firebase Cloud Messages appear in this channel" }; var notificationManager = (NotificationManager) GetSystemService(NotificationService); notificationManager.CreateNotificationChannel(channel); }

在MainActivity的OnCreate方法中调用此方法。

© www.soinside.com 2019 - 2024. All rights reserved.