我正在使用此库:
https://github.com/invertase/react-native-firebase
但是当应用程序是前景时,我会出现黄色错误:
YellowBox.js:80 Possible Unhandled Promise Rejection (id: 0):
Error: AndroidNotification: Missing required `channelId` property
Error: AndroidNotification: Missing required `channelId` property
at AndroidNotification.build (blob:http://localhost:8081/a9bbbd91-a1cf-4143-8ed0-ed77cccfe5e0:172069:17)
at Notification.build (blob:http://localhost:8081/a9bbbd91-a1cf-4143-8ed0-ed77cccfe5e0:171679:75)
at Notifications.displayNotification (blob:http://localhost:8081/a9bbbd91-a1cf-4143-8ed0-ed77cccfe5e0:170690:86)
at blob:http://localhost:8081/a9bbbd91-a1cf-4143-8ed0-ed77cccfe5e0:173153:53
at EventEmitter.emit (blob:http://localhost:8081/a9bbbd91-a1cf-4143-8ed0-ed77cccfe5e0:162784:37)
at blob:http://localhost:8081/a9bbbd91-a1cf-4143-8ed0-ed77cccfe5e0:170658:36
at EventEmitter.emit (blob:http://localhost:8081/a9bbbd91-a1cf-4143-8ed0-ed77cccfe5e0:162784:37)
at blob:http://localhost:8081/a9bbbd91-a1cf-4143-8ed0-ed77cccfe5e0:162686:30
at RCTDeviceEventEmitter.emit (blob:http://localhost:8081/a9bbbd91-a1cf-4143-8ed0-ed77cccfe5e0:3478:37)
at MessageQueue.__callFunction (blob:http://localhost:8081/a9bbbd91-a1cf-4143-8ed0-ed77cccfe5e0:2384:44)
我的代码:
import firebase from 'react-native-firebase';
function displayNotificationFromCustomData(message){
console.log('masoud masoud')
if(message.data && message.data.title){
let notification = new firebase.notifications.Notification();
notification = notification
.setTitle(message.data.title)
.setBody(message.data.body)
.setData(message.data)
.setSound("default")
notification.android.setPriority(firebase.notifications.Android.Priority.High) /// set to High
notification.android.setChannelId("teamcide") ///for android 8.0 and above
const channel = new firebase.notifications.Android.Channel(
"daily",
"Reminders",
firebase.notifications.Android.Importance.High
).setDescription("Reminds you....");
firebase.notifications().android.createChannel(channel);
firebase.notifications().displayNotification(notification);
}
}
// these callback will be triggered only when app is foreground or background
export function registerAppListener(){
console.log('Ok')
this.notificationListener = firebase.notifications().onNotification(notification => {
firebase.notifications().displayNotification(notification);
})
this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen) => {
const notif = notificationOpen.notification;
console.log(notif)
setTimeout(()=>{
alert(`User tapped notification\n${notif.notificationId}`)
}, 500)
});
this.onTokenRefreshListener = firebase.messaging().onTokenRefresh(token => {
console.log("TOKEN (refreshUnsubscribe)", token);
});
this.messageListener = firebase.messaging().onMessage((message) => {
console.log('masoud')
console.log(message)
displayNotificationFromCustomData(message);
});
}
当应用程序处于后台运行时,一切正常。