Android Push notification sound plays only when the application is in the foreground, but does not play sound when the application is in the background

I use FCM for push notification below the code to play sound when I receive a notification

public void playNotificationSound() { try { Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); Ringtone r = RingtoneManager.getRingtone(mContext, notification); r.play(); } catch (Exception e) { e.printStackTrace(); } } 

I call this method OnMessageReceived, but the sound is played only when the application is in the foreground, and not played when the application is in the background

  @Override public void onMessageReceived(RemoteMessage remoteMessage) { Log.e(TAG, "From: " + remoteMessage.getFrom()); if (remoteMessage == null) return; // Check if message contains a notification payload. if (remoteMessage.getNotification() != null) { Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody()); handleNotification(remoteMessage.getNotification().getBody()); } // Check if message contains a data payload. if (remoteMessage.getData().size() > 0) { Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString()); try { JSONObject json = new JSONObject(remoteMessage.getData().toString()); handleDataMessage(json); } catch (Exception e) { Log.e(TAG, "Exception: " + e.getMessage()); } } } private void handleNotification(String message) { if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) { // app is in foreground, broadcast the push message Intent pushNotification = new Intent(config.PUSH_NOTIFICATION); pushNotification.putExtra("message", message); LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification); // play notification sound NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext()); notificationUtils.playNotificationSound(); }else if (NotificationUtils.isAppIsInBackground(getApplicationContext())){ // If the app is in background, firebase itself handles the notification NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext()); notificationUtils.playNotificationSound(); } } 
+12
source share
5 answers

Since onMessageReceived() not called when sending a notification object, I created a BroadcastReceiver to handle it when I receive a notification:

 public class NotificationReceiver extends WakefulBroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { playNotificationSound(context); } public void playNotificationSound(Context context) { try { Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); Ringtone r = RingtoneManager.getRingtone(context, notification); r.play(); } catch (Exception e) { e.printStackTrace(); } } 

and added it to the manifest. The recipient is responsible for playing the notification ringtone.

  <receiver android:name=".notification.NotificationReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> </intent-filter> </receiver> 
+9
source

When sending notifications to Android through the Firebase Console, this will be considered as a Notification message. Notification messages will always be processed automatically by the Android device (system tray) when the application is in the background (see Message processing ).

This means that onMessageReceived() will not be called. Therefore, if you intend to always play a sound when you receive a notification, you will have to use Data Message * instead. But you will have to send messages without using the Firebase Console.

+6
source

You need to enable sound in Firebase Notification Composer in the Advanced Settings section. :)

+2
source

private void sendNotification (String messageBody, Intention intent) {

  //intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent/*.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)*/, PendingIntent.FLAG_ONE_SHOT); NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setContentIntent(pendingIntent); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);//<-- Bitmap bitmap = getBitmapfromUrl(postImageUrl); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification_recive) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.notification_recive)) .setContentTitle(postTitle + "") .setStyle(new NotificationCompat.BigPictureStyle() .setSummaryText(postTitle + "") .bigPicture(bitmap)) .setContentText(messageBody) .setLights(getResources().getColor(R.color.blue),1000,1500) .setAutoCancel(true) .setSound(defaultSoundUri)//<-- .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); } 
0
source

You already have the onMessageReceived method, firebase sends the type remoteMessage.getData () when the application is in the background. and remoteMessage.getNotification () will be null. therefore, sound is only played when the application is in the foreground, and not when the application is playing in the background. you need to add

 if (remoteMessage.getData().size() > 0) { Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString()); try { JSONObject json = new JSONObject(remoteMessage.getData().toString()); handleNotification(json.getString("msg");//I am assuming message key is msg handleDataMessage(json); } catch (Exception e) { Log.e(TAG, "Exception: " + e.getMessage()); } } 
-one
source

Source: https://habr.com/ru/post/1013930/


All Articles