phonegap / phonegap-plugin-push

Register and receive push notifications
MIT License
1.94k stars 1.91k forks source link

No custom sound for Android 8 #2492

Open jmrvm801 opened 6 years ago

jmrvm801 commented 6 years ago

Expected Behaviour

In Android < 8 the custom sound is played normally

Actual Behaviour

In Android >= 8 instead of play the custom sound is play the default one.

Reproduce Scenario (including but not limited to)

//To Android 8 Channels

PushNotification.createChannel(() => {},() => {},
        {
            id: 'channel1',
            description: 'Broadcast Emergency',
            importance: 5,
            vibration: true,
            soundname : 'alert_android' //The file is in res/raw on my APK
        });

Steps to Reproduce

Send a FCM like this:

{
    "registration_ids": [""],
    "data"            : {
        "message"           : "Test message",
        "title"             : "Title",
        "soundname"         : "alert_android", //It works on Android < 8
        "android_channel_id": "channel1", //For Android 8
    }
}

Platform and Version (eg. Android 5.0 or iOS 9.2.1)

Android 8.0 Android 7.1.1

(Android) What device vendor (e.g. Samsung, HTC, Sony...)

Sony Xperia Z5 (Android 7.1.1) Sony Xperia XZ2 (Android 8.0)

Cordova CLI version and cordova platform version

cordova --version                                   7.1.0
cordova platform version android                    6.3.0

Plugin version

cordova plugin version | grep phonegap-plugin-push   .1.3

Sample Push Data Payload

{
    "registration_ids": [""],
    "data"            : {
        "message"           : "Test message",
        "title"             : "Title",
        "soundname"         : "alert_android", //It works on Android < 8
        "android_channel_id": "channel1", //For Android 8
    }
}

Sample Code that illustrates the problem

        var push = PushNotification.init({
            android: {
                forceShow : true,
                sound: true
            },
            ios: {
                alert: true,
                badge: true,
                sound: true
            }
        });
        push.on('registration', (data) => {
            idFirebase = data.registrationId;
            var oldRegId = localStorage.getItem('registrationId');
            if (oldRegId !== data.registrationId) {
                localStorage.setItem('registrationId', idFirebase);
            }
        });
        push.on('notification', (data) => {
            navigator.notification.alert(data.message, null, data.title, 'Ok');
        });
        push.on('error', (e) => {
            console.log('error');
        });
        PushNotification.createChannel(() => {},() => {},
        {
            id: 'channel1',
            description: 'Broadcast Emergency',
            importance: 5,
            vibration: true,
            soundname : 'alert_android'
        });

Logs taken while reproducing problem

No log was recorded.

KirbySSmith commented 6 years ago

When creating the channel, set “sound” not “soundname”. https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/API.md#pushnotificationcreatechannelsuccesshandler-failurehandler-channel

jmrvm801 commented 6 years ago

That's right. thank you. I'd read about that the soundname and some was equal on Firebase and I though that it was the same case.

macdonst commented 6 years ago

@jmrvm801 you are not the first person to make that mistake. I should make it so either label works.

ahrherrera commented 5 years ago

Hi OP, How did you added the mp3 to the Android Project?

I have prblems adding it. I sent the sound name from GCM and I received the soundname in app, but It plays only the phone default notification.

kaiesh commented 5 years ago

I spent ages trying to solve this, so in the hope that this helps others, I will summarise how I got this to work:

Note: I use the PhoneGap Build cloud service for creation of my APKs and IPAs.

I added these lines to my config.xml that seemed to make the difference:

<platform name="android">
   ...
   <resource-file src="resources/common/sounds/mysound.mp3" target="res/raw/mysound.mp3" />
   ...
</platform>
<preference name="android-build-tool" value="gradle" />
<plugin name="phonegap-plugin-push" source="npm" spec="2.0.0">
   <param name="SENDER_ID" value="12345678" />
</plugin>
...

The resources/common/sounds/mysound.mp3 refers to my local ZIP file structure, and the res/raw/ destination directory for the file. The below screenshot makes this clearer: image

If you then open your built APK in a ZIP reader you should see your sound file(s) under the res/raw/ directory in the APK file. This means your sounds are now accessible to your native app container, and the rest depends on the messages you send from FCM.

_Side note: Obviously the SENDERID value has to be changed from 12345678 to whatever Google gives you.

jmrvm801 commented 5 years ago

This is my config.xml for Android. iOS needs caf files:

`

`

This is for iOS

`

    <resource-file src="notificacion.caf" />
    <resource-file src="simulacro.caf" />
    <resource-file src="sismo.caf" />
</platform>`

This is my Cordova version:

`

` My file's structure:
project/ project/alert_android.caf project/notification.caf project/simulacro.caf project/sismo.caf project/www/alert_android.mp3 project/www/notification.mp3 project/www/simulacro,mp3 project/www/sismo.mp3

For Android > 8 I just implemented the following sentences:

PushNotification.createChannel(() => {},() => {},{ id: 'adnsuiteChannel2', description: 'ADNSuite Broadcast Emergency', importance: 5, vibration: true, sound : 'alert_android' }); PushNotification.createChannel(() => {},() => {},{ id: 'adnsuiteChannel3', description: 'ADNSuite Broadcast PRE-Emergency', importance: 5, vibration: true, sound : 'sismo' }); PushNotification.createChannel(() => {},() => {},{ id: 'adnsuiteNotifySound', description: 'ADNSuite Broadcast Notification V2', importance: 5, vibration: true, sound : 'notificacion' }); PushNotification.createChannel(() => {},() => {},{ id: 'adnsuiteSimulacro', description: 'ADNSuite Broadcast Drill', importance: 5, vibration: true, sound : 'simulacro' });