Android Xamarin 使推送通知不创建新 Activity 而是使用当前 Activity

标签 android ios xamarin notifications

我们目前正在为 iOS、Android 和 WP8 开发 Xamarin Forms 中的移动应用程序,而我目前正在为 Android 开发通知部分。

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

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

我自己对 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 完全访问应用程序(并将其转换到必要的页面/ View /类),因为没有创建新的 Activity ,它还确保通知正常工作,不会因重新创建 Activity 而引起的故障。

谢谢 Jon Douglas !

最佳答案

关于Android Xamarin 使推送通知不创建新 Activity 而是使用当前 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34754149/

相关文章:

android - 如何在 FragmentActivity 中使用自定义 iPhone 选项卡?

android - Xamarin Android 文件系统路径

xamarin - 如何在Xamarin和Asp.net core之间共享类库?

iphone - 是否可以从 XCode 4.1 或 4.0.2 构建到 iOS 5 设备?

android - MvvmCross - 将汉堡菜单更改为后退按钮

android - 更新到canary Preview 5后,android studio无法运行

android - 应用程序中的onCreate是否在升级时运行?

Android - 为什么 IMarketBillingService 文件是 iadl 文件而不是 .java

ios - Google Maps iOS SDK 和 Storyboard

ios - 应用程序的本地化名称不正确 iOS