Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notification channel capability so sounds will play in android oreo + #1026

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

markterrill
Copy link
Contributor

Oreo requires use of a notification channel. Once a channel is created,
it can’t be modified (could be deleted but that’s unverified as a good
idea).
Consequently you need to create a channel for your app plus sound
combination and then use that.

Took a while to figure out an update. Need to use notificationChannel
for a sound. Have created an app specific notification channel ID as
well as a sound specific channel name. You apparently can’t update a
notification channel after its created, using default channel etc just
meant you used the system default and couldn’t change it.
Oreo requires use of a notification channel. Once a channel is created,
it can’t be modified (could be deleted but that’s unverified as a good
idea).
Consequently you need to create a channel for your app plus sound
combination and then use that.
@ellecito
Copy link

Hi.

I'm using your folk but I can't change the defaut sound.

@markterrill
Copy link
Contributor Author

Hi Ellecito, whats your payload?

Run the adb debug to see whether the payload is triggering playing a sound, and whether the file is found etc etc.

adb logcat -s "browser","chromium","Push_Plugin","FirebasePlugin","NotificationService","Notification"

@ellecito
Copy link

ellecito commented Apr 5, 2019

Hi, there is the payload after send a push test.

04-05 17:49:03.479 1690 1690 E NotificationService: Muting recently noisy 0|com.kinetta.pitzappnew|0|FCM-Notification:9458141|10167

@davidpadych
Copy link

Will it be compatible with cordova 6.3.0?

@markterrill
Copy link
Contributor Author

markterrill commented Apr 18, 2019 via email

@markterrill
Copy link
Contributor Author

Hi team, @robertarnesson . I've gone further with this pull request. It now enables you to create multiple notification channels at runtime and then specify which notification channel to use. Works on Android 9 & 10.

Here is an example GCM data payload (I'm sending via Google Cloudfunctions, ie admin.messaging().sendtoDevice(tokenList, payload, options).

{"data":{"body":"Pit is too HOT","message":"Pit is too HOT","sound":"trainwhistle","notId":"2347","title":"Too Hot  Pit is 331.01F","notification_foreground":"true","notification_body":"Pit is too HOT","notification_title":"Too Hot  Pit is 331.01F","notification_android_sound":"trainwhistle","notification_android_priority":"2","notification_android_visibility":"1","notification_android_channel_id":"smartfirebbq_trainwhistle","notification_android_patch_channel":"true","notification_android_vibrate":"500, 200, 500","notification_android_lights":"#ffff0000, 250, 250"}}

Key thing for Android9/10 is the 'notification_android_channel_id.

Back in javascript land, I am setting everything easily up with this following code. I create a default channel then I loop through the other 'sounds' that I want to offer to users:

// Define custom  channel - all keys are except 'id' are optional.
                let channel  = {
                    // channel ID - must be unique per app package
                    id: "smartfirebbq_channel",

                    // Channel description. Default: empty string
                    description: "Smartfire BBQ alerts",

                    // Channel name. Default: empty string
                    name: "Smartfire BBQ alerts",

                    //The sound to play once a push comes. Default value: 'default'
                    //Values allowed:
                    //'default' - plays the default notification sound
                    //'ringtone' - plays the currently set ringtone
                    //'false' - silent; don't play any sound
                    //filename - the filename of the sound file located in '/res/raw' without file extension (mysound.mp3 -> mysound)
                    sound: "sonar",

                    //Vibrate on new notification. Default value: true
                    //Possible values:
                    //Boolean - vibrate or not
                    //Array - vibration pattern - e.g. [500, 200, 500] - milliseconds vibrate, milliseconds pause, vibrate, pause, etc.
                    vibration: true,

                    // Whether to blink the LED
                    light: true,

                    //LED color in ARGB format - this example RED color. If set to -1, light color will be default. Default value: -1.
                    lightColor: "0xFFFF0000",

                    //Importance - integer from 0 to 4. Default value: 4
                    //0 - none - no sound, does not show in the shade
                    //1 - min - no sound, only shows in the shade, below the fold
                    //2 - low - no sound, shows in the shade, and potentially in the status bar
                    //3 - default - shows everywhere, makes noise, but does not visually intrude
                    //4 - high - shows everywhere, makes noise and peeks
                    importance: 4,

                    //Show badge over app icon when non handled pushes are present. Default value: true
                    badge: true,

                    //Show message on locked screen. Default value: 1
                    //Possible values (default 1):
                    //-1 - secret - Do not reveal any part of the notification on a secure lockscreen.
                    //0 - private - Show the notification on all lockscreens, but conceal sensitive or private information on secure lockscreens.
                    //1 - public - Show the notification in its entirety on all lockscreens.
                    visibility: 1
                };

                // Create the channel
                self.FirebasePlugin.setDefaultChannel(channel,
                    function(){
                        console.log('Notification (default) Channel created: ' + channel.id + " " + JSON.stringify(channel));
                    },
                    function(error){
                        console.log('Create (default)  notification channel error: ' + error);
                    });

                let soundList = ["trainwhistle","woopwoop","clock","radar","sonar"];
                for (let key of soundList) {
                    let name = "smartfirebbq_" + key;

                    let newChannel = Object.assign({}, channel);
                    newChannel.id = name;
                    newChannel.sound = key;
                    newChannel.name = "Smartfire BBQ " + key;

                    console.log("Notification trying to create channel for " + name);

                    // Create the channel
                    self.FirebasePlugin.createChannel(newChannel,
                        function(){
                            console.log('Notification Channel created: ' + newChannel.id + " " + JSON.stringify(newChannel));
                        },
                        function(error){
                            console.log('Create notification channel error: ' + error);
                        });
                }

                self.FirebasePlugin.listChannels(
                    function(channels){
                        if(typeof channels == "undefined" || channels === null)
                            return;

                        for(var i=0;i<channels.length;i++)
                        {
                            console.log("FirebasePlugin Channel: " + channels[i].id + ", Name: " + channels[i].name);
                        }
                    },
                    function(error){
                        alert('List channels error: ' + error);
                    });

It all works neatly. I'd love for this to be merged, and I'm sure many other users will be very happy to have a solution as previously you could only create one 'sound' and you were locked into that for the whole app duration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants