Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 0);
Builder builder = new Notification.Builder(this):
..... code for your notification
builder.setDeleteIntent(pendingIntent);
MyBroadcastReceiver
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
.... code to handle cancel
}
}
0
一个完全刷新的答案(感谢我先生的答案):
1)创建一个接收器来处理滑动到解除事件:
public class NotificationDismissedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int notificationId = intent.getExtras().getInt("com.my.app.notificationId");
/* Your code to handle the event here */
}
}
private NotificationCompat.Builder setNotificationStandardValues(Context context, long when){
String subText = "some string";
NotificationCompat.Builder builder = new NotificationCompat.Builder(context.getApplicationContext());
builder
.setLights(ContextUtils.getResourceColor(R.color.primary) , 1800, 3500) //Set the argb value that you would like the LED on the device to blink, as well as the rate
.setAutoCancel(true) //Setting this flag will make it so the notification is automatically canceled when the user clicks it in the panel.
.setWhen(when) //Set the time that the event occurred. Notifications in the panel are sorted by this time.
.setVibrate(new long[]{1000, 1000}) //Set the vibration pattern to use.
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
.setSmallIcon(R.drawable.ic_white_24dp)
.setGroup(NOTIFY_GROUP)
.setContentInfo(subText)
.setDeleteIntent(createOnDismissedIntent(context))
;
return builder;
}
3 回答
DeleteIntent :DeleteIntent是一个PendingIntent对象,可以与通知关联,并在通知被删除时被触发,以太:
用户特定的操作
用户删除所有通知 .
您可以将Pending Intent设置为广播Receiver,然后执行您想要的任何操作 .
MyBroadcastReceiver
一个完全刷新的答案(感谢我先生的答案):
1)创建一个接收器来处理滑动到解除事件:
2)在清单中添加一个条目:
3)使用未决意图的唯一ID(此处使用通知ID)创建待定意图,因为没有这个,将为每个解雇事件重复使用相同的额外内容:
4) Build 你的通知:
另一个想法:
如果您通常创建通知,则还需要其中一个,两个或三个操作 . 我创建了一个“NotifyManager”,它创建了我需要的所有通知,并且还接收了所有Intent调用 . 所以我可以管理所有的行动,并在一个地方捕捉解雇事件 .
创建deleteIntent使用它(在NotificationManager中):
我用这个来设置删除意图(在NotificationManager中):
最后在同一个NotificationManager中是perform函数: