gpt4 book ai didi

android - 使用 FCM 关闭应用程序时如何处理通知?

转载 作者:行者123 更新时间:2023-11-30 00:41:38 50 4
gpt4 key购买 nike

当我的应用关闭时,我会收到来自 FCM 的新通知,但当我点击它时,我的应用会崩溃。当我的应用程序处于后台或前台时没关系,然后他们将打开正确的通知页面。我不调用 SendNotification,因为我不想在应用程序处于前台时收到本地通知。

我的 fcm 服务:

[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);
}
}

}

关于android - 使用 FCM 关闭应用程序时如何处理通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42481374/

50 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com