Xamarin 在通知点击时形成打开视图模型

2024-03-20

我正在使用此代码在通知栏中显示通知。点击通知后,将启动主要活动。是否可以使用 MvvmCross 在 Xamarin 表单应用程序中启动视图模型而不是活动。

 Intent notificationIntent = new Intent(context,typeof(MainActivity));
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |                                Intent.FLAG_ACTIVITY_SINGLE_TOP);      
   PendingIntent pIntent = PendingIntent.getActivity(context, code,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationManager manager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder notify = new NotificationCompat.Builder(
            context);

    notify.setContentIntent(pIntent);
    notify.setSmallIcon(R.drawable.app_icon);
    notify.setContentTitle(“Title”);
    manager.notify(reqCode, notify.build());

我的想法是利用PutExtra https://developer.xamarin.com/recipes/android/fundamentals/activity/pass_data_between_activity/结合消息中心 https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/messaging-center/.

首先,在通知栏中显示通知:

Intent intent = new Intent(Forms.Context, typeof(MainActivity));
if (openPage)
{
    intent.SetFlags(ActivityFlags.SingleTop);
    intent.PutExtra("OpenPage", "SomePage");
}

const int pendingIntentId = 0;
PendingIntent pendingIntent = PendingIntent.GetActivity(Forms.Context, pendingIntentId, intent, PendingIntentFlags.OneShot);

var nMgr = (NotificationManager)Android.App.Application.Context.GetSystemService(Context.NotificationService);
Notification.Builder notBuilder = new Notification.Builder(Android.App.Application.Context)
    .SetContentIntent(pendingIntent)
    .SetContentTitle("SomeApp")
    .SetContentText(message)
    .SetDefaults(NotificationDefaults.Sound | NotificationDefaults.Vibrate)
    .SetSmallIcon(Resource.Drawable.ic_launcher)
    .SetAutoCancel(true);

var notification = notBuilder.Build();
nMgr.Notify(0, notification);

In MainActivity.cs您检查额外的内容:

protected override void OnNewIntent(Intent intent)
{
    // Send message to the PCL (XF) if a certain page should be opened.
    if (intent.HasExtra("OpenPage"))
    {
        string pageName = intent.GetStringExtra("OpenPage") ?? "None";

        if (pageName != "None")
        {
            var message = new OpenPageMessage { PageName = pageName };
            MessagingCenter.Send(message, Message.Msg_OpenPage);
        }
    }

    base.OnNewIntent(intent);
}

以及您的中央导航实例(例如MainPage),订阅此消息:

MessagingCenter.Subscribe<Message.OpenPageMessage>(this, Message.Msg_OpenPage, (async) message =>
{
    // Loads a certain page if a message is received

    switch (message.PageName)
    {
        case "SomePage":
            await Navigation.PushModalAsync(new SomePage(), true);
            break;
        default:
            break;
    }
});

另外,这是我的消息.cs:

public class Message
{
    public const string Msg_OpenPage = "OpenPage";

    public class OpenPageMessage {
        public string PageName { get; set; }
    }
}

借助这个source https://stackoverflow.com/a/44964328/426227.

Edit

如果您一次有多个推送通知,并且通知被覆盖,则会出现问题。人们可以使用不同的requestCode https://developer.android.com/reference/android/app/PendingIntent.html#getActivity(android.content.Context,%20int,%20android.content.Intent,%20int) or use FLAG_UPDATE_CURRENT https://developer.android.com/reference/android/app/PendingIntent.html#FLAG_UPDATE_CURRENT.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Xamarin 在通知点击时形成打开视图模型 的相关文章

随机推荐