1

I am facing an issue when I use custom sound notification in Android Pie. Notifications are being notified with same sound when the app is in the foreground. When the app is in the background or killed its notification, sound is changed to default automatically.

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";

    @Override public void onMessageReceived(RemoteMessage remoteMessage) {
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
            makeNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
        }
    }
        // [END receive_message]

    private void makeNotification(String title, String messageBody) {
        Intent intent = new Intent(this, WaiterHandlerActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT);
        Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.notification_icon);
        Bitmap scaled = Bitmap.createScaledBitmap(b, 300, 300, true);
        Uri alarmSound = Uri.parse("android.resource://" + this.getPackageName() + "/" + R.raw.sonar_ping);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(MyFirebaseMessagingService.this)
                        .setContentTitle(getResources().getString(R.string.app_name))
                        .setPriority(NotificationCompat.PRIORITY_MAX).setContentText(title).setAutoCancel(true).setSound(alarmSound)
                        .setSubText(messageBody)
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(title))
                        .setContentIntent(pendingIntent)
                        .setColor(ContextCompat.getColor(this, R.color.colorPrimaryThemeDark));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_ALARM)
                    .build();
            Random r = new Random();
            NotificationChannel channel = new NotificationChannel(r.nextInt() + "",
                    "YOUR_CHANNEL_NAME",
                    NotificationManager.IMPORTANCE_HIGH);
            channel.setSound(alarmSound, audioAttributes);

        }
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            notificationBuilder.setSmallIcon(R.drawable.notification_icon);
            notificationBuilder.setLargeIcon(scaled);
            notificationBuilder.setColor(getResources().getColor(R.color.colorPrimaryThemeDark));
        } else {
            notificationBuilder.setLargeIcon(scaled);
            notificationBuilder.setSmallIcon(R.drawable.notification_icon);
        }
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && notificationManager != null) {
            String channelID = "Your Channel ID";// The id of the channel.
            int importance = NotificationManager.IMPORTANCE_HIGH;
            notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
            NotificationChannel mChannel = new NotificationChannel(channelID, "My_Name", importance);
            AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_ALARM)
                    .build();
            mChannel.setSound(alarmSound, audioAttributes);
            notificationManager.createNotificationChannel(mChannel);
            // Create a notification and set the notification channel.
            Notification notification = notificationBuilder
                    .setChannelId(channelID)
                    .build();
            Random r = new Random();
            notificationManager.createNotificationChannel(mChannel);
            notificationManager.notify(r.nextInt(), notification);
        } else if (notificationManager != null) {
            Random r = new Random();
           NotificationCompat.Builder notificationBuilder = getNotificationBuilder();
            notificationManager.notify(r.nextInt() /* ID of notification                 notificationBuilder.build());
        }
            Random r =new Random();
            notificationManager.notify(r.nextInt());
    }
}
showdev
  • 28,454
  • 37
  • 55
  • 73
  • It's probably worth adding the message payload that you ask FCM to deliver to the device. If that contains a notification element then the behaviour is different if the app is in the foreground. Also, notification channel may or may not be an element. – GregHNZ Sep 18 '19 at 03:38
  • Hope this will help you : https://stackoverflow.com/a/53931291/1318946 – Pratik Butani Sep 18 '19 at 04:55

1 Answers1

0

Try Changing notification importance.

 NotificationChannel channel = new NotificationChannel(EVENT_CHANNEL_ID,
                channelName, NotificationManager.IMPORTANCE_LOW);

This helped me.

Varun
  • 214
  • 1
  • 5