【发布时间】:2021-08-02 18:19:47
【问题描述】:
我的应用中有一条通知,其中包含以下代码:
@SuppressLint("RestrictedApi")
void ShowNotification(String title, String text, int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
channelId = getString(R.string.default_notification_channel_id);
String description = getString(R.string.channel_description);
NotificationChannel channel = new NotificationChannel(channelId, "MyNew notification", NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
Intent deleteIntent = new Intent(this, MainActivity.class);
deleteIntent.setAction(Intent.ACTION_DELETE);
deleteIntent.putExtra(EXTRA_NOTIFICATION_ID, 0);
PendingIntent deletePendingIntent = PendingIntent.getService(this, 0, deleteIntent, 0);
// Create pending intent, mention the Activity which needs to be
//triggered when user clicks on notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mNotify = new NotificationCompat.Builder(this, channelId);
mNotify.setLights(color, 100 , 200);
mNotify.setSmallIcon(R.drawable.ic_action_name);
mNotify.setLargeIcon(BitmapFactory. decodeResource (getResources() , R.drawable.ic_action_name)) ;
mNotify.setContentTitle(title);
mNotify.setOngoing(true);
mNotify.setShowWhen(true);
mNotify.setContentText(text);
mNotify.setStyle(new NotificationCompat.BigTextStyle().bigText("Much longer text that cannot fit one line..."));
mNotify.setDefaults(Notification.DEFAULT_SOUND);
mNotify.addAction(R.mipmap.shield3_foreground, "Delete", deletePendingIntent);
mNotify.setContentIntent(contentIntent);//Activity which needs to be triggered when user clicks on notification
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int mId = 1001;
try {
mNotificationManager.notify(mId, mNotify.build());
}
catch (Exception e) { e.printStackTrace(); }
}
我的通知工作正常,但问题是,当我在通知中单击“删除”时,它不会删除通知。
我明白,我必须在我的代码中使用 cancel(),但不知道如何在此处使用它以及在何处使用此方法。我应该怎么做才能在我的通知中使用“删除”?谢谢。
【问题讨论】:
标签: java android firebase notifications