gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 00:08:26 25 4
gpt4 key购买 nike

我正在开发一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com