Android Xamarin 使推送通知不创建新活动而是使用当前活动 [英] Android Xamarin make push notification not create a new activity but use the current one

查看:48
本文介绍了Android Xamarin 使推送通知不创建新活动而是使用当前活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We're currently working on a mobile app in Xamarin Forms for iOS, Android and WP8 and I'm currently working on the notifications part for Android.

现在我能够接收通知并将它们显示给用户,当他们点击通知时,它会将它们带到应用程序,但它不像我们希望的那样工作.它不是在同一个 Activity 中继续运行,而是启动一个全新的 Activity,该 Activity 会丢失实际应用的整个上下文.

Right now I am able to receive notifications and show them to the user and when they click on the notification it takes them to the app but it doesn't work like we want it to work. Instead of continuing on in the same Activity it starts a whole new activity which loses the entire context of the actual app.

在我们的推送通知接收器上,我们覆盖了 OnMessage 方法,一旦有东西从我们的服务器进来就会被调用,在这里我们有以下代码

On our push notification receiver we are overriding the OnMessage method which get's called as soon as something comes in from our server and in here we have the following code

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 应定义).

In the MainActivity.cs I am able to catch the data form the Notification when the user presses it but that creates a new activity instead of continuing on in the current one (which PendingIntentFlags.UpdateCurrent should define).

我想做的事情的基本原理是在 Android 上接收通知的方式与我们在 iOS 上的接收方式基本相同,它基本上只是在应用程序的根目录中调用一个委托,然后将信息发送到应用程序本身.

The basics of what I want to do is basically have the same manner of receiving notifications on Android as we do on iOS where it basically just calls a delegate in the root of the application which then sends the information through to the app itself.

我自己对 Android 几乎没有经验,我的大部分 Google 搜索在按下通知并加载数据时都没有显示任何执行代码的方式,只是显示了如何创建通知而不做任何事情.

I myself have little experience with Android and most of my Google searches don't show any way of executing code when the notification is pressed and also loading it's data, the just show how to create a notification without it doing much of anything.

问题已解决,方法如下

在 MainActivity.cs 中,我将 LaunchMode = LaunchMode.SingleTop 添加到 Activity 属性并像这样覆盖 OnNewIntent 方法

In the MainActivity.cs I've added LaunchMode = LaunchMode.SingleTop to the Activity attribute and overridden the method OnNewIntent like this

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 方法更改为

And in my PushBroadcastReceiver I've changed the OnMessage method to

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 完全访问应用程序(并将其投射到必要的页面/视图/类),因为没有创建新的 Activity,它还确保通知正常工作,不会因重新创建 Activity 引起的故障.

Because of the 'LaunchMode = LaunchMode.SingleTop' and 'PendingIntentFlags.UpdateCurrent' the MainActivity no longer get's re-created but the OnNewIntent event is called every time the user clicks on the notification, when the OnNewIntent event is caught you have full access to the app through App.Current (and cast it to the necessary Page/View/Class) because no new Activity is created, it also ensures that the Notification works without hitches that would be caused by re-creating the Activity.

感谢乔恩·道格拉斯

推荐答案

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

I believe you need to make use of singleTop in your Activity here:

android:launchMode="singleTop"

http://developer.android.com/guide/主题/清单/活动元素.html#lmode

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

Additionally these Intent flags may help as well:

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

这篇关于Android Xamarin 使推送通知不创建新活动而是使用当前活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