如何在 android studio 上禁用清理或删除通知
How to disable clean or remove notification on android studio
我正在使用 firebase 推送通知。所以在我的项目中,如果我点击一个通知,它应该打开一个调用“UserDetailsActivity”的界面。但我想在打开“UserDetailsActivity”之前禁用删除或清除通知。
这是我的代码:
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d(TAG, "From: " + remoteMessage.getFrom());
super.onMessageReceived(remoteMessage);
Log.d("msg", "onMessageReceived: " + remoteMessage.getData().get("message")+"hey "+remoteMessage.getSentTime());
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
if(remoteMessage.getData().get("title").equals("Pagamento")){
HashMap<String,String> map = new HashMap<String,String>();
map.put("title",remoteMessage.getData().get("title"));
map.put("Parametro1",remoteMessage.getData().get("Parametro1"));
String Parametro1 = map.get("Parametro1");
String[] seperater =Parametro1.split(":");
String orderId =seperater[1];
map.remove("Parametro1");
map.put("orderId",orderId);
Intent intent = new Intent(this, UserDetailsActivity.class);
intent.putExtra("pushNotification",map);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
String channelId = "Default";
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_push_notificatioin)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody()).setAutoCancel(true).setContentIntent(pendingIntent);;
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
}
manager.notify((int) remoteMessage.getSentTime(), builder.build());
stopForeground(true);
startService(new Intent(getApplicationContext(),UserDetailsActivity.class));
Log.d(TAG, "Message data payload:1 " );
}
}
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
startService(new Intent(getApplicationContext(),UserDetailsActivity.class));
}
}
要使用无法删除的通知,我必须添加新的第一行 notificationCompat.Bilder,它是:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_push_notificatioin)
.setOngoing(true)
**如果设置“setOngoing”通知为真则用户无法删除它
我正在使用 firebase 推送通知。所以在我的项目中,如果我点击一个通知,它应该打开一个调用“UserDetailsActivity”的界面。但我想在打开“UserDetailsActivity”之前禁用删除或清除通知。
这是我的代码:
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d(TAG, "From: " + remoteMessage.getFrom());
super.onMessageReceived(remoteMessage);
Log.d("msg", "onMessageReceived: " + remoteMessage.getData().get("message")+"hey "+remoteMessage.getSentTime());
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
if(remoteMessage.getData().get("title").equals("Pagamento")){
HashMap<String,String> map = new HashMap<String,String>();
map.put("title",remoteMessage.getData().get("title"));
map.put("Parametro1",remoteMessage.getData().get("Parametro1"));
String Parametro1 = map.get("Parametro1");
String[] seperater =Parametro1.split(":");
String orderId =seperater[1];
map.remove("Parametro1");
map.put("orderId",orderId);
Intent intent = new Intent(this, UserDetailsActivity.class);
intent.putExtra("pushNotification",map);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
String channelId = "Default";
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_push_notificatioin)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody()).setAutoCancel(true).setContentIntent(pendingIntent);;
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
}
manager.notify((int) remoteMessage.getSentTime(), builder.build());
stopForeground(true);
startService(new Intent(getApplicationContext(),UserDetailsActivity.class));
Log.d(TAG, "Message data payload:1 " );
}
}
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
startService(new Intent(getApplicationContext(),UserDetailsActivity.class));
}
}
要使用无法删除的通知,我必须添加新的第一行 notificationCompat.Bilder,它是:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_push_notificatioin)
.setOngoing(true)
**如果设置“setOngoing”通知为真则用户无法删除它