【问题标题】:Xamarin Android OnNewIntent Not Called by Closed App关闭的应用程序未调用 Xamarin Android OnNewIntent
【发布时间】:2017-05-17 18:43:37
【问题描述】:

什么是有效的

我已经使用以下 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);
}

有什么问题

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

已经尝试过什么

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

  1. Android OnNewIntent not called

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

Intent intent = new Intent(this, typeof(MainActivity));
Bundle data = intent.Extras;
  1. onNewIntent is not called

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

编辑

总是会收到推送通知,并且总是会调用 OnMessageReceived,无论应用程序是在后台还是完全关闭。此外,当应用程序处于后台时,OnNewIntent 会收到待处理的 Intent,而在应用程序关闭时则不会。

this.Intent.Extras;

总是显示为 null(当前位于 OnResume 方法中)。

【问题讨论】:

    标签: android-intent xamarin xamarin.android


    【解决方案1】:

    在 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
    

    在您的服务中您的通知到达onMessageReceived 的情况下,您需要自己处理传入的通知,并自行决定是要呈现通知还是执行其他操作。

    听起来您正在发送定期通知并准备PendingIntent,当您按下它时触发OnNewIntent

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

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

    【讨论】:

    • 感谢您的回复。我尝试使用 this.Intent.Extras 并相应地编辑了我的问题。似乎 this.Intent 的值总是被覆盖,尽管选择了带有待处理意图的通知,可能是由于我的活动或待处理意图标志的某种组合?
    • 经过测试,SingleTop 似乎调用了 OnNewIntent,但未能为 MainActivity.Intent 设置值,SingleTop 提供了应用程序已打开时所需的功能。因此,我能找到的唯一解决方法是删除 OnNewIntent,在 OnMessageReceived 中,我在 MainActivity 中设置了一个静态变量来包含意图的值。因此,无论应用程序是关闭还是在后台,意图都可用于 OnResume 方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-28
    • 1970-01-01
    • 1970-01-01
    • 2015-01-23
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多