React-native-push-notification: Custom Sound does not work depending model device or the version of Android

Created on 18 Oct 2020  路  8Comments  路  Source: zo0r/react-native-push-notification

Bug

I have the following problem:
On a given device, the customized sound and vibrate works normally, and on another device only the standard sound works and no vibrate never.

Environment info

Device custom sound and vibrate ok: Vernee Mars pro Android 7.0
Device custom sound dont play (play default) and vibrate not work: Xiaomi Mi A2 Lite Android 10.
Using PushNotification.checkPermissions on 2 devices I get: { alert: true }, nothing appears about badge or sound.

System:
OS: Windows 10 10.0.18362
CPU: (4) x64 Intel(R) Core(TM) i5-4570 CPU @ 3.20GHz
Memory: 5.39 GB / 15.88 GB
Binaries:
Node: 12.18.3 - C:\Program Filesnodejsnode.EXE
Yarn: 1.13.0 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
npm: 6.14.6 - C:\Program Filesnodejs\npm.CMD
Watchman: Not Found
SDKs:
Android SDK:
API Levels: 23, 27, 28, 29
Build Tools: 23.0.1, 27.0.3, 28.0.3, 29.0.2, 30.0.2
System Images: android-27 | Google APIs Intel x86 Atom, android-28 | Google APIs Intel x86 Atom
Android NDK: Not Found
Windows SDK: Not Found
IDEs:
Android Studio: Version 4.0.0.0 AI-193.6911.18.40.6626763
Visual Studio: Not Found
Languages:
Java: 1.8.0_201
Python: 2.7.15
npmPackages:
@react-native-community/cli: Not Found
react: 16.13.1 => 16.13.1
react-native: 0.63.3 => 0.63.3
react-native-windows: Not Found
npmGlobalPackages:
react-native: Not Found

Library version: x.x.x
react-native-push-notification": "^6.1.1",

Reproducible sample code

PushNotification.channelExists('channel-push-rotina', (exists) => {
      if (!exists) {
        PushNotification.createChannel({
          channelId: 'channel-1489', // (required)
          channelName: 'PushChanel', // (required)
          vibrate: true,
          // soundName: 'alertsound2', // (optional) See `soundName` parameter of `localNotification` function
        });
      }
    });
  function sendNotification() {
    PushNotification.localNotification({
      title: '14:00 - TITLE HERE',
      channelId: 'channel-push-rotina',
      message: 'You pushbutton ou push the nn button!',
      smallIcon: 'ic_notification',
      largeIcon: 'ic_launcher',
      soundName: 'alertsound2',
      playSound: true,
      vibrate: true,
      vibration: 1000,
      autoCancel: false,
      actions: ['close'],
    });
  }

Most helpful comment

sorry for my english, i speak english as well as i'm a programmer :)

Sorry the code I sent didn't really match what I had, I probably had already tried so much that when posting my example I was wrong to post with the commented line.

So ... you're right, after switching libs when trying to build, I got other errors that the original lib didn't occur, so I went back, and decided to do more tests, and realized that for my project I really needed to create 2 channels, 1 with the default sound and one with the custom sound, and there it worked beautifully.

Sorry for taking your time.
Below the code that worked for me.

