Android 通知操作按钮点击监听器

标签 android push-notification google-cloud-messaging

我正在开发一个 Activity 策划应用程序,目前我正在开发一项将项目分配给客人的功能。 分配元素后,会向客人推送通知,询问他是否可以携带所需元素。根据用户的输入,一个 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());
}

先谢谢你!

最佳答案

希望对你有帮助

//带按钮的通知示例

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

关于Android 通知操作按钮点击监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36067899/

相关文章:

android - 如何扩展 Android 按钮并使用 xml 布局文件

java - 闹钟服务安卓不工作

google-cloud-messaging - 每个项目的 Google Cloud PubSub 订阅限制

node.js - 这个错误: Falsy value for recipient key 'registrationTokens' 是什么意思

android - 防止因单击操作栏中的向上/主页而关闭 Activity ?

java - 关闭并重新打开应用程序后,Android Canvas 游戏循环中断 onResume()

java - 当设备处于 sleep 模式时,如何在 Android 中的某个特定时间推送本地通知

ios - 不使用互联网、wifi、APNS 和苹果推送通知服务器的推送通知

swift - 如何使用 swift 3 将通知详细信息检索到各个数据元素中

Android - FirebaseApp/Firebase 初始化未启动