Android Xamarin 如何使推送通知不创建新活动而是使用当前活动?

5
我们目前正在为iOS、Android和WP8开发一个Xamarin Forms移动应用程序,我正在处理Android的通知部分。
现在我能够接收通知并将其显示给用户,当他们点击通知时,它会带他们进入应用程序,但它不像我们想要的那样工作。它不是在同一个Activity中继续,而是启动了一个全新的Activity,失去了实际应用程序的整个上下文。
在我们的推送通知接收器中,我们重写了OnMessage方法,该方法在从我们的服务器收到消息时立即调用,在此处我们有以下代码。
protected override void OnMessage(Context context, Intent intent)
    {
        string message = string.Empty;

        // Extract the push notification message from the intent.
        if (intent.Extras.ContainsKey("message"))
        {
            message = intent.Extras.Get("message").ToString();
            var title = "Notification:";

            // Create a notification manager to send the notification.
            var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;

            Intent resultIntent = new Intent(context, typeof(MainActivity));
            resultIntent.PutExtras(intent.Extras);

            PendingIntent contentIntent = PendingIntent.GetActivity(
                                              context,
                                              0,
                                              new Intent(),
                                              PendingIntentFlags.UpdateCurrent
                                          );


            // Create the notification using the builder.
            var builder = new Notification.Builder(context);
            builder.SetAutoCancel(true);
            builder.SetContentTitle(title);
            builder.SetContentText(message);
            builder.SetSmallIcon(Resource.Drawable.icon);
            builder.SetContentIntent(contentIntent);
            builder.SetExtras(intent.Extras);

            var notification = builder.Build();

            // Display the notification in the Notifications Area.
            notificationManager.Notify(0, notification);
        }
    }

在MainActivity.cs中,我能够捕获来自通知的数据,当用户按下它时,但这会创建一个新的活动而不是继续当前的活动(这应该由PendingIntentFlags.UpdateCurrent定义)。
我想做的基本上就是在Android上以与iOS相同的方式接收通知,它基本上只是调用应用程序根目录中的委托,然后将信息发送到应用程序本身。
我个人对Android几乎没有经验,我的大多数谷歌搜索都没有显示出在按下通知时执行代码并加载其数据的任何方法,他们只是展示如何创建通知而不做任何事情。
编辑:
问题已解决,以下是解决方法:
在MainActivity.cs中,我已将LaunchMode = LaunchMode.SingleTop添加到Activity属性,并重写了OnNewIntent方法
protected override void OnNewIntent(Intent intent)
{
    string json = intent.Extras.GetString("payload");
    json = HttpUtility.UrlDecode(json).Replace("\\", "");
    PushReceiver.HandlePush(json, true);

    base.OnNewIntent(intent);
}

在我的PushBroadcastReceiver中,我已经将OnMessage方法更改为:
protected override void OnMessage(Context context, Intent intent)
    {
        string message = string.Empty;

        // Extract the push notification message from the intent.
        if (intent.Extras.ContainsKey("message"))
        {
            message = intent.Extras.Get("message").ToString();
            var title = "Notification:";

            // Create a notification manager to send the notification.
            var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;

            Intent resultIntent = new Intent(context, typeof(MainActivity));
            resultIntent.PutExtras(intent.Extras);

            PendingIntent resultPendingIntent =
                PendingIntent.GetActivity(
                    context,
                    0,
                    resultIntent,
                    PendingIntentFlags.UpdateCurrent
                );

            // Create the notification using the builder.
            var builder = new Notification.Builder(context);
            builder.SetAutoCancel(true);
            builder.SetContentTitle(title);
            builder.SetContentText(message);
            builder.SetSmallIcon(Resource.Drawable.icon);
            builder.SetContentIntent(resultPendingIntent);

            var notification = builder.Build();

            // Display the notification in the Notifications Area.
            notificationManager.Notify(new Random((int)(DateTime.Now.ToFileTime() % int.MaxValue)).Next(), notification);

        }
    }

由于“LaunchMode = LaunchMode.SingleTop”和“PendingIntentFlags.UpdateCurrent”,MainActivity不再重新创建,但每次用户点击通知时都会调用OnNewIntent事件。当捕获到OnNewIntent事件时,您可以通过App.Current完全访问应用程序(并将其转换为必要的Page/View/Class),因为不会创建新的Activity,这还确保了通知正常工作,而不会因重新创建Activity而出现问题。
感谢Jon Douglas

如果MainActivity不是当前视图,会发生什么? - xleon
在我们的情况下,这种情况不可能发生(整个应用程序依赖于主Activity),所以我不确定,但我的最佳猜测是Push将被发送到Push Builder设置的Activity。 - MegaMiley
1个回答

8

2
谢谢!singleTop帮了很大的忙,再加上一些Google搜索,我成功地让通知在主活动中调用OnNewIntent而不是创建一个新的活动,现在推送通知系统正常工作了:D,我会在今天稍后更新上面的代码以展示工作版本。 - MegaMiley
我正在尝试做同样的事情。在设置了android:launchMode="singleTop"并将singleTop添加到意图后,它仍然无法正常工作。 - xleon
1
我已经更新了帖子并附上了我的可用代码,你可以看一下。此外,我没有在Intent上使用SingleTop,而是使用UpdateCurrent,这可能是不同之处。 - MegaMiley

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