cordova-plugin-firebasex icon indicating copy to clipboard operation
cordova-plugin-firebasex copied to clipboard

Custom sound vs documentation vs multiple Android versions + iOS

Open globules-io opened this issue 4 years ago • 6 comments
trafficstars

Here is the payload that I'm sending through firebase. I have linked my sound in my config and created a channel when the app starts. Both the sound and the channel are named "test". The sound works when the app is in the foreground only.

window.FirebasePlugin.createChannel({id:'test', sound:'test'});

Here's my payload

{"priority":"high","registration_ids":["xxxxxxxx"],"apns":{"payload":{"aps":{"content-available":1,"sound":"test.caf"}}},"data":{"notification_foreground":"true","notification_android_sound":"test"},"android":{"notification":{"icon":"fcm_push_icon","sound":"test","channel_id":"test"}},"notification":{"body":"Some Text","title":"Some Title","icon":"fcm_push_icon"}}"

My config links the sound properly (it works when app in foreground)

<resource-file src="res/sounds/test.mp3" target="app/src/main/res/raw/test.mp3" />

From the rest I ran, here what I get

  • App is not running, notification is received in tray but sound is not played
  • App is running in the background, notification is received in tray but sound is not played
  • App is running in the foreground, notification is received in tray and sound is played

These were on Android 6.0.1 [email protected]

How do I play the custom sound when the notification is received when the app is not in the foreground please (or at least when in the background)? Thanks.

globules-io avatar Nov 26 '20 18:11 globules-io

Figured it out, you might want to update the documentation.

Android < 8 needs payload.notification.sound = "my sound" Android >= 8 needs payload.android.notification.sound = "my sound"

In your doc

{
  "name": "my_notification",
  "notification": {
    "body": "Notification body",
    "title": "Notification title"
  },
  "android": {
    "notification": {
      "channel_id": "my_channel_id",
      "sound": "my_sound"
    }
  }
}

needs to be

{
  "name": "my_notification",
  "notification": {
    "body": "Notification body",
    "title": "Notification title",
    "sound": "my_sound"
  },
  "android": {
    "notification": {
      "channel_id": "my_channel_id",
      "sound": "my_sound"
    }
  }
}

to work on everything

globules-io avatar Nov 26 '20 18:11 globules-io

Also discovered an additional issue. Since the proper payload is as I posted above for Android, if you have a sound on iOS, it won't work...

While this works on Android

{
  "name": "my_notification",
  "notification": {
    "body": "Notification body",
    "title": "Notification title",
    "sound": "my_sound"
  },
  "android": {
    "notification": {
      "channel_id": "my_channel_id",
      "sound": "my_sound"
    }
  }
}

And supposedly, adding the ios apns should works for both iOS and Android


{
	"priority":"high",
	"registration_ids":["xxx"],
	"apns":{
		"payload":{
			"aps":{
				"content-available":1,
				"sound":"test.caf"
			}
		}
	},
	"data":{
		"notification_android_sound":"test",
		"notification_ios_sound":"test.caf"
	},
	"android":{
		"notification":{
			"icon":"fcm_push_icon",
			"sound":"test",
			"channel_id":"test"
		}
	},
	"notification":{
		"body":"test",
		"title":"test",
		"icon":"fcm_push_icon",
		"sound":"test"
	}
}

But it does not because notification.sound overrides everything, which is required to make it work for Android < 8. Now my sound is named test.caf on iOS and test.mp3 on Android and called by its resource name "test" on Android.

If I do a test with

notification.sound = 'test.caf' then I do get the custom sound on iOS. But then I can't get it on Android because test.caf does not exist. Also notification_ios_sound does not seem to have any influence on this.

How can I go around this road block ? Basically down to notification.sound filename vs resource name.

globules-io avatar Nov 26 '20 21:11 globules-io

And I also found a work around so I thought I might share here.

1- Remove file extension off your caf file for iOS and name the same as your Android resource, to have

 <resource-file src="res/sounds/mysound" />  //iOS - remove .caf from your file and here
 <resource-file src="res/sounds/mysound.mp3" target="app/src/main/res/raw/mysound.mp3" /> //Android

2- Now settingpayload.notification.sound = "mysound" works on both Android and iOS for all versions.

The complete working payload should look like

{
	"priority":"high",
	"registration_ids":["xxx"],
	"apns":{
		"payload":{
			"aps":{
				"sound":"test",
                                 ...
			}
		}
	},
	"data":{
		"notification_android_sound":"test",
		"notification_ios_sound":"test"
	},
	"android":{
		"notification":{			
			"sound":"test",
			"channel_id":"test",
                        ...
		}
	},
	"notification":{		
		"sound":"test",
                ...
	}
}

globules-io avatar Nov 26 '20 22:11 globules-io

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar May 12 '21 14:05 stale[bot]

Reopening as stalebot incorrectly closed it

dpa99c avatar Jul 23 '21 11:07 dpa99c

Thank you

globules-io avatar Jul 23 '21 13:07 globules-io