0

I want to play custom notification tone on firebase onMessage receive method, custom sound play only when the app is running, when app destroys system notification default sound play, I want to play custom notification when app destroyed or mobile locked firebase onMessage receive method.

Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
    Uri sound = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.door_bell);

    RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE, sound);

    Notification mNotification = new Notification.Builder(this)
            .setContentTitle("New Post!")
            .setContentText("Here's an awesome update for you!")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(pIntent)
            .addAction(R.mipmap.ic_launcher, "View", pIntent)
            .addAction(0, "Remind", pIntent)
            .setSound(sound)
            .build();

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    if (notificationManager != null) {
        notificationManager.notify(0, mNotification);
    }
    startActivity(new Intent(getApplicationContext(), MainActivity.class));
Tanveer Munir
  • 1,956
  • 1
  • 12
  • 27
  • Can you try the added edit from this answer?: https://stackoverflow.com/a/31398504/2232127 – JensV Apr 11 '19 at 06:28

1 Answers1

0

If you want to use your custom Notification when app destroyed.
Make sure your firebase data have format like

{
   ... 
   "data":{
     "title":"New Notification",
     "body":"Test 123"
   },
   ...
}

instead of

{
   ...
   "notification":{
     "title":"New Notification",
     "body":"Test 123"
   },
   ...
} (if you use this way, phone will display DEFAULT Notification instead of YOUR CUSTOM NOTIFICATION when your app destroyed)
Linh
  • 57,942
  • 23
  • 262
  • 279