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

时间:2016-01-12 21:31:11

标签: android ios xamarin notifications

我们目前正在开发适用于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没有什么经验,我的大多数Google搜索都没有显示任何按下通知时执行代码的方式,也没有加载它的数据,只显示如何创建通知而不执行任何操作

编辑:

问题已经解决,这是如何

在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完全访问应用程序(并将其转换为必要的页面/视图/类),因为没有创建新的活动,它还确保通知可以在不重复创建活动的情况下正常工作。 / p>

谢谢Jon Douglas

1 个答案:

答案 0 :(得分:7)