【发布时间】:2019-07-08 10:46:28
【问题描述】:
我正在开发自定义声音通知功能。自定义声音在版本 8 以下的 iOS 和 Android 上正常工作。通知也出现在所有版本的 Android 中。但是,自定义声音不适用于 android 8 和 9。我还创建了一个频道 ID。
下面我分享了我的代码。谁能帮帮我吗?提前致谢。
async checkPermission() {
const enabled = await firebase.messaging().hasPermission();
if (enabled) {
this.getToken();
} else {
this.requestPermission();
}
let channel = new firebase.notifications.Android.Channel(
"channelId",
"Channel Name",
firebase.notifications.Android.Importance.Max
).setDescription("A natural description of the channel");
firebase.notifications().android.createChannel(channel);
firebase
.notifications()
.getInitialNotification()
.then(notificationOpen => {
if (notificationOpen) {
const action = notificationOpen.action;
const notification = notificationOpen.notification;
}
});
// the listener returns a function you can use to unsubscribe
this.unsubscribeFromNotificationListener = firebase
.notifications()
.onNotification(notification => {
if (Platform.OS === "android") {
const channel = new firebase.notifications.Android.Channel(
"channelId",
"Channel Name",
firebase.notifications.Android.Importance.Max
)
.setDescription("A natural description of the channel")
.setSound(
notification.data.sound ? notification.data.sound : "default"
);
firebase.notifications().android.createChannel(channel);
const localNotification = new firebase.notifications.Notification({
sound: notification.data.sound
? notification.data.sound
: "default",
show_in_foreground: true
})
.setNotificationId(notification.notificationId)
.setTitle(notification.title)
.setSubtitle(notification.subtitle)
.setBody(notification.body)
.setData(notification.data)
.setSound(
notification.data.sound ? notification.data.sound : "default"
)
.android.setSmallIcon("notification_icon_black")
.android.setChannelId("channelId")
.android.setAutoCancel(true)
.android.setVibrate(1000)
.android.setColor("#000000") // you can set a color here
.android.setGroup(notification.notificationId)
.android.setPriority(firebase.notifications.Android.Priority.High);
firebase
.notifications()
.displayNotification(localNotification)
.catch(err => console.error(err));
} else if (Platform.OS === "ios") {
const localNotification = new firebase.notifications.Notification()
.setNotificationId(notification.notificationId)
.setTitle(notification.title)
.setSound(
notification.data.sound ? notification.data.sound : "default"
)
.setSubtitle(notification.subtitle)
.setBody(notification.body)
.setData(notification.data)
.ios.setBadge(notification.ios.badge);
firebase
.notifications()
.displayNotification(localNotification)
.catch(err => console.error(err));
}
});
const notificationOpen = await firebase
.notifications()
.getInitialNotification();
if (notificationOpen) {
const action = notificationOpen.action;
const notification = notificationOpen.notification;
if (notification.data) {
//handle data
}
}
this.notificationOpenedListener = firebase
.notifications()
.onNotificationOpened(notificationOpen => {
const notification = notificationOpen.notification;
if (notification.data) {
//handle data
}
});
}
async requestPermission() {
try {
await firebase.messaging().requestPermission();
// User has authorised
this.getToken();
} catch (error) {
// User has rejected permissions
console.log("permission rejected");
}
}
async getToken() {
let fcmToken = await firebase.messaging().getToken();
if (fcmToken) {
console.log("fcm token===>", fcmToken);
}
}
【问题讨论】:
-
RNcoremodule 未找到遇到此问题。并且无法解决。我也搜索并尝试了很多东西。那么我该如何实现 Firebase 通知
标签: android firebase react-native react-native-firebase