mar*_*331 7 notifications android
我在我的通知中添加了两个操作按钮,当我点击其中任何一个时,他们执行所需的操作,但通知仍保留在我的通知抽屉中.我知道,当点击操作按钮时,可以从通知抽屉中删除通知,因为Gmail的功能就是这样.如果我单击主要通知,则会打开应用程序并从通知抽屉中删除通知.
这是我的代码片段:
Intent completeIntent = new Intent(getApplicationContext(), MarkComplete.class);
completeIntent.putExtra("rowid", inrowid);
completeIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
PendingIntent markCompleteIntent = PendingIntent.getActivity(getApplicationContext(), inrow, completeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title")
.setContentText("text")
.setContentIntent(notificationReleaseIntent)
.setPriority(Notification.PRIORITY_HIGH)
.setAutoCancel(true)
.addAction(R.drawable.complete, "Mark Complete", markCompleteIntent);
Run Code Online (Sandbox Code Playgroud)
编辑 - 正如zionpi指出我需要调用notification.cancel(); 单击addAction按钮后删除通知.我只是将这个方法添加到PendingIntent指向的类中.
public static void CancelNotification(Context ctx, int notifyId) {
String s = Context.NOTIFICATION_SERVICE;
NotificationManager mNM = (NotificationManager) ctx.getSystemService(s);
mNM.cancel(notifyId);
}
Run Code Online (Sandbox Code Playgroud)
因此,您要删除通知,初步是要有通知ID.
然后调用类似下面的方法来消除它.
public static void CancelNotification(Context ctx, int notifyId) {
String s = Context.NOTIFICATION_SERVICE;
NotificationManager mNM = (NotificationManager) ctx.getSystemService(s);
mNM.cancel(notifyId);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5011 次 |
| 最近记录: |