Как сделать так, чтобы уведомление создавалось только тогда, когда пользователь нажимает на него? - PullRequest
0 голосов
/ 05 мая 2020

Я работаю над уведомлением Firebase. Я успешно получаю уведомление от Firebase. Но теперь я хочу, чтобы уведомление очищалось только в том случае, если пользователь нажимает на него, а не просто нажимает кнопку «Очистить все» на панели уведомлений.

Я пробовал использовать Notification.FLAG_NO_CLEAR и Notification.FLAG_ONGOING_EVENT , но не получил.

Если кто-нибудь знает, как этого добиться, пожалуйста, помогите меня. Заранее благодарим за помощь.

Вот мое настраиваемое уведомление:

CustomNotification. java

public class CustomNotification {

public static void notify(final Context context, String title, String message,
                          PendingIntent contentIntent) {
    Notification notification;
    final NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setDefaults(Notification.DEFAULT_ALL)
            .setSmallIcon( R.drawable.alerter_ic_notifications)
            .setContentTitle(title)
            .setContentText(message)
            .setContentIntent(contentIntent)
            .setOngoing( true )
            .setStyle(new NotificationCompat.InboxStyle()
                    .addLine(message)
                    .setBigContentTitle(title));


    notification = builder.build();
    notification.flags |= Notification.FLAG_NO_CLEAR;
    notification.flags |= Notification.FLAG_ONGOING_EVENT;

    notify(context, builder.build());


}

private static void notify(final Context context, final Notification notification) {
    final NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    assert notificationManager != null;
    notificationManager.notify(1, notification);
}
}

FirebaseMessageHandleService. java

public class FirebaseMessageHandleService extends FirebaseMessagingService {

String TAG = "TAG";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Log.e(TAG, "From: " + remoteMessage.getFrom());
    Map<String, String> data = remoteMessage.getData();
    for (String key : data.keySet())
        Log.e(TAG, "FIRE " + key + " = " + data.get(key));

    if (remoteMessage.getNotification() != null) {
        Log.e(TAG, "FIRE1 " + remoteMessage.getNotification().getTitle());
        Log.e(TAG, "FIRE1 " + remoteMessage.getNotification().getBody());
        Log.e(TAG, "FIRE1 " + remoteMessage.getNotification().getIcon());
        Log.e(TAG, "FIRE1 " + remoteMessage.getNotification().getSound());
    }

    String title = remoteMessage.getNotification().getTitle();
    String message = remoteMessage.getNotification().getBody();

    sendNotification(this, title, message);
}

private void sendNotification(Context context, String title, String message) {
    Intent intent = new Intent(context, HomeActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);

    String channelId  = getString( R.string.default_notification_channel_id);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        // Create channel to show notifications.
        String channelName = getString(R.string.default_notification_channel_name);
        NotificationManager notificationManager =
                getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(new NotificationChannel(channelId,
                channelName, NotificationManager.IMPORTANCE_HIGH));
    }

    CustomNotification.notify(context, title, message, contentIntent);
}

}
...