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

Android通知操作按钮单击侦听器,android,push-notification,google-cloud-messaging,Android,Push Notification,Google Cloud Messaging,我正在开发一个活动计划应用程序,目前正在开发一个功能,用于将项目分配给客人。 分配物品时,会向客人发送通知,询问他是否可以携带所需物品。根据用户的输入,HTTP请求被发送回服务器,更新项目携带/不携带的状态 我对点击通知操作按钮的反应有些迟钝,因为我不太明白它是如何工作的。我设法显示了动作按钮,但不知道如何对点击按钮做出反应。正如我在网上看到的,对通知操作按钮的响应由PendingEvent管理,我无法理解它是如何工作的,以及如何根据单击的按钮使用它发送HTTP请求 我很乐意得到一些帮助和建议

我正在开发一个活动计划应用程序,目前正在开发一个功能,用于将项目分配给客人。 分配物品时,会向客人发送通知,询问他是否可以携带所需物品。根据用户的输入,HTTP请求被发送回服务器,更新项目携带/不携带的状态

我对点击通知操作按钮的反应有些迟钝,因为我不太明白它是如何工作的。我设法显示了动作按钮,但不知道如何对点击按钮做出反应。正如我在网上看到的,对通知操作按钮的响应由PendingEvent管理,我无法理解它是如何工作的,以及如何根据单击的按钮使用它发送HTTP请求

我很乐意得到一些帮助和建议

[这是发送给用户的通知示例。][

这是通知用户的方法:

    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());
}
高级版谢谢!

您必须为您的操作设置PendingEvent对象,这样当用户在通知按钮上执行任何操作时,就会触发特定的意图。 可以为活动、广播接收器或服务设置挂起内容。 如果希望在用户单击通知按钮时显示活动,则可以使用-

    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
您必须为您的操作设置PendingEvent对象,以便当用户在通知按钮上执行任何操作时,触发特定意图。 可以为活动、广播接收器或服务设置挂起内容。 如果希望在用户单击通知按钮时显示活动,则可以使用-

    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

我想这对你有帮助

//带有按钮的通知示例

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

我想这对你有帮助

//带有按钮的通知示例

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