微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android Xamarin使推送通知不创建新活动但使用当前活动

我们目前正在开发适用于iOS,Android和WP8的Xamarin Forms移动应用程序,目前我正在开发Android的通知部分.

现在,我能够收到通知并将其显示给用户,当他们点击通知时,它会将他们带到应用程序,但它不会像我们希望它一样工作.它不是继续在同一个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中,当用户按下时,我能够从Notification中捕获数据,但这会创建一个新的活动而不是继续在当前的活动中(PendingIntentFlags.UpdateCurrent应该定义).

我想要做的基本操作基本上与在iOS上接收通知的方式相同,就像在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(并将其转换为必要的页面/视图/类),因为没有创建新的活动,它还确保通知可以正常工作,而不会因重新创建活动而导致错误.

谢谢Jon Douglas

解决方法:

我相信你需要在你的Activity中使用singleTop:

机器人:launchMode = “singleTop”

http://developer.android.com/guide/topics/manifest/activity-element.html#lmode

此外,这些Intent标志也可能有所帮助:

http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP

http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_SINGLE_TOP

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