代码之家  ›  专栏  ›  技术社区  ›  Nick Jones

Xamarin表单-处理通知单击

  •  6
  • Nick Jones  · 技术社区  · 6 年前

    我有一个Xamarin表单应用程序,它会引发Android通知,但我很难创建一个简单的页面,当用户单击通知时,该页面将与用户交互。 我在Xamarin了解这一点。表单只有1个活动,因此挂起的目的必须是该主活动

    我已将LaunchMode设置为SingleTop和and Intent Filter,以匹配PendingEvent中使用的Intent名称

    现在,当我单击通知时,我确实会被路由到MainActivity的OnResume,但我不知道如何: 1) 认识到我参与此活动是因为通知点击-我尝试向待定意图添加额外内容,但当我检查此内容时,它不存在。意图额外费用 2) 即使我知道由于通知单击而处于活动中,我如何从活动中启动特定页面。我是Xamarin的新手,但我不知道如何导航到内容页或访问导航堆栈。

    这一定是一个非常常见的用例,但我找不到任何相关的。

    1 回复  |  直到 6 年前
        1
  •  9
  •   SushiHangover    6 年前

    确保已设置 LaunchMode.SingleTop 在您的 MainActivity :

    启动模式。单顶

    [Activity(~~~, LaunchMode = LaunchMode.SingleTop, ~~~]
    public class MainActivity
    {
       ~~~~
    

    在您的 主要活动 (FormsAppCompatActivity子类)添加 OnNewIntent 覆盖:

    OnNewIntent公司:

    protected override void OnNewIntent(Intent intent)
    {
        base.OnNewIntent(intent);
        NotificationClickedOn(intent);
    }
    

    现在您可以检查 intent.Action / intent.HasExtra 以确定发送的通知是否是您的,并对其进行处理。具有 Xamarin.Forms 最简单的方法是使用 MessagingCenter 发送在中订阅的邮件。NetStd/PCL 沙马林。表格 代码库。

    通知单击打开:

    void NotificationClickedOn(Intent intent)
    {
        if (intent.Action == "ASushiNotification" && intent.HasExtra("MessageFromSushiHangover"))
        {
            /// Do something now that you know the user clicked on the notification...
    
            var notificationMessage = intent.Extras.GetString("MessageFromSushiHangover");
            var winnerToast = Toast.MakeText(this, $"{notificationMessage}.\n\n🍣 Please send 2 BitCoins to SushiHangover to process your winning ticket! 🍣", ToastLength.Long);
            winnerToast.SetGravity(Android.Views.GravityFlags.Center, 0, 0);
            winnerToast.Show();
        }
    }
    

    发送通知示例:

    void SendNotifacation()
    {
        var title = "Winner, Winner, Chicken Dinner";
        var message = "You just won a million StackOverflow reputation points";
    
        var intent = new Intent(BaseContext, typeof(MainActivity));
        intent.SetAction("ASushiNotification");
        intent.PutExtra("MessageFromSushiHangover", message);
        var pending = PendingIntent.GetActivity(BaseContext, 0, intent, PendingIntentFlags.CancelCurrent);
    
        using (var notificationManager = NotificationManager.FromContext(BaseContext))
        {
            Notification notification;
            if (Android.OS.Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.O)
            {
    #pragma warning disable CS0618 // Type or member is obsolete
                notification = new Notification.Builder(BaseContext)
                                                            .SetContentTitle(title)
                                                            .SetContentText(message)
                                                            .SetAutoCancel(true)
                                                            .SetSmallIcon(Resource.Drawable.icon)
                                                            .SetDefaults(NotificationDefaults.All)
                                                            .SetContentIntent(pending)
                                                            .Build();
    #pragma warning restore CS0618 // Type or member is obsolete
            }
            else
            {
                var myUrgentChannel = BaseContext.PackageName;
                const string channelName = "Messages from SushiHangover";
    
                NotificationChannel channel;
                channel = notificationManager.GetNotificationChannel(myUrgentChannel);
                if (channel == null)
                {
                    channel = new NotificationChannel(myUrgentChannel, channelName, NotificationImportance.High);
                    channel.EnableVibration(true);
                    channel.EnableLights(true);
                    channel.SetSound(
                        RingtoneManager.GetDefaultUri(RingtoneType.Notification),
                        new AudioAttributes.Builder().SetUsage(AudioUsageKind.Notification).Build()
                    );
                    channel.LockscreenVisibility = NotificationVisibility.Public;
                    notificationManager.CreateNotificationChannel(channel);
                }
                channel?.Dispose();
    
                notification = new Notification.Builder(BaseContext)
                                                            .SetChannelId(myUrgentChannel)
                                                            .SetContentTitle(title)
                                                            .SetContentText(message)
                                                            .SetAutoCancel(true)
                                                            .SetSmallIcon(Resource.Drawable.icon)
                                                            .SetContentIntent(pending)
                                                            .Build();
            }
            notificationManager.Notify(1331, notification);
            notification.Dispose();
        }
    }