【问题标题】:How to handle notifications when app is closed with FCM?使用 FCM 关闭应用程序时如何处理通知?
【发布时间】:2017-07-17 19:23:35
【问题描述】:

当我的应用关闭时,我会收到来自 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());

    }

【问题讨论】:

  • 把你的崩溃日志放在这里。
  • OnCreate() 与大写 O?你确定吗?
  • 嗨。发布堆栈跟踪。 :)
  • @CedricFranck 是的,我确定,它是 C# CamleCase。没关系。
  • 抱歉没有看到 Xamarin 标签

标签: android notifications xamarin.android xamarin.forms firebase-cloud-messaging


【解决方案1】:

我找到了解决办法,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);
        }
    }

}

【讨论】:

    猜你喜欢
    • 2018-07-31
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 2019-02-19
    • 1970-01-01
    • 1970-01-01
    • 2017-04-11
    • 2019-06-19
    相关资源
    最近更新 更多