I am trying to create custom notification which data is coming from Firebase cloud messaging and it is working fine if app is running but when app is closed system taking default notification.

So what is solution or alternative for this?

@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        if (remoteMessage.getNotification() != null) {
            Log.e("@@@TAG", "onMessageReceived: " + remoteMessage.getData().toString());
            try {
                if (remoteMessage.getNotification().getImageUrl() != null && !remoteMessage.getNotification().getImageUrl().toString().isEmpty()) {
                    showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(), remoteMessage.getData().get("type"), remoteMessage.getNotification().getImageUrl().toString());
                } else {
                    showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(), remoteMessage.getData().get("type"), "");
                }
            } catch (Exception e) {
                Log.e("@@@TAG", "onMessageReceived Exception : " + e.getMessage());
                e.printStackTrace();
            }
        }
    }
// i added this code in my onMessageReceived 
public void showNotification(String title, String message, String type, String imageUrl) throws IOException {
      
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("type", type);
        String channel_id = "notification_channel";
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, Build.VERSION.SDK_INT < 31 ? PendingIntent.FLAG_ONE_SHOT : PendingIntent.FLAG_IMMUTABLE);

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

                NotificationChannel notificationChannel = new NotificationChannel(channel_id, "web_app", NotificationManager.IMPORTANCE_HIGH);
                NotificationManager notificationManager = getSystemService(NotificationManager.class);
                notificationManager.createNotificationChannel(notificationChannel);

                // Attach the custom Expand layout
                RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_layout);
                notificationLayout.setTextViewText(R.id.notification_text, title);
//                notificationLayout.setTextViewText(R.id.notification_text, message);
                notificationLayout.setBitmap(R.id.notification_thumbnail, "setImageBitmap", getBitmapFromUrl(imageUrl));

                // Attach the custom Init layout
                RemoteViews notificationLayoutInit = new RemoteViews(getPackageName(), R.layout.notification_init_layout);
                notificationLayoutInit.setTextViewText(R.id.notification_text, title);
//                notificationLayout.setTextViewText(R.id.notification_text, message);
                notificationLayoutInit.setBitmap(R.id.notification_thumbnail, "setImageBitmap", getBitmapFromUrl(imageUrl));

                NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channel_id)
                        .setSmallIcon(R.mipmap.app_launcher)
                        .setAutoCancel(true)
                        .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
                        .setOnlyAlertOnce(true)
                        .setContentIntent(pendingIntent)
                        .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
                        .setDefaults(NotificationCompat.DEFAULT_SOUND | NotificationCompat.DEFAULT_VIBRATE)
                        .setPriority(NotificationCompat.PRIORITY_HIGH)
                        .setCustomContentView(notificationLayoutInit)
                        .setCustomBigContentView(notificationLayout);

                // Show the notification
                notificationManager.notify(1, builder.build());

            } else {
                NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channel_id)
                        .setSmallIcon(R.mipmap.app_launcher)
                        .setAutoCancel(true)
                        .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
                        .setOnlyAlertOnce(true)
                        .setContentIntent(pendingIntent)
//                        .setContentTitle(title)
                        .setDefaults(NotificationCompat.DEFAULT_SOUND | NotificationCompat.DEFAULT_VIBRATE)
                        .setPriority(NotificationCompat.PRIORITY_HIGH)
                        .setContentText(title);
                if (imageUrl != null && !imageUrl.isEmpty()) {
                    builder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(getBitmapFromUrl(imageUrl)));
                }
                NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
                notificationManager.notify(0, builder.build());
            }
        }

I want to show my custom notification even if app is closed. Can i use service for that and if yes than how?

1

There are 1 best solutions below

0
Frank van Puffelen On

When the app is not running, notification messages are displayed by the system and not by your application code.

If you always want your application code to handle the display, you should send only data messages.

For more on this, have a look at the Firebase documentation on messages types.