【发布时间】:2020-04-19 19:34:15
【问题描述】:
我有一个包含 2 个操作(接受、拒绝)的推送通知。如何确定实际点击了哪个操作,然后让通知消失(就像用户点击了实际通知而不是操作按钮一样)?看起来两个动作点击都进入了我的 OnNewIntent 方法,我想根据哪个被点击来执行不同的操作。除了不再显示操作(按钮)之外,我的通知也仍然显示在通知栏中。当我点击它时,通知会完全消失。
这是我在 FirebaseService 中的 SendLocalNotification 方法(忽略 cmets。它们是给我的):
void SendLocalNotification(string body)
{
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.SingleTop);
intent.PutExtra("OpenPage", "SomePage");
//intent.PutExtra("message", body);
//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)
.AddAction(0, "Accept", pendingIntent) // THIS ADDS A BUTTON TO THE NOTIFICATION
.AddAction(0, "Decline", pendingIntent) // THIS ADDS A BUTTON TO THE NOTIFICATION
.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());
}
然后我在 MainActivity 中的 OnNewIntent:
protected override void OnNewIntent(Intent intent)
{
if (intent.HasExtra("OpenPage"))
{
MessagingCenter.Send(Xamarin.Forms.Application.Current, "Notification");
}
base.OnNewIntent(intent);
}
编辑:我的 OnNewIntent 目前具有该逻辑,因为我正在使用 MessagingCenter 向视图模型上的订阅者发送消息并进行视图导航。然后我发现了 Actions,这就是我最终在这里尝试使用通知按钮执行该逻辑的方式。
【问题讨论】:
标签: c# android firebase xamarin.forms push-notification