C# 如何处理推送通知操作点击?
我有一个包含两个操作(接受、拒绝)的推送通知。如何确定实际点击了哪个操作,然后通知消失(就像用户点击了实际通知而不是操作按钮一样)?看起来两个动作点击都进入了我的OnNewIntent方法,我想根据点击的动作做不同的动作。我的通知仍显示在通知栏中,但不再显示操作(按钮)。当我点击它时,通知完全消失 以下是我在FirebaseService中的SendLocalNotification方法(忽略注释。它们是给我的): 然后是我的活动中的OnNewIntent:C# 如何处理推送通知操作点击?,c#,android,firebase,xamarin.forms,push-notification,C#,Android,Firebase,Xamarin.forms,Push Notification,我有一个包含两个操作(接受、拒绝)的推送通知。如何确定实际点击了哪个操作,然后通知消失(就像用户点击了实际通知而不是操作按钮一样)?看起来两个动作点击都进入了我的OnNewIntent方法,我想根据点击的动作做不同的动作。我的通知仍显示在通知栏中,但不再显示操作(按钮)。当我点击它时,通知完全消失 以下是我在FirebaseService中的SendLocalNotification方法(忽略注释。它们是给我的): 然后是我的活动中的OnNewIntent: protected override
protected override void OnNewIntent(Intent intent)
{
if (intent.HasExtra("OpenPage"))
{
MessagingCenter.Send(Xamarin.Forms.Application.Current, "Notification");
}
base.OnNewIntent(intent);
}
编辑:我的OnNewIntent当前具有该逻辑,因为我使用MessagingCenter向视图模型上的订阅者发送消息并进行视图导航。然后我发现了动作,这就是为什么我在这里尝试用通知按钮来实现这一逻辑。用“是”、“否”和“可能”动作创建NOFIFIFIcation:
然后创建要在单击操作时调用的广播接收器:
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(YES_ACTION.equals(action)) {
Log.v("shuffTest","Pressed YES");
} else if(MAYBE_ACTION.equals(action)) {
Log.v("shuffTest","Pressed NO");
} else if(NO_ACTION.equals(action)) {
Log.v("shuffTest","Pressed MAYBE");
}
}
请注意,在发送操作时要适当 因此,我能够通过遵循巴雷尔的一些回答以及其他一些So页面并将其拼凑在一起,找出(我认为)我需要什么 我确信我以后会遇到一些需要做的更改,但现在,这让我有了两个操作(接受、拒绝),选择其中一个操作按钮,然后点击一些逻辑,在那里我将实现我想对这两个操作执行的操作
FirebaseService类中的SendLocalNotification方法:
void SendLocalNotification(string body)
{
//Unique request code to avoid PendingIntent collision.
var requestCode = new Random().Next();
// accept
var acceptIntent = new Intent(this, typeof(MainActivity));
acceptIntent.SetAction("ACCEPT_ACTION");
var pendingIntentAccept = PendingIntent.GetActivity(this, requestCode, acceptIntent, PendingIntentFlags.OneShot);
// decline
var declineIntent = new Intent(this, typeof(MainActivity));
declineIntent.SetAction("DECLINE_ACTION");
var pendingIntentDecline = PendingIntent.GetActivity(this, requestCode, declineIntent, PendingIntentFlags.OneShot);
var notificationBuilder = new NotificationCompat.Builder(this)
.AddAction(0, "Accept", pendingIntentAccept)
.AddAction(0, "Decline", pendingIntentDecline)
.SetContentTitle("Load Match")
.SetSmallIcon(Resource.Drawable.laundry_basket_icon_15875)
.SetContentText(body)
.SetAutoCancel(true)
.SetShowWhen(false)
.SetContentIntent(pendingIntentAccept)
.SetContentIntent(pendingIntentDecline);
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
notificationBuilder.SetChannelId(AppConstants.NotificationChannelName);
}
var notificationManager = NotificationManager.FromContext(this);
notificationManager.Notify(0, notificationBuilder.Build());
}
然后我必须更新main活动中的OnNewIntent覆盖。请注意管理器.CancelAll()
。我必须这样做,才能在单击操作后从我的栏中删除通知:
protected override void OnNewIntent(Intent intent)
{
switch (intent.Action)
{
case "ACCEPT_ACTION":
Console.WriteLine("hit accept action case.");
break;
case "DECLINE_ACTION":
Console.WriteLine("hit decline action case.");
break;
default:
Console.WriteLine("didn't hit either action.");
break;
}
var manager = (NotificationManager)this.GetSystemService(Context.NotificationService);
manager.CancelAll();
base.OnNewIntent(intent);
}
谢谢你,巴雷尔。当我实现您的回复时,我可以在通知中显示操作,但当我单击其中一个操作时,我的OnReceive覆盖从未被命中。广播接收器是一种代替在OnNewIntent中尝试做某事的方式吗?我知道,这是一种方式,如果你想让接收器打电话,请看以下内容:你能标记这个答案吗,它将帮助更多有同样问题的人:)?必须再等6个小时
void SendLocalNotification(string body)
{
//Unique request code to avoid PendingIntent collision.
var requestCode = new Random().Next();
// accept
var acceptIntent = new Intent(this, typeof(MainActivity));
acceptIntent.SetAction("ACCEPT_ACTION");
var pendingIntentAccept = PendingIntent.GetActivity(this, requestCode, acceptIntent, PendingIntentFlags.OneShot);
// decline
var declineIntent = new Intent(this, typeof(MainActivity));
declineIntent.SetAction("DECLINE_ACTION");
var pendingIntentDecline = PendingIntent.GetActivity(this, requestCode, declineIntent, PendingIntentFlags.OneShot);
var notificationBuilder = new NotificationCompat.Builder(this)
.AddAction(0, "Accept", pendingIntentAccept)
.AddAction(0, "Decline", pendingIntentDecline)
.SetContentTitle("Load Match")
.SetSmallIcon(Resource.Drawable.laundry_basket_icon_15875)
.SetContentText(body)
.SetAutoCancel(true)
.SetShowWhen(false)
.SetContentIntent(pendingIntentAccept)
.SetContentIntent(pendingIntentDecline);
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
notificationBuilder.SetChannelId(AppConstants.NotificationChannelName);
}
var notificationManager = NotificationManager.FromContext(this);
notificationManager.Notify(0, notificationBuilder.Build());
}
protected override void OnNewIntent(Intent intent)
{
switch (intent.Action)
{
case "ACCEPT_ACTION":
Console.WriteLine("hit accept action case.");
break;
case "DECLINE_ACTION":
Console.WriteLine("hit decline action case.");
break;
default:
Console.WriteLine("didn't hit either action.");
break;
}
var manager = (NotificationManager)this.GetSystemService(Context.NotificationService);
manager.CancelAll();
base.OnNewIntent(intent);
}