havesource / cordova-plugin-push

Register and receive push notifications
MIT License
149 stars 284 forks source link

Custom sound not playing when app is in the background or closed #73

Open hainakaron opened 3 years ago

hainakaron commented 3 years ago

Bug Report

The custom sound is not playing when a notification arrives

Expected Behaviour

when the app is closed or in the background, the custom sound should play when a notification arrives

Actual Behaviour

no sound is played when a notification arrives

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

Android 10

(Android) Device Vendor (e.g. Samsung, HTC, Sony...)

Android Studio Emulator (Pixel 3A XL)

cordova info Printout

Cordova Packages:

cli: 10.0.0
    common: 4.0.2
    create: 3.0.0
    lib: 10.0.0
        common: 4.0.2
        fetch: 3.0.0
        serve: 4.0.0

Project Installed Platforms:

android: 9.0.0

Project Installed Plugins:

cordova-clipboard: 1.3.0
cordova-plugin-dialogs: 2.0.2
havesource-cordova-plugin-push: 1.0.0

Environment:

OS: macOS Catalina 10.15.4 (19E287) (darwin 19.4.0) x64
Node: v12.14.1
npm: 6.13.4

android Environment:

android:

ERROR: Command failed with ENOENT: android list target spawn android ENOENT

Project Setting Files:

config.xml:

<?xml version='1.0' encoding='utf-8'?>

Sample
package.json:

--- Start of Cordova JSON Snippet --- { "platforms": [ "android", "ios" ], "plugins": { "cordova-plugin-dialogs": {}, "cordova-clipboard": {}, "@havesource/cordova-plugin-push": {}, "havesource-cordova-plugin-push": { "ANDROID_SUPPORT_V13_VERSION": "28.0.0", "FCM_VERSION": "18.+" } } } --- End of Cordova JSON Snippet ---

Sample Push Data Payload

$data = [ 'title' => 'sample', 'message' => 'sample message', 'soundname' => 'bing', 'url' => 'www.google.com' ]; $body = [ 'registration_ids' => [ 'cXR4I8wBP34:APA91bFOjjd9xDcPMy4Ry7G2NE_C8exE2c5y1WK7MiQsC-C2J0dDEGBmnRdZwrn0BLlnczip7PgF80D_Tch78mNytIeyQHbXiNEexEW0xInMW7-U3JXu063T6ZL-bVpLC8-oExppSMOw' ], 'data' => $data 'android' => [ 'priority' => 'high' ] ];

Logs taken while reproducing problem

2020-12-25 17:37:25.105 19125-19317/com.sample D/Push_FCMService: onMessage - from: 564140600537 2020-12-25 17:37:25.106 19125-19317/com.sample D/Push_FCMService: sender id = 564140600537 2020-12-25 17:37:25.106 19125-19317/com.sample D/Push_FCMService: normalize extras 2020-12-25 17:37:25.106 19125-19317/com.sample D/Push_FCMService: key = sound 2020-12-25 17:37:25.106 19125-19317/com.sample D/Push_FCMService: replace key sound with sound 2020-12-25 17:37:25.106 19125-19317/com.sample D/Push_FCMService: key = title 2020-12-25 17:37:25.106 19125-19317/com.sample D/Push_FCMService: replace key title with title 2020-12-25 17:37:25.107 19125-19317/com.sample D/Push_FCMService: no locale found for key = title, error Value sample of type java.lang.String cannot be converted to JSONObject 2020-12-25 17:37:25.107 19125-19317/com.sample D/Push_FCMService: key = message 2020-12-25 17:37:25.107 19125-19317/com.sample D/Push_FCMService: replace key message with message 2020-12-25 17:37:25.107 19125-19317/com.sample D/Push_FCMService: no locale found for key = message, error Value trying of type java.lang.String cannot be converted to JSONObject 2020-12-25 17:37:25.107 19125-19317/com.sample D/Push_FCMService: background 2020-12-25 17:37:25.107 19125-19317/com.sample D/Push_FCMService: message =[trying again for testing] 2020-12-25 17:37:25.107 19125-19317/com.sample D/Push_FCMService: title =[sample] 2020-12-25 17:37:25.108 19125-19317/com.sample D/Push_FCMService: contentAvailable =[null] 2020-12-25 17:37:25.108 19125-19317/com.sample D/Push_FCMService: forceStart =[null] 2020-12-25 17:37:25.108 19125-19317/com.sample D/Push_FCMService: create notification 2020-12-25 17:37:25.109 19125-19317/com.sample E/Push_FCMService: Number format exception - Error parsing notId: s == null 2020-12-25 17:37:25.118 19125-19317/com.sample D/Push_FCMService: Using channel ID = PushPluginChannel 2020-12-25 17:37:25.120 19125-19317/com.sample D/Push_FCMService: stored icon=null 2020-12-25 17:37:25.120 19125-19317/com.sample D/Push_FCMService: stored iconColor=null 2020-12-25 17:37:25.120 19125-19317/com.sample D/Push_FCMService: stored sound=true 2020-12-25 17:37:25.120 19125-19317/com.sample D/Push_FCMService: stored vibrate=true 2020-12-25 17:37:25.120 19125-19317/com.sample D/Push_FCMService: no icon resource found - using application icon 2020-12-25 17:37:25.121 19125-19317/com.sample D/Push_FCMService: android.resource://com.sample/raw/bing 2020-12-25 17:37:25.123 19125-19317/com.sample D/Push_FCMService: create actions: with in-line

hainakaron commented 3 years ago

Update: I got it working by defining the sound name in the init function like below:

var push = PushNotification.init({ 'android': { 'senderID': ANDROID_TOKEN, 'sound': 'fanfare', //fanfare.wav 'vibrate': 'true' } });

However, with this setup, I could not change the custom sound to another without reinstalling the app.

bkervaski commented 3 years ago

Confused ... does the plugin support custom push notification sounds? I've had no luck.

Same as you but Pixel 5 (fully updated).

I have a sound file -> res/raw/sound.mp3

This is my test data from PHP, the notification goes through just fine. I've tried "sound" and "soundfile" as well as just the filename without ".mp3" and with. Also the full path. No luck.

Advice appreciated :)

$fields = [ 'registration_ids' => [ $device, ], 'data' => [ "title" => 'Some Title', "message" => 'Some Message', "soundname" => "sound.mp3", ], ];

hainakaron commented 3 years ago

I am not sure if the plugin still supports custom push notification sound if the sound is set on the server-side. On the update I posted, the fix was to set it on the mobile-side by defining the value of the 'sound' parameter when calling the PushNotification.init. However, the downside of this "fix" is that you can only set the custom sound once. If you want to change the custom sound, you will have to uninstall the app, reset the value of the 'sound' parameter, and then install the new version again.

mickjol commented 3 years ago

Any news ? Since the custom notification sound set on server-side work on IOS. I think it should work on Android.

bkervaski commented 3 years ago

Same issue here, something to do with the new push notification channels for firebase. Even defining a the default channel in strings.xml no luck. Any additional insight?

pwebdev commented 2 years ago

tambem estou a procura.

RJoshi1981 commented 2 years ago

I have mentioned sound file name in the data payload whereas it is not working. Can you please help me