单击操作按钮时,我需要取消通知。 当用户单击通知时,它正在工作。
下面是我的代码。我已使用此通知打开消息传递平台。
PendingIntent pendingIntent = PendingIntent.getActivity(this,
(int) Calendar.getInstance().getTimeInMillis() /* Request code */, chooser,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action action =
new NotificationCompat.Action.Builder(
0, "SEND", pendingIntent
)
.build();
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager notifManager = null;
final int NOTIFY_ID = 1002;
//There are hardcoding only for show it's just strings
String name = "my_package_channel";
String id = "my_package_channel_1"; // The user-visible name of the channel.
String description = "my_package_first_channel"; // The user-visible description of the channel.
NotificationCompat.Builder builder;
if (notifManager == null) {
notifManager =
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
}
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = notifManager.getNotificationChannel(id);
if (mChannel == null) {
mChannel = new NotificationChannel(id, name, importance);
mChannel.setDescription(description);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
notifManager.createNotificationChannel(mChannel);
}
builder = new NotificationCompat.Builder(this, id);
pendingIntent = PendingIntent.getActivity(this, 0, sendIntent, 0);
builder.setContentTitle(getString(R.string.app_name)) // required
.setSmallIcon(R.drawable.notification_small_icon) // required
.setContentText(message) // required
.setDefaults(Notification.DEFAULT_ALL)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message))
.addAction(action)
.setAutoCancel(true)
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
.setContentIntent(pendingIntent)
.setTicker(message)
.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
Notification notification = builder.build();
notifManager.notify(NOTIFY_ID, notification);