我尝试按照此处找到的说明在将打开特定视图的通知点击上添加 MessagingCenter 订阅操作。在某个地方我的发送/订阅没有互相交谈,我只是看不到在哪里。消息中心的细节对我来说仍然是新的,所以我确定我只是在某个地方使用了错误的类或发件人。
下面的代码已根据链接中显示的内容进行了修改。这个想法仍然大致相同。
这是我的FirebaseService类中的SendLocalNotification方法:
void SendLocalNotification(string body)
{
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.SingleTop);
intent.PutExtra("OpenPage", "SomePage");
//Unique request code to avoid PendingIntent collision.
var requestCode = new Random().Next();
var pendingIntent = PendingIntent.GetActivity(this, requestCode, intent, PendingIntentFlags.OneShot);
var notificationBuilder = new NotificationCompat.Builder(this)
.SetContentTitle("Load Match")
.SetSmallIcon(Resource.Drawable.laundry_basket_icon_15875)
.SetContentText(body)
.SetAutoCancel(true)
.SetShowWhen(false)
.SetContentIntent(pendingIntent);
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
notificationBuilder.SetChannelId(AppConstants.NotificationChannelName);
}
var notificationManager = NotificationManager.FromContext(this);
notificationManager.Notify(0, notificationBuilder.Build());
}
这是android MainActivity中的OnNewIntent方法:
protected override void OnNewIntent(Intent intent)
{
if (intent.HasExtra("OpenPage"))
{
MessagingCenter.Send(this, "Notification");
}
base.OnNewIntent(intent);
}
这里是我尝试在我的LoadsPageViewModel中订阅消息的地方(不在 android 项目中):
public LoadsPageViewModel()
{
MessagingCenter.Subscribe<LoadsPageViewModel>(this, "Notification", (sender) =>
{
// navigate to a view here
});
}