Cordova-plugin-local-notifications: Android 8.0 custom notification sound

Created on 28 Feb 2019  Â·  23Comments  Â·  Source: katzer/cordova-plugin-local-notifications

Provide a general summary of the issue.

Your Environment

  • Plugin version: 0.9.0-beta.2
  • Platform: Android
  • OS version: 8.0.x
  • Device manufacturer / model: Samsung Galaxy s8
  • Cordova version (cordova -v): 4.2.1
  • Cordova platform version (cordova platform ls): 7.1.4
  • Plugin config: none
  • Ionic Version: 3.9.2

Expected Behavior

Using:

this.localNotifications.schedule({
                    id: 1,
                    text: 'You have arrived',
                    sound: 'file://assets/sounds/alerts/arrival.mp3',
                    data: { secret: 'hellloo' },
                    vibrate: true
                });

I want to display a notification with a custom sound
Located in src/assets/sounds/alerts/...

Actual Behavior

I've tried every possible filepath (I think) however it still plays the default Samsung Galaxy s8 notification sound.

Context

Explained above.

Debug logs

Logcat output:

02-28 17:38:06.354  3779  3779 D EdgeLightingManager: showForNotification : isInteractive=true, isHeadUp=false, color=0, sbn = StatusBarNotification(pkg=io.ionic.starter user=UserHandle{0} id=1 tag=null key=0|io.ionic.starter|1|null|10203: Notification(channel=default-channel-id pri=0 contentView=null vibrate=null sound=null tick defaults=0x0 flags=0x11 color=0x00000000 number=0 vis=PUBLIC semFlags=0x0 semPriority=0 semMissedCount=0))
02-28 17:38:06.354  3779  3779 D EdgeLightingPolicyManager: putNotification tickerText:true,titleText:true,text:true,sub:falsecn= + null
02-28 17:38:06.354  3779  3779 D EdgeLightingPolicyManager.NotificationGroup: updateNotificationData put :0|io.ionic.starter|1|null|10203,size=7
02-28 17:38:06.354  3779  3779 D EdgeLightingPolicyManager.NotificationGroup: 0|io.ionic.starter|1|null|10203:NotificationData time=1551371886354,
02-28 17:38:06.354  3779  3779 D EdgeLightingPolicyManager.NotificationGroup: 0|android|40|null|1000:NotificationData time=1551371251595,
02-28 17:38:06.355  3779  3779 D EdgeLightingPolicyManager.NotificationGroup: 0|com.rolflex.mobCSRApp|6|null|10156:NotificationData time=1551291051811,
02-28 17:38:06.355  3779  3779 D EdgeLightingPolicyManager.NotificationGroup: 0|com.wssyncmldm|1|null|1000:NotificationData time=1551349895823,
02-28 17:38:06.355  3779  3779 D EdgeLightingPolicyManager.NotificationGroup: 0|com.google.android.apps.maps|g:atn_group:NotificationData time=1551371775363,
02-28 17:38:06.355  3779  3779 D EdgeLightingPolicyManager.NotificationGroup: 0|com.rolflex.mobCSRApp|-574543954|null|10156:NotificationData time=1551291051833,
02-28 17:38:06.355  3779  3779 D EdgeLightingPolicyManager.NotificationGroup: 0|com.android.vending|874755343|null|10043:NotificationData time=1551314492705,
02-28 17:38:06.355  3779  3779 D EdgeLightingPolicyManager.NotificationGroup: 0|com.android.settings|9|null|1000:NotificationData time=1550852356484,
02-28 17:38:06.355  3779  3779 D EdgeLightingManager: showForNotificationScreenOn : return false by isAcceptableApplication.
02-28 17:38:06.365  3779  3779 I AccessibilityManagerService: semOnLightNotification(null), bIsFlashOn : false
02-28 17:38:06.372  4196  4196 D StatusBar: addNotification key=0|io.ionic.starter|1|null|10203 fullscreen:false
02-28 17:38:06.460  3426 15233 V NuPlayer: onSourceNotify() kWhatFlagsChanged  FLAG_CAN_PAUSE: 1  FLAG_CAN_SEEK_BACKWARD: 1 
02-28 17:38:06.461  3426 15233 V NuPlayer: NuPlayer::onSourceNotify Source::kWhatPrepared source: 0xe7d6dc40

I've noticed that 'sound = null' is being logged, which is kind of weird.
Thanks in advance.

With kind regards,

Jonah

Most helpful comment

I use this version ==> https://github.com/Steffaan/cordova-plugin-local-notifications
for my application it works perfectly on Android 7+ and here is the code I use

