我已经使用FCM从云函数成功发送推送通知。这对iOS和Android都有效,可以在iOS上显示适当的图标并播放自定义声音。
除了Android的自定义声音外,一切正常,它只会播放默认声音。
我创建了一个文件夹,并按如下方式添加了我的声音文件:
我读到可能需要为Android的后续版本创建推送通知通道,因此问题可能与此有关。我尝试创建一个通道并使用云函数中的channelID,它可以工作,但没有声音,只有振动。
测试设备是运行Android 8的Moto G6。 我正在使用: FCM Firebase Cloud Functions Ionic 4 Capacitor https://github.com/stewwan/capacitor-fcm 云函数:
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)
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版本 < 8上,自定义声音才能正常工作。这只是在模拟器上测试过。