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

Notification channel capability so sounds will play in android oreo +

Open markterrill opened this issue 5 years ago • 6 comments

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.

markterrill avatar Mar 14 '19 01:03 markterrill

Hi.

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

ellecito avatar Mar 28 '19 21:03 ellecito

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"

markterrill avatar Apr 05 '19 01:04 markterrill

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

ellecito avatar Apr 05 '19 20:04 ellecito

Will it be compatible with cordova 6.3.0?

davidpadych avatar Apr 17 '19 20:04 davidpadych

I haven’t been able to build my app with <7 for a while, welcome anyone else’s input?

There is a switcher based on android version. I’d hope that’d cover 90% of cases..

Mark

Sent from my iPhone

On 18 Apr 2019, at 06:40, David Padých <[email protected]mailto:[email protected]> wrote:

Will it be compatible with cordova 6.3.0?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/arnesson/cordova-plugin-firebase/pull/1026#issuecomment-484252883, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AAT5UUIHYDPGCESZGOT4KFLPQ6DMXANCNFSM4G6J6OEQ.

markterrill avatar Apr 18 '19 04:04 markterrill

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.

markterrill avatar Apr 24 '20 03:04 markterrill