打开应用程序时react-native-firebase无法接收通知

时间:2019-11-05 09:25:29

标签: reactjs firebase react-native

iam使用react native firebase存在一个问题,如果我关闭了它,我在打开应用程序时无法收到通知

1 个答案:

答案 0 :(得分:0)

尝试一下对我有用

   notification=()=>{
Const FCN = firebase.notifications();
Const channel = New firebase.notifications.Android.Channel(
  'all',
  'All Notifications',
  firebase.notifications.Android.Importance.Max
).setDescription('All notifications');

FCN.android.createChannel(channel);
 this.notificationDisplayedListener = firebase.notifications().onNotificationDisplayed((notification: Notification) => {
 // Process your notification As required
 // ANDROID: Remote notifications Do Not contain the channel ID. You will have To specify this manually If you'd like to re-display the notification.

 Const { title, body } = notification;

 });

this.notificationlocal = FCN.onNotification((notif: Notification) => {

 If (Platform.OS === 'android') {

Const { title, body } = notif;
        Const localNotification = New firebase.notifications.Notification({
          sound: 'sampleaudio',
            show_in_foreground: True,
             local: True,
          })

          .setSound('sampleaudio.wav') 
        .setNotificationId(notif.notificationId)
        .setTitle(notif.title)
          .setBody(notif.body)
          .android.setChannelId('all') // e.g. the id you chose above
          .android.setSmallIcon('@drawable/logo') // create this icon in Android Studio 
          .android.setColor('#000000')
           .android.setPriority(firebase.notifications.Android.Priority.High);
           console.log("channel",channel)
        firebase.notifications()
          .displayNotification(localNotification)

          .catch(err => console.error(err));

      }



 })
 this.notificationListener = firebase.notifications().onNotification((notification: Notification) => {
 // Process your notification As required
 this.notificationDisplayedListener = firebase.notifications().onNotificationDisplayed((notification: Notification) => {
 // Process your notification As required
 // ANDROID: Remote notifications Do Not contain the channel ID. You will have To specify this manually If you'd like to re-display the notification.
 Const { title, body } = notification;
 });
 Const { title, body } = notification;
 });
this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen: NotificationOpen) => {
 // Get the action triggered By the notification being opened
  this.notificationDisplayedListener = firebase.notifications().onNotificationDisplayed((notification: Notification) => {
 // Process your notification As required
 // ANDROID: Remote notifications Do Not contain the channel ID. You will have To specify this manually If you'd like to re-display the notification.
 Const { title, body } = notification;
 });
 Const action = notificationOpen.action;
 // Get information about the notification that was opened
 Const notification: Notification = notificationOpen.notification;
  Const { title, body } = notification;
 firebase.notifications().removeDeliveredNotification(notification.notificationId);
 //console.log('OPEN:', notification.data);
 });
firebase.notifications().getInitialNotification()
 .then(Async(notificationOpen: NotificationOpen) => {
 // console.log("notificationOpen1",notificationOpen)
 If (notificationOpen) {
 //console.log("notificationOpen",notificationOpen)
 // App was opened By a notification
 // Get the action triggered By the notification being opened
 Const action = notificationOpen.action;
 // Get information about the notification that was opened
 Const notification: Notification = notificationOpen.notification; 
 //Const { title, body } = JSON.parse(notification.data.custom_notification);

 }
 });
 this.messageListener = firebase.messaging().onMessage((message: RemoteMessage) => {

 });
}

componentDidMount(){
this.notification
}