PushNotification.channelExists('rn-push-notification-channel', (exists) => { if (!exists) { PushNotification.createChannel({ channelId: 'rn-push-notification-channel', // (required) channelName: 'PushChanelDefault', // (required) channelDescription: 'Canal de notifica莽玫es padr茫o', // (optional) default: undefined. vibrate: true, }); } }); PushNotification.channelExists( 'rn-push-notification-channel-alert', (exists) => { if (!exists) { PushNotification.createChannel({ channelId: 'rn-push-notification-channel-alert', // (required) channelName: 'PushChanelAlert', // (required) channelDescription: 'Canal de notifica莽玫es Alarme', // (optional) default: undefined. vibrate: true, soundName: 'my_sound.mp3', // (optional) See `soundName` parameter of `localNotification` function }); } }, );

function sendNotification() { PushNotification.localNotification({ title: '14:00 - T脥TULO AQUI', channelId: 'rn-push-notification-channel-alert', message: 'You pushbutton ou push the nn button!', smallIcon: 'ic_notification', largeIcon: 'ic_launcher', soundName: 'my_sound.mp3', playSound: true, vibrate: true, vibration: 1000, autoCancel: false, actions: ['Fechar'], }); }

All 8 comments

following.....

@fieldesigner how you know that custom sound is working ? if have a same issue 1706

@fieldesigner how you know that custom sound is working ? if have a same issue 1706

In the soundName try: just 'notif' without .mp3
For me in Android 7.0 works, in Android 10 not work

I changed my notifications lib to https://github.com/MaximusBaton/react-native-push-notification
Is just a fork the react-native-push-notification original, but works for me.
Unfortunatelyi dont cant try in Android 7 becose the devide broken yesterday

Hi,
Moving to a fork not maintained is not a better solution.
I don't understand your issue.

What are you doing in you "reproducible" example ?

You create a channel with the default sound:

          // soundName: 'alertsound2', // (optional) See `soundName` parameter of `localNotification` function

Then you trigger the local notification with another sound:

      soundName: 'alertsound2',
      playSound: true,

You must set the same value to get the sound on both Android version. If you change this, you can't expect it to work.

Also:
https://github.com/zo0r/react-native-push-notification#channel-management-android

Once the channel is created, the channel cannot be update. Make sure your channelId is different if you change these options.

Which means if you create the channel without the custom sound and make the change after, the sound will not be update.
https://developer.android.com/reference/android/app/NotificationManager#createNotificationChannel(android.app.NotificationChannel)

Creates a notification channel that notifications can be posted to. This can also be used to restore a deleted channel and to update an existing channel's name, description, group, and/or importance.
The name and description should only be changed if the locale changes or in response to the user renaming this channel. For example, if a user has a channel named 'John Doe' that represents messages from a 'John Doe', and 'John Doe' changes his name to 'John Smith,' the channel can be renamed to match.
The importance of an existing channel will only be changed if the new importance is lower than the current value and the user has not altered any settings on this channel.
The group an existing channel will only be changed if the channel does not already belong to a group. All other fields are ignored for channels that already exist.

Also check 6.1.2 for vibration.

sorry for my english, i speak english as well as i'm a programmer :)

Sorry the code I sent didn't really match what I had, I probably had already tried so much that when posting my example I was wrong to post with the commented line.

So ... you're right, after switching libs when trying to build, I got other errors that the original lib didn't occur, so I went back, and decided to do more tests, and realized that for my project I really needed to create 2 channels, 1 with the default sound and one with the custom sound, and there it worked beautifully.

Sorry for taking your time.
Below the code that worked for me.

PushNotification.channelExists('rn-push-notification-channel', (exists) => { if (!exists) { PushNotification.createChannel({ channelId: 'rn-push-notification-channel', // (required) channelName: 'PushChanelDefault', // (required) channelDescription: 'Canal de notifica莽玫es padr茫o', // (optional) default: undefined. vibrate: true, }); } }); PushNotification.channelExists( 'rn-push-notification-channel-alert', (exists) => { if (!exists) { PushNotification.createChannel({ channelId: 'rn-push-notification-channel-alert', // (required) channelName: 'PushChanelAlert', // (required) channelDescription: 'Canal de notifica莽玫es Alarme', // (optional) default: undefined. vibrate: true, soundName: 'my_sound.mp3', // (optional) See `soundName` parameter of `localNotification` function }); } }, );

function sendNotification() { PushNotification.localNotification({ title: '14:00 - T脥TULO AQUI', channelId: 'rn-push-notification-channel-alert', message: 'You pushbutton ou push the nn button!', smallIcon: 'ic_notification', largeIcon: 'ic_launcher', soundName: 'my_sound.mp3', playSound: true, vibrate: true, vibration: 1000, autoCancel: false, actions: ['Fechar'], }); }

Nice to see it works !

This is probably because the sound was not set the first time the code run, then the channel wasn't updated.

It would take me too much time for now, but there is some improvement needed in the documentation.
Too much important information... 馃槃

Was this page helpful?
0 / 5 - 0 ratings

Related issues

uendar picture uendar  路  3Comments

nidzovito picture nidzovito  路  3Comments

NiuQiaoling picture NiuQiaoling  路  3Comments

Kiran0791 picture Kiran0791  路  3Comments

nguyenvanphuc2203 picture nguyenvanphuc2203  路  3Comments