Android 推送通知的自定义声音不起作用 (FCM)

Mad*_*Mac 6 android ionic-framework firebase-cloud-messaging capacitor

我使用来自云功能的 FCM 推送通知。这适用于 iOS 和 Android,并显示适当的图标并在 iOS 上播放自定义声音。

除了 Android 的自定义声音外,一切正常,它只是播放默认声音。

我创建了一个文件夹并将我的声音文件添加到其中,如下所示: android\app\src\main\res\raw\mp3_example.mp3

这个mp3有27秒长。我也试过 .wav 和 .aiff。

我读到我可能必须为更高版本的 Android 创建一个推送通知渠道,因此它可能与此有关。我尝试创建一个频道并使用来自云功能的 channelID 并且它可以工作但没有声音只是振动。

测试设备是运行 Android 8 的 Moto G6。我正在使用:FCM Firebase Cloud Functions Ionic 4 Capacitor https://github.com/stewwan/capacitor-fcm

云功能:

const notification: admin.messaging.Notification = {
      title: title,
      body: body
  }

  const message: admin.messaging.Message = {
    notification,
    topic: 'QMTBC',
    android:{
      notification:{
        sound: 'mp3_example.mp3',
        icon: 'push_logo',
        color: '#000000'
      }
    },
    apns:{
        payload:{
          aps: {
            sound: 'gears-short.wav'
          }
        }
    }
  }

  return admin.messaging().send(message)
Run Code Online (Sandbox Code Playgroud)

app.component.ts

import { FCM } from 'capacitor-fcm';

const fcm = new FCM();
const { PushNotifications } = Plugins;

initializeApp() {
    this.platform.ready().then(() => {

      PushNotifications.register();

      PushNotifications.addListener('registration', (token: PushNotificationToken) => {
        console.log('token ' + token.value);
        fcm
        .subscribeTo({ topic: 'QMTBC' })
        .then(r => console.log(`subscribed to topic`))
        .catch(err => console.log(err));        
      });

      PushNotifications.addListener('registrationError', (error: any) => {
        console.log('error on register ' + JSON.stringify(error));
      });

      PushNotifications.addListener('pushNotificationReceived', (notification: PushNotification) => {
        console.log('notification ' + JSON.stringify(notification));
        this.pushNotificationService.notifications.push(notification);
      });

      PushNotifications.addListener('pushNotificationActionPerformed', (notification: PushNotificationActionPerformed) => {
        console.log('notification ' + JSON.stringify(notification));
        this.pushNotificationService.notifications.push(notification);
      });

      fcm.getToken()
        .then(r => console.log(`Token ${r.token}`))
        .catch(err => console.log(err));
    });
  }
Run Code Online (Sandbox Code Playgroud)

更新:

我尝试按如下方式创建频道。如果我使用频道,我只会得到默认声音。如果我指定没有频道或一个不存在的频道,我也会得到默认声音(默认频道)。

云功能:

const message: admin.messaging.Message = {
    notification,
    topic: 'QMTBC',
    android:{
      notification:{
        sound: 'punch.mp3',
        icon: 'push_logo',
        color: '#000000',
        channelId: 'QMTBC'
      }
    }
Run Code Online (Sandbox Code Playgroud)

app.component.ts

const channel: PushNotificationChannel = {
          description: 'QMTBC',
          id : 'QMTBC',
          importance: 5,
          name : 'QMTBC'
        };

        PushNotifications.createChannel(channel).then(channelResult => {
          console.log(channelResult);
          console.log('Channel created');
          // PushNotifications.listChannels().then(channels => {
          //   console.log('Channels');
          //   console.log(channels);
          // });
        }, err => {
          console.log('Error Creating channel');
          console.log(err);
        });
      });
Run Code Online (Sandbox Code Playgroud)

更新 2:

我可以在我的设备上看到我为应用程序创建的频道,它说声音是默认的。我可以手动将其更改为另一个内置的 android 声音,这有效。但是我仍然无法使用我的自定义声音。

更新 3:

如果 Android 版本 < 8,则自定义声音有效。仅在模拟器上测试过。

Tho*_*yFD 5

@MadMac 这些天我遇到了同样的问题,在阅读了 FCM 文档和 Capacitor Java 代码后,我明白了。

有必要将可见性设置为 1,将文件放在 res/raw 文件夹中。

PushNotifications.createChannel({
            description: 'General Notifications',
            id: 'fcm_default_channel',
            importance: 5,
            lights: true,
            name: 'My notification channel',
            sound: 'notifications.wav',
            vibration: true,
            visibility: 1
        }).then(()=>{
            console.log('push channel created: ');
        }).catch(error =>{
            console.error('push channel error: ', error);
        });
Run Code Online (Sandbox Code Playgroud)

我在 firestore 函数中使用此有效负载来发送通知

{
            android: {
                notification: {
                    defaultSound: true,
                    notificationCount: 1,
                    sound: 'notifications.wav',
                    channelId: 'fcm_default_channel'
                },
                ttl: 20000,
                collapseKey
            },
            apns: {
                payload: {
                    aps: {
                        badge: 1,
                        sound: 'default'
                    }
                }
            },
            notification: {
                title,
                body: message,
            },
            token: token
        };
Run Code Online (Sandbox Code Playgroud)