📅  最后修改于: 2022-03-11 15:01:14.705000             🧑  作者: Mango
const App = () =>{
useEffect = (() =>{
getToken();
createChannel();
notificationlistner();
},[])
const getToken = async () => {
const fcmToken = await messaging().getToken();
console.log(fcmToken);
};
const createChannel = () =>{
const channel = new firebase.notifications.Android.createChannel(
'channelId',
'channelName',
firebase.notifications.Android.Importance.Max
).setDescription('Description');
firebase.notifications().Android.createChannel(channel);
};
const notificationlistner = () => {
firebase.notifications().onNotification((notification) =>{
if(Platform.OS === 'android'){
const localNotification = new firebase.notifications.Notification({
sound : 'default',
show_in_forground : true,
})
.setNotificationId(notification.notificationId)
.setTitle(notification.title)
.setSubtitle(notification.subtitle)
.setBody(notification.body)
.setData(notification.data)
.android.setChannelId('channelId')
.android.setPriority(firebase.notifications.Android.Priority.High);
firebase.notifications().displayNotification(localNotification)
.catch((err) => console.log(err))
}
});
};