应用程序终止后,点击通知无法打开特定视图

11

我正在使用Plugin.FirebasePushNotificationXamarin.Forms中开发推送通知。当应用程序被终止(从任务栏中关闭)时,通知无法打开特定视图。

当应用程序处于打开或后台状态时,单击通知时可以导航到特定页面。 这是应用程序类。

public class AppClass : Android.App.Application, Android.App.Application.IActivityLifecycleCallbacks
{
    public override void OnCreate()
    {
        base.OnCreate();
        RegisterActivityLifecycleCallbacks(this);
        FirebasePushNotificationManager.Initialize(this,false,true);
        var instanceid = FirebaseInstanceId.Instance.Token;
    }
}

MainActivity类

protected override void OnCreate(Bundle savedInstanceState)
{
    TabLayoutResource = Resource.Layout.Tabbar;
    ToolbarResource = Resource.Layout.Toolbar;
    base.OnCreate(savedInstanceState);
    global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
    LoadApplication(new App());
}

protected override void OnNewIntent(Intent intent)
{
    base.OnNewIntent(intent);
    FirebasePushNotificationManager.ProcessIntent(this, intent);
}

共享项目中的App.xaml.cs类

protected override void OnStart()
{
    CrossFirebasePushNotification.Current.OnNotificationOpened += (s, p) =>
    {
        if (p.Data.ContainsKey("color"))
        {
        Device.BeginInvokeOnMainThread(() =>
        {
            Xamarin.Forms.Application.Current.MainPage.Navigation.PushModalAsync(new Page1()
            {
                BackgroundColor = Color.FromHex($"{p.Data["color"]}")
            });
        });
        }
    };
}

这是我从Postman发送的有效载荷。

{ 
 "to":"dhNe4U_jSDo:APA91bHhupDfnqW5Y9EBIfWV-uQVmq_HyPXTzBZ_TnSfM-7cDEq8SUFDLeudA4vWzyMj28E8A_c5HS0F2f5xT8quWZHcmWL8RJEbeDNre3RMsuY7brcAxqQMQOMCDcVXWDsl7s15l-NC", 
 "notification" : {
 "body" : "New announcement assigned",
 "content_available" : true,
 "priority" : "max",
 "color":"Page1",
 "content_available" : true,
 "title": "notification TITLE",
 "content_available" : true,
 "body": "notification BODY",
 },
 "data" : {
 "OrganizationId":"2",
 "color":"Page1",
 "click_action":"MainActivity"
 "sectionId":"33",
 "priority" : "high",
 "title": "notification TITLE",
 "content_available" : true,
 "body": "notification BODY",
}
}

这是我的清单类

<application android:label="FCMPush.Android">
    <uses-permission android:name="android.permission.INTERNET" />
    <receiver
      android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" 
      android:exported="false" />
    <receiver
          android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver"
          android:exported="true"
          android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
          <action android:name="com.google.android.c2dm.intent.RECEIVE" />
          <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
          <category android:name="${applicationId}" />
        </intent-filter>      
    </receiver>
</application>

我也在GitHub上将这个问题分配了。谢谢。


1
“当应用在前台(从任务栏中关闭)”是什么意思?应用在前台表示用户正在使用该应用,而不是从任务栏中关闭。 - nevermore
哦!我以为当应用程序完全关闭时,它就在前台。这种状态叫做后台吗?谢谢。 - R15
基本上,后台意味着应用程序仍在后台运行。当应用程序从任务栏中被终止时,您可以称其状态为“终止”。 - nevermore
1
看起来您误用了 click_action。1. 它需要在 notification 下面。2. 根据文档 "如果指定,当用户点击通知时,将启动具有匹配意图过滤器的活动。" - AL.
看起来是这样。我不完全确定,但我记得click_action中的值有一个特定的格式。我已经有一段时间没有测试过了。你能否尝试添加活动的确切位置(例如com.sample.Activity)--就像我在这里的答案中所做的那样? - AL.
显示剩余4条评论
3个回答

1

嗯,我不确定问题可能出在哪里。然而,如果我们查看文档:https://github.com/CrossGeeks/FirebasePushNotificationPlugin/blob/master/docs/GettingStarted.md,有几件事情可以尝试。

首先,在你的MainActivity中设置Exported = trueLaunchMode = LaunchMode.SingleTop还要在你的onCreate()中,在LoadApplication(new App())之后设置FirebasePushNotificationManager.ProcessIntent(this, Intent)

请注意,从Android 8.0开始,您还必须在应用程序的onCreate()中设置DefaultNotificationChannelIdDefaultNotificationChannelName

然后,如果您有一个SplashActivity,请确保将MainLauncher = true, NoHistory = true添加到其中,并在onCreate()中添加以下代码:

var mainIntent = new Intent(Application.Context, typeof(MainActivity));

 if (Intent.Extras != null) {
     mainIntent.PutExtras(Intent.Extras);
 }
 mainIntent.SetFlags(ActivityFlags.SingleTop);
 StartActivity(mainIntent);

我建议您查看存储库中以下文件夹:https://github.com/CrossGeeks/FirebasePushNotificationPlugin/tree/master/samples/FirebasePushSample/FirebasePushSample.Android。如果您仔细遵循所有代码,它应该可以工作。

我之前缺少了FirebasePushNotificationManager.ProcessIntent(this, Intent);,但现在它已经可以正常工作了。你指出了几个问题,让我更加关注细节。幸运的是你得到了奖励 :)。 - R15
很高兴听到这个 :) - gi097
我的情况是我将 enableDelayedResponse 设置为 false,FirebasePushNotificationManager.ProcessIntent(this, Intent, enableDelayedResponse:false);,通过将 enableDelayedResponse 设置回 true(默认值)来解决问题。 - BladeMaster

0
如果您在主活动之前有闪屏活动,则在恢复时将推送信息发送到主活动,示例如下:
protected override void OnResume()
    {
        base.OnResume();
        Task startupWork = new Task(() => { SimulateStartup(); });
        startupWork.Start();
    }

    // Simulates background work that happens behind the splash screen
    async void SimulateStartup()
    {

        var intent = new Intent(this, typeof(MainActivity));
        if (Intent.Extras != null)
        {
            intent.PutExtras(Intent.Extras); // copy push info from splash to main
        }
        StartActivity(intent);
    }

在主活动的新意图方法中,您可以获取详细信息。示例如下:
protected override void OnNewIntent(Intent intent)
    {
        base.OnNewIntent(intent);

        var type = intent.GetStringExtra("type");
        if ((!string.IsNullOrEmpty(type) && object.Equals(type, "notification")))
        {
            // do your logic

        }
    }

0

当应用程序被杀死或不在后台时,OnNewIntent方法不会被调用。仅当现有屏幕在后台(即已存在的意图在后台)并且您触发新意图以将该屏幕前置时,才会调用它。(我可能没有使用正确的技术词汇,只是尝试解释概念)

在您的情况下,由于应用程序不在后台,因此将调用OnCreate,因此您需要从那里处理它,

尝试将进程意图调用移动到oncreate中的LoadApplication方法之下。
例如:

LoadApplication(new App());
FirebasePushNotificationManager.ProcessIntent(this,Intent)

-- 或者 --

我建议在OnCreate方法中解析意图,并通过构造函数将必要的信息作为参数传递给APP类,每当单击通知时都这样做。

例如:LoadApplication(new App('您想要的重要数据'));

然后从App类中,您可以导航用户到任何您想要的地方,而不会干扰应用程序的后退堆栈。


检查编辑后的答案,让我知道它是否有效。目前我无法编写示例代码。 - Suraj Jadhav

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接