且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android Firebase通知没有自定义声音

更新时间:2023-02-26 21:18:31

最后我找到了解决方案。对于Android 8.0及更高版本,必须在您的应用中创建一个通知通道:

Finally I found the solution. For Android 8.0 and higher it's necessary to create a notification channel in your App:

NotificationChannel channel = new NotificationChannel('my_id', name, importance);

(更多信息: https://developer.android.com/training/notify-user/channels#java

然后,当您发送通知时:

Then when you send the notification:

var registrationToken = 'xxxxxx';

var message = {

    notification: {
      title: 'my title',
      body: 'my body',
    },
    android: {
      ttl: 3600 * 1000,
      notification: {
        color: '#ff0000',
        sound: 'mysound.mp3',
        channel_id: 'my_id' // important to get custom sound
      }
    }, 
    token: registrationToken

};