【问题标题】:Custom sound for Android Push Notification not working (FCM)Android推送通知的自定义声音不起作用(FCM)
【发布时间】:2020-01-09 19:48:00
【问题描述】:

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

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

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

此 mp3 长达 27 秒。我也试过 .wav 和 .aiff。

我读到我可能需要为更高版本的 Android 创建一个推送通知通道,以便与此相关。我尝试创建一个频道并使用云功能中的频道 ID,它可以工作,但没有声音,只是振动。

测试设备是运行 Android 8 的 Moto G6。 我在用: 流式细胞仪 Firebase 云函数 离子 4 电容器 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)

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));
    });
  }

更新:

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

云功能:

const message: admin.messaging.Message = {
    notification,
    topic: 'QMTBC',
    android:{
      notification:{
        sound: 'punch.mp3',
        icon: 'push_logo',
        color: '#000000',
        channelId: 'QMTBC'
      }
    }

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);
        });
      });

更新 2:

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

更新 3:

如果 Android 版本

【问题讨论】:

  • 您找到发送通知并使用自定义声音播放的解决方案了吗?我的文件在 res/raw 中,但它们无法播放
  • @Kash 不,我从来没有找到解决方案。
  • 我有同样的问题,设置自定义声音在 Android 8 之后不起作用
  • 当您定义通知通道时,我没有看到您在任何地方指定punch.mp3。
  • 是的@MadMac 声音是在您定义频道时设置的,而不是在您发送通知时设置的。

标签: android ionic-framework firebase-cloud-messaging capacitor


【解决方案1】:

这是一个很好的问题,帮助我找到了答案。所以我在这里发布我的答案。尝试在创建频道时将通知的声音设置为通知频道本身。我想,根据您的信息,旧的 Android 版本将根据通知有效负载中的声场播放声音,但在新版本中,您必须将其直接设置为通知通道本身,因为现在是控件所在的位置目前打算由 Google 提供。我必须卸载并重新安装应用程序才能使此代码更改生效,因为我的频道之前已初始化,并且在第一次初始化后频道不会更新。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !notificationChannelsInitialized) {
            val newMessagesChannel = NotificationChannel(NEW_MESSAGES_NOTIFICATION_CHANNEL_ID, "New Messages", NotificationManager.IMPORTANCE_HIGH)

            val notificationSoundUri =
                Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE.toString() + "://" + context.packageName + "/" + R.raw.ns) // ns.wav is my notification sound file in the res/raw folder in Android Studio
            val notificationSoundUriAttributes = AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .build()
            newMessagesChannel.setSound(notificationSoundUri, notificationSoundUriAttributes)

            val notificationManager: NotificationManager =
                context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannels(listOf( newMessagesChannel)) // and other channels
        }

【讨论】:

  • 谢谢。我会在下个月左右更新我的项目时尝试一下。
【解决方案2】:

@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);
        });

我在我的 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
        };

【讨论】:

  • defaultSound: true?
  • 是的@genericUser,那么您可以在 XML 配置中指定您的默认声音。来自 FCM 的文档:/** * 如果设置为 true,则使用 Android 框架的默认声音作为通知。 * 默认值在config.xml 中指定。 */ defaultSound?: boolean;
猜你喜欢
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-28
  • 1970-01-01
  • 2022-07-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多