简体   繁体   English

如何处理推送通知动作点击?

[英]How to handle push notification Action taps?

I have a push notification with 2 actions (Accept, Decline).我有一个带有 2 个操作(接受、拒绝)的推送通知。 How can I determine which action was actually tapped and then have the notification disappear (acting as if the user has tapped the actual notification and not the action button)?如何确定实际点击了哪个操作,然后让通知消失(就像用户点击了实际通知而不是操作按钮一样)? It looks like both action taps go into my OnNewIntent method and I'd like to do different actions based on which one was tapped.看起来两个动作都将 go 轻拍到我的OnNewIntent方法中,我想根据哪个被轻拍来做不同的动作。 My notification is also still showing in the notification bar except the actions (buttons) are no longer displayed.除了不再显示操作(按钮)之外,我的通知也仍然显示在通知栏中。 The notification fully goes away when I tap it.当我点击它时,通知会完全消失。

Here's my SendLocalNotification method in FirebaseService (ignore the comments. they're for me):这是我在FirebaseService中的SendLocalNotification方法(忽略评论。它们是给我的):

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());
    }

And then my OnNewIntent in MainActivity :然后我在MainActivityOnNewIntent

protected override void OnNewIntent(Intent intent)
    {
        if (intent.HasExtra("OpenPage"))
        {
            MessagingCenter.Send(Xamarin.Forms.Application.Current, "Notification");
        }

        base.OnNewIntent(intent);
    }

EDIT: My OnNewIntent has that logic currently because I was using the MessagingCenter to send a message to a subscriber on a view model and doing a view navigation.编辑:我的OnNewIntent目前具有该逻辑,因为我正在使用 MessagingCenter 在视图 model 上向订阅者发送消息并进行视图导航。 Then I discovered Actions and that's how I ended up here trying to do that logic with the notification buttons.然后我发现了 Actions,这就是我最终在这里尝试使用通知按钮执行该逻辑的方式。

Create Nofification with "yes", "no" and "maybe" actions:使用“yes”、“no”和“maybe”动作创建Nofification:

//Yes intent
Intent yesReceive = new Intent();  
yesReceive.setAction(YES_ACTION);
PendingIntent pendingIntentYes = PendingIntent.getBroadcast(this, 12345, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.addAction(R.drawable.calendar_v, "Yes", pendingIntentYes);

//Maybe intent
Intent maybeReceive = new Intent();  
maybeReceive.setAction(MAYBE_ACTION);
PendingIntent pendingIntentMaybe = PendingIntent.getBroadcast(this, 12345, maybeReceive, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.addAction(R.drawable.calendar_question, "Partly", pendingIntentMaybe);

//No intent
Intent noReceive = new Intent();  
noReceive.setAction(NO_ACTION);
PendingIntent pendingIntentNo = PendingIntent.getBroadcast(this, 12345, noReceive, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.addAction(R.drawable.calendar_x, "No", pendingIntentNo);

Then create broadcast receiver to called when action clicked:然后创建广播接收器以在单击操作时调用:

@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");
    }
}

Notice to be appropriate when you sending actions.发送动作时注意适当。

So I was able to figure out what (I think) I needed by following some of what Barel answered as well as some other SO pages and piecing this together.因此,通过遵循 Barel 回答的一些内容以及其他一些 SO 页面并将它们拼凑在一起,我能够弄清楚(我认为)我需要什么。

I'm sure I'll run into some changes I need to make later but, for now, this got me to where I could have 2 actions (Accept, Decline), select either of the action buttons, and then hit some logic where I'm going to implement what I want to do with both actions.我确定我稍后会遇到一些需要进行的更改,但是,就目前而言,这让我可以进行 2 个操作(接受、拒绝),select 任何一个操作按钮,然后点击一些逻辑我将用这两个动作实现我想要做的事情。


SendLocalNotification method in FirebaseService class: FirebaseService class 中的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());
    }

And then I had to update my OnNewIntent override in MainActivity .然后我必须在MainActivity更新我的OnNewIntent覆盖。 Notice the manager.CancelAll() .注意manager.CancelAll() I had to do this to actually remove the notification from my bar after clicking the action:单击操作后,我必须这样做才能真正从我的栏中删除通知:

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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM