1

Here is my firebase messaging service code file. My service is not called.

public class NotificationService extends FirebaseMessagingService {
    String myTitle, myImage;
    
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        myTitle = remoteMessage.getData().get("title");
        myImage = remoteMessage.getData().get("body");
        Log.d("Service","Hi this is service");
        //bitmap = getBitmapfromUrl(myImage);
        //Toast.makeText(this,"Hi test",Toast.LENGTH_SHORT).show();
        showNotification(myTitle,myImage);
    }

    

    private void showNotification(String myTitle,String myImage) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("Notification_Title", "yes");
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        //Toast.makeText(this,title,Toast.LENGTH_SHORT).show();
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);
        String channelId = "Custom_Notification";
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.drawable.ic_notifications_none_white_24dp)
                        .setContentTitle(myTitle)
                        .setContentText(myImage)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "Custom Notification",
                    NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(channel);
        }
        notificationManager.notify(0, notificationBuilder.build());
    }
}
Shalu T D
  • 3,921
  • 2
  • 26
  • 37

1 Answers1

0

If you are not received any remote message or your service is not working mean, check out the StackOverflow link.

To Create Custom Notification Channel

private void showNotification(String myTitle,String myImage) {
      Intent intent = new Intent(this, MainActivity.class);
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      intent.putExtra("Notification_Title", "yes");

      PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
        PendingIntent.FLAG_ONE_SHOT);

      String channelId = "Custom_Notification";
      Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
      NotificationCompat.Builder notificationBuilder =
      new NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.drawable.ic_notifications_none_white_24dp)
                .setContentTitle(myTitle)
                .setContentText(myImage)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent)
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setAutoCancel(true)
                .setPriority(NotificationCompat.PRIORITY_HIGH);
NotificationManager notificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel channel = new NotificationChannel(channelId,
            "Custom Notification",
            NotificationManager.IMPORTANCE_HIGH);
    notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0, notificationBuilder.build());      }     

Above code is working fine after receiving the remote message. If you want to add any custom layout then use below code

    // Get the layouts to use in the custom notification
    RemoteViews notificationLayout = new RemoteViews(getPackageName(),           R.layout.notification_small);
     RemoteViews notificationLayoutExpanded = new RemoteViews(getPackageName(), R.layout.notification_large);

    // Apply the layouts to the notification
     Notification customNotification = new NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(R.drawable.notification_icon)
            .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
            .setCustomContentView(notificationLayout)
            .setCustomBigContentView(notificationLayoutExpanded)
            .build();

Notification custom layout

Note: In android 10, and above Notification Service is not calling basically in the background. You will receive a notification but custom design it will not reflect. Still, If you want to receive notification in the background also. then listen to the broadcast receiver for firebase message event. that time you have to wake up your app to the foreground.

Yabaze Cool
  • 523
  • 6
  • 16