1

I have an app that is supposed to show a push notification but it doesn't this line new NotificationCompat.Builder(this); is canceled out in the code. I'm new to this, how can this work here is the code in FirebaseMessagingService.java class

    @Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    sendNotification(remoteMessage.getNotification().getBody());
}

private void sendNotification(String MessageBody){
 Intent intent= new Intent(this,MainActivity.class);
 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this);
   notificationBuilder.setSmallIcon(R.drawable.ic_launcher_background);
    notificationBuilder.setContentTitle("Hope");
     notificationBuilder.setContentText(MessageBody);
   notificationBuilder.setAutoCancel(true);
   notificationBuilder.setSound(defaultSoundUri);
  notificationBuilder.setContentIntent(pendingIntent);
  NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
  notificationManager.notify(0,notificationBuilder.build());

}

}
Frank van Puffelen
  • 418,229
  • 62
  • 649
  • 645

3 Answers3

1

try to use:

NotificationChannel mChannel = new NotificationChannel("", "", 1);
notificationManager.createNotificationChannel(mChannel);
Vadim Eksler
  • 818
  • 9
  • 22
1

It seems duplicate question of here.

Since Android O (It means your targetSdk is 26 or above), notification will not be showing without notification channel.

This is my working code :)

Context c = getApplicationContext();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

// The id of the channel.
final String CHANNEL_ID = "default";
// The user-visible name of the channel.
final String CHANNEL_NAME = "Default";
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    NotificationChannel defaultChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
    notificationManager.createNotificationChannel(defaultChannel);
}

intent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(c, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(c, CHANNEL_ID)
        .setLargeIcon(your_custom_bitmap)
        .setSmallIcon(R.drawable.ic_notification)
        .setColor(ContextCompat.getColor(c, R.color.notification))
        .setContentTitle(getString(R.string.app_name))
        .setContentText(message)
        .setWhen(System.currentTimeMillis())
        .setSound(defaultSoundUri)
        .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
        .setLights(ContextCompat.getColor(c, R.color.colorAccentLight), 5000, 5000)
        .setAutoCancel(true)
        .setPriority(NotificationCompat.PRIORITY_MAX)
        .setContentIntent(pendingIntent);

notificationManager.notify("myapp", 0, notificationBuilder.build());
wonsuc
  • 2,284
  • 17
  • 25
0

If you push notification from Firebase console, you just get notification display if the app is on background or not running. You can read more at HERE. If you want to receive notification data on onMessageReceived() method, you have to send the notification from a backend