【发布时间】:2015-01-09 20:31:19
【问题描述】:
我想在用户点击通知时自动取消我的通知。以下代码适用于除 Android lollipop 设备之外的所有设备。在 Lollipop 设备中,只有当用户滑动它时才会发出通知。
@SuppressWarnings("deprecation")
public void sendNotification(int id){
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Test")
.setContentText("Jump to next screen")
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true);
mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE;
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, NextActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
resultIntent, 0);
//PendingIntent.FLAG_UPDATE_CURRENT
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// id, if there is a need to update the notification later on.
mNotificationManager.notify(id, mBuilder.build());
Log.v(TAG, "Notification ID value " + id);
//mNotificationManager.cancel(id);
}
这段代码缺少什么?
【问题讨论】:
-
即使我也看到了这个问题。你能找到工作吗?我看到的是,它被删除了,并且通知中心发生了任何刷新。
-
以下。我也卡住了。到处查看,但还没有解决这个问题。我目前正在运行一个计时器并在计时器用完后取消所有通知。
标签: android notifications push-notification android-notifications notificationmanager