Nativescript-plugin-firebase: Custom notification sound not working on android 8 with Cloud Messaging

Created on 24 Jul 2018  路  6Comments  路  Source: EddyVerbruggen/nativescript-plugin-firebase

When sending a notification with custom notification sounds(see below), the custom sound works as expected on <8 but the default sound is played on 8.

{ 
    android: { 
        priority: 'normal',
        notification: { sound: 'custom_notification', title: 'New message! 馃挰', body: 'YO' } 
    },
    topic: '5b571df2c693f8e74ee53e32' 
}

All 6 comments

custom sound works as expected on >8, if we create notification channel, when send a message the
notification message json should have the channel id.

https://developer.android.com/training/notify-user/channels
https://firebase.google.com/docs/cloud-messaging/concept-options
https://firebase.google.com/docs/cloud-messaging/http-server-ref

add the below code in your app.componet.ts

import { isAndroid } from 'platform';
declare var android: any;

ngOnInit() {
this.createNotificationChannel();
}

createNotificationChannel() {
if (isAndroid) {
this.notificationChannel("channelId_01", "Channel Name", "Channel Description",
"SoundFileName");
}
}

notificationChannel(channelID: string, channelName: string, channelDesc: string, soundFileName: string) {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        var utils = require("utils/utils");
        var context = utils.ad.getApplicationContext();

        var mNotificationManager = context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);

        // The id of the channel.
        const channelId = channelID;
        // The user-visible name of the channel.
        const name = channelName;
        // The user-visible description of the channel.
        const description = channelDesc;
        const importance = android.app.NotificationManager.IMPORTANCE_HIGH;
        const mChannel = new android.app.NotificationChannel(channelId, name, importance);
        // Configure the notification channel.
        mChannel.setDescription(description);
        mChannel.enableLights(true);
        // Sets the notification light color for notifications posted to this
        // channel, if the device supports this feature.
        mChannel.setLightColor(android.graphics.Color.RED);
        mChannel.enableVibration(true);
        mChannel.setSound(android.net.Uri.parse("android.resource://org.nativescript.notification/raw/" + soundFileName), null);
        mNotificationManager.createNotificationChannel(mChannel);
    }
}

I am still facing this issue even after sending channel id in the message json.

Place the mp3 file under raw folder:

EX: App_Resources\Android\src\main\res\raw\nsound.mp3

Create Notification Channel as i mentioned in my above comment

this.notificationChannel("channelId_01", "Channel Name", "Channel Description", "nsound"); // please should not give sound file extension

You can check whether the channel is created in your phone or emulator under setting -> your app --> notification settings.

Notification Message JSON Should be like this:

{
"notification" : { "title" : "Test Notification", "body" : "Message..", "android_channel_id" : "channelId_01", "sound": "nsound.mp3" },
"priority" : "high"
"data" : "foreground Message.."
}

Oreo (Android 8) and above App will use android_channel_id property
Below Oreo App will use sound property

Note: Sound will be played only background mode.

If your app is in foreground, you have to use some other plugin to play the sound file in message callback method.

mChannel.setSound(android.net.Uri.parse("android.resource://" + context.getPackageName() + "/raw/" + soundFileName), null);

The above worked for me.

Just a note to other people seeing this thread. Once you create a notification channel the sound or other aspects can't be mutated.

@DanLatimer apparently only name and description can be changed, according to this SO answer.
And I think it makes sense, because otherwise it would be pretty annoying for users.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

b02505048 picture b02505048  路  3Comments

ButterMeWaffle picture ButterMeWaffle  路  4Comments

EddyVerbruggen picture EddyVerbruggen  路  3Comments

thunder413 picture thunder413  路  3Comments

Aceman18 picture Aceman18  路  3Comments