var sound = device.platform != 'iOS' ? 'file://audio/adhan.mp3' : 'content://audio/adhan.mp3';
cordova.plugins.notification.local.schedule({
            id:1,
            channel: 'myChannel',
            title: 'my title',
            text:'place here any text',
            at: new Date(),
            sound: sound,
            priority:1,
            count:1
        });

hope this help

All 23 comments

I have the Same issue.. tried everything. Also in beta 3 same thing.

I use this version ==> https://github.com/Steffaan/cordova-plugin-local-notifications
for my application it works perfectly on Android 7+ and here is the code I use

var sound = device.platform != 'iOS' ? 'file://audio/adhan.mp3' : 'content://audio/adhan.mp3';
cordova.plugins.notification.local.schedule({
            id:1,
            channel: 'myChannel',
            title: 'my title',
            text:'place here any text',
            at: new Date(),
            sound: sound,
            priority:1,
            count:1
        });

hope this help

Hi,
Thank you for your answer.

I did not manage to fix it.I dont know what i doing wrong.
Is it possible you to send me an working example?
Or should i send you a test what i have done.
Hope you can help.

Den söndag 3 mars 2019 16:14:43 EET, talal-alkaremie <[email protected]> skrev:

I use this version ==> https://github.com/Steffaan/cordova-plugin-local-notifications
for my application it works perfectly on Android 7+ and here is the code I use
var sound = device.platform != 'iOS' ? 'file://audio/adhan.mp3' : 'content://audio/adhan.mp3';
cordova.plugins.notification.local.schedule({
id:1,
channel: 'myChannel',
title: 'my title',
text:'place here any text',
at: new Date(),
sound: sound,
priority:1,
count:1
});

hope this help

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.

I use this version ==> https://github.com/Steffaan/cordova-plugin-local-notifications
for my application it works perfectly on Android 7+ and here is the code I use

var sound = device.platform != 'iOS' ? 'file://audio/adhan.mp3' : 'content://audio/adhan.mp3';
cordova.plugins.notification.local.schedule({
          id:1,
          channel: 'myChannel',
          title: 'my title',
          text:'place here any text',
          at: new Date(),
          sound: sound,
          priority:1,
          count:1
      });

hope this help

Not working for me, even if it did. It's a work around.

This bug should be fixed.

I think I have the same issue, but it looks like it is ignoring the 'sound: null' parameter as well. There seems to be a problem communicating the sound and vibration correctly on Android, in my experience, it is just ignoring them. Has anyone found any solutions?

