Xamarin Android应用程序与FCM崩溃后再次杀死和重新打开

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

我已在我的应用中成功实施了Firebase云消息传递(FCM)推送通知。该应用程序能够在它处于前台时接收通知并导航到相应的活动。然而,当我杀了应用程序并尝试再次打开应用程序崩溃。

这是我的第一个活动代码,即Splash活动:

  protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        Thread.Sleep(3000);
        Intent intent;

        if (string.IsNullOrEmpty(oStaticVariables.MembershipID))
        {
            intent = new Intent(this, typeof(LoginView));
        } 
        else
        {
            oStaticVariables.NewsListPreviousPosition = "";
            oStaticVariables.PTRShown = false;
            oStaticVariables.UpdateMsgShown = false;
            intent = new Intent(this, typeof(MainActivity));
        }

            StartActivity(intent);
            Finish();
        CheckForBackgroundFCMNotifications();

    }


   *Fired when app is in background and the app receives notification*
    private void CheckForBackgroundFCMNotifications()
    {
        if (Intent.Extras != null)
        {
            foreach (var key in Intent.Extras.KeySet())
            {
                var value = Intent.Extras.GetString(key);
                //Log.Debug("", "Key: {0} Value: {1}", key, value);
                if (key == "NotifyId")
                {
                    oStaticVariables.GCMID = value;
                }
                if (key == "Header")
                {
                    oStaticVariables.GCMSubject = value;
                }
            }

            Intent nextActivity = new Intent(this, typeof(NewsNotifications));
            StartActivity(nextActivity);

        }

    }

如果我删除了CheckForBackgroundFCMNotifications()方法,则应用程序在终止并重新打开后不会崩溃。但我确实需要该方法来获取通知详细信息并在应用程序处于后台时导航到相应的活动。

请帮忙

c# android xamarin xamarin.android firebase-cloud-messaging
1个回答
0
投票

没有堆栈跟踪,我们无法肯定地说,但我很确定它要么是因为你从已经完成的活动启动活动,要么是因为你正在启动两个活动。无论哪种方式,你调用逻辑都是错误的。

首先检查额外的意图。然后,如果有额外的,处理它们并开始相应的活动(在你的情况下NewsNotifications - 顺便说一下你应该真的称这个NewsNotificationsActivity为命名一致性),如果没有启动正常活动(在你的情况下为MainActivity)。

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);    
        if (Intent.Extras == null)
        {   
            if (string.IsNullOrEmpty(oStaticVariables.MembershipID))
            {
                var intent = new Intent(this, typeof(LoginView));
                StartActivity(intent);
            } 
            else
            {
                intent.PutExtra("NewsListPreviousPosition", "");
                intent.PutExtra("PTRShown", false);
                intent.PutExtra("UpdateMsgShown", false);
                var intent = new Intent(this, typeof(MainActivity));
                StartActivity(intent);
            }
            Finish();
        }
        else
        {
            CheckForBackgroundFCMNotifications();
        } 
    }

   // Called when app is in background and the app receives notification
    private void CheckForBackgroundFCMNotifications()
    {
         Intent nextActivity = new Intent(this, typeof(NewsNotificationsActivity));
         foreach (var key in Intent.Extras.KeySet())
         {
                try  // just in case the value is not a string
                {
                    var value = Intent.Extras.GetString(key);
                    //Log.Debug("", "Key: {0} Value: {1}", key, value);
                    if (key == "NotifyId")
                        nextActivity.PutExtra("NotifyId", value);
                    if (key == "Header")
                        nextActivity.PutExtra("Header", value);
                catch {}
        }
        StartActivity(nextActivity);
    }

然后从MainActivity和NewsNotificationsActivity中的intent中取出额外的值,而不是从不同活动中的静态变量中取出。您可能还想要进行额外提取

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