我已经在我的应用程序中成功实现了Firebase Cloud Messaging(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()方法,则该应用程序在终止并重新打开后不会崩溃。但是我确实需要该方法来获取通知详细信息,并在应用程序处于后台时导航到相应的活动。
请帮助
答案 0 :(得分:0)
我们不能肯定地说没有堆栈跟踪,但是我很确定它是因为您要从已经完成的活动中启动一个活动,还是因为您要启动两个活动。无论哪种方式,您调用逻辑都是错误的。
首先检查是否有其他用途。然后,如果有其他活动,请对其进行处理并启动适当的活动(就您的情况而言,NewsNotificationsActivity-BTW,您应该真正将此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的意图中拉出额外的值,而不是从其他活动中的静态变量中拉出。您可能还希望将Extra提取