简体   繁体   English

Android 通知操作按钮单击侦听器

[英]Android Notification Action Button Click Listener

I am developing an event planning application, and I'm currently working on a feature for assigning an item to a guest.我正在开发一个活动策划应用程序,目前正在开发一项功能,用于将项目分配给客人。 When the item is assigned, a notification is pushed to the guest, asking him whether or not he can bring the required item.分配物品后,会向客人推送通知,询问他是否可以携带所需物品。 Depending on the input of the user, an HTTP request is sent back to the server, updating the status of item (bringing/not bringing).根据用户的输入,一个 HTTP 请求被发送回服务器,更新项目的状态(带/不带)。

I'm kind of stuck on responding to the click on the notification action buttons, since I didn't quite understand how it works.我有点坚持响应点击通知操作按钮,因为我不太明白它是如何工作的。 I managed to show the action buttons, but couldn't figure out how to respond to clicking on them.我设法显示了操作按钮,但无法弄清楚如何响应点击它们。 As I've seen online, the response to the notification action buttons, is managed by the PendingIntent, which I couldn't manage to understand how it works, and how I can use it to send HTTP requests according to the button clicked.正如我在网上看到的,对通知操作按钮的响应是由 PendingIntent 管理的,我无法理解它是如何工作的,以及如何根据单击的按钮使用它来发送 HTTP 请求。

I'd be glad for some help and advice.我很高兴能得到一些帮助和建议。

[This is an example of the notification sent to the user.][ [这是发送给用户的通知示例。][

This is the method that notifies the user:这是通知用户的方法:

    private void notifyUser(Bundle data) {
    ...
    //Notification configuration.
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(...)
            .setLargeIcon(...)
            .setContentTitle(...)
            .setContentText(...)
            .setAutoCancel(...)
            .setSound(...);

        //Get the item data from the request sent to the GCM server.
        Item item = GsonFactory.getGson()
                .fromJson(data.getString("data"), Item.class);

        //This is the part that I do not understand...
        notificationBuilder
                .addAction(R.drawable.ic_close_black, "No", null)
                .addAction(R.drawable.ic_done_black, "Yes", null);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());
}

Thank you in advanced!先谢谢了!

I hoope this can help you我希望这可以帮助你

//Exemple of notification with Button //带按钮的通知示例

private static void scheduleNotificationWithButton(Context context, String message) {

    int notifReCode = 1;

    //What happen when you will click on button
    Intent intent = new Intent(context, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_ONE_SHOT);

    //Button
    NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.mipmap.ic_launcher, "Go", pendingIntent).build();

    //Notification
    Notification notification = new NotificationCompat.Builder(context)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentText("Back to Application ?")
            .setContentTitle("Amazing news")
            .addAction(action) //add buton
            .build();

    //Send notification
    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, notification);
}

You have to set a PendingIntent object to your actions so that when the user performs any action on the notification buttons that particular intent gets fired.您必须为您的操作设置一个PendingIntent对象,以便当用户对通知按钮执行任何操作时,特定意图会被触发。 The PendingIntent may be set for a Activity, Broadcast Receiver or Service .可以为Activity, Broadcast Receiver or Service设置 PendingIntent。 If you want to show an activity when the user clicks the notification button then you can use -如果您想在用户单击通知按钮时显示活动,则可以使用 -

    Intent intent = new Intent(this, NotificationReceiverActivity.class);
    intent.putExtra(<key>, item ); //If you wan to send data also
    PendingIntent pIntent = PendingIntent.getActivity(this, <notification_id>, intent, 0); //<notification_id> may be any number

And set this intent to your notification actions -并将此意图设置为您的通知操作 -

notificationBuilder
                .addAction(R.drawable.ic_close_black, "No", pIntent)
                .addAction(R.drawable.ic_done_black, "Yes", pIntent);

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

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