簡體   English   中英

react-native-firebase 在打開應用程序時無法收到通知

[英]react-native-firebase can't receive notification while open app

我正在使用本機反應 firebase 有一個問題我在打開應用程序時無法收到通知,如果它關閉我收到它

試試這個它對我有用

   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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM