关闭的应用程序未调用 Xamarin Android OnNewIntent

Jar*_*d L 7 android-intent xamarin.android xamarin

什么是工作

我已经使用以下 Xamarin 指南为 Xamarin Forms 应用程序的 Android 端实现了推送通知:

https://developer.xamarin.com/guides/android/application_fundamentals/notifications/remote-notifications-with-gcm/

这很有效,我们正确地在应用程序运行时和在后台调用 OnNewIntent。然后我们调用我们的方法来处理推送通知的意图,一切都很好。

protected override void OnNewIntent(Intent intent)
{
    base.OnNewIntent(intent);
    GoogleListenerService.ReceivedRemoteNotification(intent);
}
Run Code Online (Sandbox Code Playgroud)

问题是什么

如果我们关闭应用程序,触发推送通知,然后单击推送通知,应用程序似乎不会调用 OnNewIntent,而是会像单击应用程序图标(调用 OnCreate)一样启动应用程序。

尝试了什么

我尝试实施的主要解决方法概述如下:

  1. 未调用 Android OnNewIntent

Xamarin 只有一个 GetIntent 方法,需要字符串 URL 输入,并使用以下结果在空数据中。

Intent intent = new Intent(this, typeof(MainActivity));
Bundle data = intent.Extras;
Run Code Online (Sandbox Code Playgroud)
  1. 未调用 onNewIntent

在第二篇文章中,我们的意图服务、MainActivity.cs 文件和清单文件中 LaunchMode = SingleTop 的所有更改都没有导致行为发生任何变化。

编辑

推送通知总是被接收并且 OnMessageReceived 总是被调用,无论应用程序是在后台还是完全关闭。此外, OnNewIntent 在应用程序处于后台时接收挂起的意图,而不是在它关闭时。

this.Intent.Extras;
Run Code Online (Sandbox Code Playgroud)

始终显示为 null(当前位于 OnResume 方法中)。

Che*_*ron 4

在 Android 上接收通知时,无论您是接收通知还是数据通知以及应用程序的行为方式,都会有所不同。

查看云消息传递文档中的表格:https ://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages

App State  |    Notification     |        Data        |    Both
-------------------------------------------------------------------------------------
Foreground |  onMessageReceived  |  onMessageReceived | onMessageReceived
Background |  System tray        |  onMessageReceived | Notification: system tray
                                                        Data: in extras of the intent
Run Code Online (Sandbox Code Playgroud)

如果您的通知到达onMessageReceived您的服务,您需要自己处理传入的通知,并自行决定是否要呈现通知或执行其他操作。

由此看来,您正在发送定期通知并准备PendingIntent,这就是当您按下它时触发 的内容OnNewIntent

当应用程序在后台运行并且您收到定期通知时,应用程序应该定期启动。如果您已将数据附加到通知中,您应该能够从Intent Extras酒店获取它。

所以你在这里看到的是正确的行为。