My format (I'm using latest katzer version from git):
var sound = device.platform == 'Android' ? 'file://notification.mp3' : 'file://notification.caf';
cordova.plugins.notification.local.schedule({
id: 0,
title: "Canning timer is complete",
text: "Continue to next step",
sound: sound,
vibrate: true,
icon: "file://notify-icon.png",
smallIcon: "res://n_icon.png",
trigger: {at: reminder0}
});
cordova.plugins.notification.local.schedule({
id: 1,
title: "Canning timer",
text: "Next step at " + reminder0.toLocaleTimeString('en-US'),
sound: null,
icon: "file://notify-icon.png",
smallIcon: "res://n_icon.png"
});

I think I have the same issue, but it looks like it is ignoring the 'sound: null' parameter as well. There seems to be a problem communicating the sound and vibration correctly on Android, in my experience, it is just ignoring them. Has anyone found any solutions?

My format (I'm using latest katzer version from git):
var sound = device.platform == 'Android' ? 'file://notification.mp3' : 'file://notification.caf';
cordova.plugins.notification.local.schedule({
id: 0,
title: "Canning timer is complete",
text: "Continue to next step",
sound: sound,
vibrate: true,
icon: "file://notify-icon.png",
smallIcon: "res://n_icon.png",
trigger: {at: reminder0}
});
cordova.plugins.notification.local.schedule({
id: 1,
title: "Canning timer",
text: "Next step at " + reminder0.toLocaleTimeString('en-US'),
sound: null,
icon: "file://notify-icon.png",
smallIcon: "res://n_icon.png"
});

I've not found a solution yet.

Hi again, so I played around with Steffaan's version from above, and I now have it working on Android 8. One thing I did not notice earlier, was that you need to define each channel, much like you did with the id in the Katzer version. So, for example, I changed my notifications from above to have a channel and priority parameter:
cordova.plugins.notification.local.schedule({
id: 1,
channel: 'channel1',
title: "Canning timer",
text: "Next step at " + reminder0.toLocaleTimeString('en-US'),
sound: null,
icon: "file://notify-icon.png",
smallIcon: "res://n_icon.png",
priority: -1
});

      cordova.plugins.notification.local.schedule({
        id: 0,
        channel: 'channel0',
        title: "Canning timer is complete",
        text: "Continue to next step",
        sound: sound,
        vibrate: true,
        icon: "file://notify-icon.png",
        smallIcon: "res://n_icon.png",
        priority: 2,
        trigger: {at: reminder0}
      });

So, I now have one notification that displays, but is silent, and another that is makes sound and vibrates. I'm still testing, but this seems to work for me. It would be nice to pull the Android channel changes into the master.

Same problem in Android 8/9. Also, Push notification plugin has the same problem in background. It seems to be a SO issue than a plugin one

could anyone play custom sound on Android. It works perfect for me on iOS using the following :
cordova.plugins.notification.local.schedule({ title: 'Custom sound', trigger: { every: 'minute', count: 2 }, sound: 'file://statics/daybreak.wav' })

katzer version worked for me only without the custom sound and vibrating.

As @talal-alkaremie suggested, the Steffaan version worked perfectly for me so far. I've tested with an Android 8.1.0 and i had to put the resource-file in config.xml with the right paths to have it working and i'm using Phonegap Build.

As @SaltyPotato questioned before, are there any chances of getting the improvements from Steffaan version to main (katzer) version?

According to android documentation:
->Notice that the NotificationCompat.Builder constructor requires that you provide a channel ID. This is required for compatibility with Android 8.0 (API level 26) and higher, but is ignored by older versions.

->Before you can deliver the notification on Android 8.0 and higher, you must register your app's notification channel with the system by passing an instance....

https://developer.android.com/training/notify-user/build-notification.html

in conclution is need add channel and priority in versions superior 8.0

same problem here. cannot play custom sound on android.

Is there any idea when this is gonna be fixed? I am also not able to set a custom notification sound.

I've checked Android Studio console and I can see this error:

E/Asset: File not found: assets/www/assets/audio/cuckoo-cuckoo-clock.mp3
W/System.err: java.io.FileNotFoundException: www/assets/audio/cuckoo-cuckoo-clock.mp3

image

@jalison-dasilva as other folks said earlier, this one worked better for me, on iOS and also Android:

https://github.com/Steffaan/cordova-plugin-local-notifications

Hi @caioschwartz
Can you please share code here and tell us where to place audio file because i am facing issue in playing custom sound.

Thanks

@talhaZahidch, don't forget to consider, as i said before, that the katzer/cordova-plugin-local-notifications din't work for me. I've used the Steffaan version.

With the Steffaan version i did:

A. Put a .mp3 and .caf file in the root of the project

B. At config.xml

  1. Add <plugin name="https://github.com/Steffaan/cordova-plugin-local-notifications" />
    2 Add <resource-file src="???.mp3" target="app/src/main/res/raw/???.mp3" /> inside <platform name="android">
  2. Add <resource-file src="???.caf" target="www/res/???.caf" /> inside <platform name="ios">

C. Call the method

cordova.plugins.notification.local.schedule( { channel: 'ChannelName', color: '#16A7E0', icon: 'res://icon', id: 3, priority: 2, smallIcon: 'res://ic_dialog_alert', sound: device.platform.toLowerCase() === 'android' ? "res://???.mp3" : "res://???.caf", title: 'Warning', text: 'Time is running out', trigger: { at: dateToPopNotification }, vibrate: true } );

Hey guys after long time testing, I have figured it out using the capacitor local notifications on any version of Android. *To get the sound working you must create a channel

First you need to put the sound.mp3 file that you have inside the raw folder located in the res folder of Android files.

Second you need to create a channel using the plugin and put in the right values, for (sound: 'sound.mp3'). Then schedule a notification with the basic values but don't include the sound param when you schedule the notification. Examples below

//// Creating channel example /////
const channel1: NotificationChannel = {
id: 'mychannel',
name: 'channel',
importance: 5,
sound: 'sound.mp3',
visibility: 1,

}

Important NotificationChannel from capacitor plugins

Plugins.LocalNotification.createchannel(channel1)

//// Scheduling notifications /////
Plugins.LocalNotification.schedule({
notifications {
title: 'example',
id: 1,
channel: 'mychannel' (channel id from above)
body: 'example',
trigger: 'example'

}
})

Third inside the capacitor.config.json file put
Plugins {
LocalNotification {
"smallicon": "icon"
"Iconcolor": "#22222#
"sound": "sound.mp3"
}
}

That's all after that it will be working. Once your notification triggers it will play the custom sound you assigned. If you get confused let me know. Here is the video you can follow to help you understand better.
https://youtu.be/bww4a4B43tM

Hi all, perhaps you can try this fork, it is more recent and still maintained:
https://github.com/timkellypa/cordova-plugin-local-notifications

This fork also includes updates for Android 10.

Please note that some property names are different than that of Steffaan's:
Steffaan: channel and channelDescription.
timkellypa: channelId and channelName.

Hope it helps. Thanks.

Hi @moderntechllc,

I have local notifications working with this cordova plugin and custom sound also for android 10,
but I would like to move to capacitor,
So I am trying your suggestion for custom channel.

Plugins.LocalNotification.createchannel(channel1)

but it gives me an error:

```
Unhandled Promise rejection: not available ; Zone: ; Task: null ; Value: Error: not available
zone-evergreen.js:659 Unhandled Promise rejection: not available ; Zone: ; Task: null ; Value: Error: not available
at Object.fromNative (capacitor-runtime.js:226)
at :1:18 Error: not available
at Object.fromNative (capacitor-runtime.js:230:14)
at :1:18

Do you know what am I doing wrong?

I changed **channel**: `'mychannel'` to **channelId**`: 'mychannel'`

because here core-plugin-definitions.d LocalNotification is defined:

export interface LocalNotification {
title: string;
body: string;
id: number;
schedule?: LocalNotificationSchedule;
/*
* Name of the audio file with extension.
* On iOS the file should be in the app bundle.
* On Android the file should be on res/raw folder.
* Doesn't work on Android version 26+ (Android O and newer), for
* Recommended format is .wav because is supported by both platforms.
*/
sound?: string;
/
*
* Android-only: set a custom statusbar icon.
* If set, it overrides default icon from capacitor.config.json
/
smallIcon?: string;
/
*
* Android only: set the color of the notification icon
/
iconColor?: string;
attachments?: LocalNotificationAttachment[];
actionTypeId?: string;
extra?: any;
/
*
* iOS only: set the thread identifier for notification grouping
/
threadIdentifier?: string;
/
*
* iOS 12+ only: set the summary argument for notification grouping
/
summaryArgument?: string;
/
*
* Android only: set the group identifier for notification grouping, like
* threadIdentifier on iOS.
/
group?: string;
/
*
* Android only: designate this notification as the summary for a group
* (should be used with the group property).
/
groupSummary?: boolean;
/
*
* Android only: set the notification channel on which local notification
* will generate. If channel with the given name does not exist then the
* notification will not fire. If not provided, it will use the default channel.
/
channelId?: string;
/
*
* Android only: set the notification ongoing.
* If set to true the notification can't be swiped away.
/
ongoing?: boolean;
/
*
* Android only: set the notification to be removed automatically when the user clicks on it
*/
autoCancel?: boolean;
}


LocalNotifications.schedule({
notifications: [{
title: 'Last Known Location',
body: 1 + 'testing sound', // 'Latitude: ' + location.coords.latitude + ' Long: ' + location.coords.longitude,
id: 1,
channelId: 'mychannel',
sound: file://assets/audio/${sound}.mp3,
ongoing: true
}]
});
```

@MInesGomes
Do you have capacitor installed? Because that plugins uses the capacitor apis, you need you install the capacitorjs and then import it to your page after that it will work. Unless you don't want to use the capacitorjs then you can use the Cordova plugin in that katzer offers, but I haven't tested out to get the sound and everything working with this Plugin. That's why I used the capacitor plugin with the capacitor framework.

@moderntechllc Yes I have installed. I made a new project to test and installed just capacitor & no cordova
The notification appears. geolocation(capacitor) is working and Backgournd tasks Plugin (capacitor) also.
But custom sound no, and it gives me the error above.

If I comment

Plugins.LocalNotifications.createChannel(channel1);

Doesn't give the error

@MInesGomes
Yes I see that when you schedule your notification the format and values you did it wrong. Please refer back to the examples of how I set up the notifications schedule. You shouldn't include sound value when scheduling a notification rather it should be done in the capacitor.config.json file

Thanks for your help @moderntechllc
I remove the sound but it keeps giving the error in creating the channel here

Plugins.LocalNotifications.createChannel(channel1);

before schedule

Do you have a link to a working code?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MaZZly picture MaZZly  Â·  79Comments

DuaneQ picture DuaneQ  Â·  21Comments

carpiediem picture carpiediem  Â·  25Comments

acianti picture acianti  Â·  126Comments

Ionas83 picture Ionas83  Â·  26Comments