简体   繁体   中英

No sound when receiving push notification

i am using react-native-firebase v6 to handle the push notification. The problem is there is no sound played when the push notification received.

I have create a channel and set it to high priority since the default channel created by react-native-firebase has no sound. Here the code i used in the MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {    
    NotificationChannel notificationChannel = new NotificationChannel("notifid", "App notification channel", NotificationManager.IMPORTANCE_HIGH);
    notificationChannel.setShowBadge(true);
    notificationChannel.setDescription("custom notification channel");
    notificationChannel.enableVibration(true);
    notificationChannel.enableLights(true);
    notificationChannel.setVibrationPattern(new long[]{400, 200, 400});
    //notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
    NotificationManager manager = getSystemService(NotificationManager.class);
    manager.createNotificationChannel(notificationChannel);
  }
}

And here is the code to change the channel in firebase.json

{
  "react-native": {
    "messaging_android_notification_channel_id": "notifid"
  }
}

But the sound still not playing when the notification received.

You need to set sound through setSound method. Using NotificationChannel class you can set sound for each channel separately and get it played on receiving particular notification. According to the docs

Sets the sound that should be played for notifications posted to this channel and its audio attributes. Notification channels with an importance of at least NotificationManager#IMPORTANCE_DEFAULT should have a sound. Only modifiable before the channel is submitted to NotificationManager#createNotificationChannel(NotificationChannel).

You can either use system sound or get your custom sound place inside res folder and call it in the method signature accordingly. Just add the following line of code to set sound:

notificationChannel.setSound("sound.mp3",AttributesToSet);

Here AttributesToSet refers to AudioAttributes class which you can use to provide further information about your sound file.

UPDATE: To use default notification sound, you can use following snippet.

Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); //define this before building notification channel

Then use this uri in setSound method as: notificationChannel.setSound(notification);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM