我正在开发一个活动策划应用程序,我目前正在开发一个将项目分配给访客的功能。 分配项目后,会向客人推送通知,询问他是否可以携带所需物品。根据用户的输入,HTTP请求被发送回服务器,更新项目的状态(带来/不带)。
我有点坚持回应点击通知操作按钮,因为我不太明白它是如何工作的。我设法显示了操作按钮,但无法弄清楚如何响应点击它们。正如我在网上看到的那样,对通知操作按钮的响应由PendingIntent管理,我无法理解它是如何工作的,以及我如何根据点击的按钮使用它来发送HTTP请求。 / p>
我很乐意提供一些帮助和建议。
[这是发送给用户的通知示例。] [
这是通知用户的方法:
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());
}
先谢谢你了!
答案 0 :(得分:0)
您必须为您的操作设置PendingIntent
对象,以便当用户对通知按钮执行任何操作时特定意图被触发。
可以为Activity, Broadcast Receiver or Service
设置PendingIntent。
如果要在用户单击通知按钮时显示活动,则可以使用 -
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);
答案 1 :(得分:0)
我认为这可以帮到你
//使用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);
}