【问题标题】:Android Notification Action Button Click ListenerAndroid 通知操作按钮单击侦听器
【发布时间】:2016-03-17 17:38:28
【问题描述】:

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

我有点卡在响应点击通知操作按钮上,因为我不太明白它是如何工作的。我设法显示了操作按钮,但不知道如何响应单击它们。正如我在网上看到的,对通知操作按钮的响应是由 PendingIntent 管理的,我无法理解它是如何工作的,以及如何使用它来根据单击的按钮发送 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());
}

谢谢你!

【问题讨论】:

    标签: android push-notification google-cloud-messaging


    【解决方案1】:

    希望对你有帮助

    //按钮通知示例

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

    【讨论】:

      【解决方案2】:

      您必须为您的操作设置一个PendingIntent 对象,以便当用户在通知按钮上执行任何操作时,特定意图会被触发。 PendingIntent 可以设置为Activity, Broadcast Receiver or Service。 如果您想在用户单击通知按钮时显示活动,那么您可以使用 -

          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
      

      并将此意图设置为您的通知操作 -

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

      【讨论】:

        猜你喜欢
        • 2012-02-11
        • 2015-07-31
        • 1970-01-01
        • 2013-03-14
        • 2014-10-17
        • 2018-08-05
        • 1970-01-01
        • 2012-01-21
        • 1970-01-01
        相关资源
        最近更新 更多