Android 当应用程序使用FCM关闭时,如何处理通知?
当我的应用程序关闭时,我会收到来自FCM的新通知,但当我点击它时,我的应用程序将崩溃。当我的应用程序处于后台或前台时,一切正常,然后他们将打开正确的页面以获取通知。我不调用SendNotification,因为我不想在应用程序位于前台时使用本地通知 我的fcm服务:Android 当应用程序使用FCM关闭时,如何处理通知?,android,notifications,xamarin.android,xamarin.forms,firebase-cloud-messaging,Android,Notifications,Xamarin.android,Xamarin.forms,Firebase Cloud Messaging,当我的应用程序关闭时,我会收到来自FCM的新通知,但当我点击它时,我的应用程序将崩溃。当我的应用程序处于后台或前台时,一切正常,然后他们将打开正确的页面以获取通知。我不调用SendNotification,因为我不想在应用程序位于前台时使用本地通知 我的fcm服务: [Service] [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })] public class MyFirebaseMessagingService :
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
const string TAG = "MyFirebaseMsgService";
public override void OnMessageReceived(RemoteMessage message)
{
Log.Debug(TAG, "From: " + message.From);
MessagingCenter.Send(App.CurrentApp, "ReceivedNotification");
}
void SendNotification(string messageBody)
{
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
var notificationBuilder = new Android.App.Notification.Builder(this)
.SetSmallIcon(Resource.Drawable.ic_launcher)
.SetContentTitle("FCM Message")
.SetContentText(messageBody)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
var notificationManager = NotificationManager.FromContext(this);
notificationManager.Notify(0, notificationBuilder.Build());
}
}
OnCreate中的通知处理:
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
Forms.Init(this, bundle);
ImageCircleRenderer.Init();
IsPlayServicesAvailable();
if (Intent.Extras != null)
{
NotificationSendObject notifcation = new NotificationSendObject();
foreach (var key in Intent.Extras.KeySet())
{
switch (key)
{
case "object_id":
notifcation.objectId = Intent.Extras.GetString(key);
break;
case "notification_id":
notifcation.notificationId = Intent.Extras.GetString(key);
break;
case "object_type":
notifcation.objectType = Intent.Extras.GetString(key);
break;
}
}
if (notifcation.notificationId != null)
{
MessagingCenter.Send(App.CurrentApp, "OnTapNotification", notifcation);
}
}
SecureStorageImplementation.StoragePassword = "*****";
LoadApplication(new App());
}
我找到了一个解决方案,Intent.Extras必须在LoadApplication方法之后调用 固定的:
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
Forms.Init(this, bundle);
ImageCircleRenderer.Init();
IsPlayServicesAvailable();
SecureStorageImplementation.StoragePassword = "*****";
LoadApplication(new App());
if (Intent.Extras != null)
{
NotificationSendObject notifcation = new NotificationSendObject();
foreach (var key in Intent.Extras.KeySet())
{
switch (key)
{
case "object_id":
notifcation.objectId = Intent.Extras.GetString(key);
break;
case "notification_id":
notifcation.notificationId = Intent.Extras.GetString(key);
break;
case "object_type":
notifcation.objectType = Intent.Extras.GetString(key);
break;
}
}
if (notifcation.notificationId != null)
{
MessagingCenter.Send(App.CurrentApp, "OnTapNotification", notifcation);
}
}
}
将崩溃日志放在这里。OnCreate()是否使用大写O?你确定吗?嗨。请勿张贴stacktrace.:)@塞德里克·弗兰克是的,我确定,这是卡姆莱凯斯。没关系很抱歉没看到Xamarin标签