【发布时间】: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