如何处理从后端发送的本机应用程序中收到的推送通知?

时间:2019-02-19 05:58:26

标签: react-native

我正在使用本机Fcm,并且卡在某一点。 我想在后端触发通知时显示或打印通知响应结果,在应用关闭,前台(尤其是处于终止状态)时显示在应用中, 如何在本机应用程序中接收并显示

我的代码:-我已经尝试过,但是只会显示并显示后台通知。 不适用于杀死状态和前景状态

componentDidMount() {
    //Create Channel for custom notifications and target >=26
    FCM.createNotificationChannel({
      id: 'default',
      name: 'Default',
      description: 'used for example',
      priority: 'high'
    })
    //Check Permission for notifications
    FCM.requestPermissions(); 
    // Here you get the fcm token
    FCM.getFCMToken().then(token => { 
    // store fcm token in your server
      this.setState({device_id:token});
    });

    // store fcm token for ios in your server
    // if (Platform.OS === "ios") {
    //   FCM.getAPNSToken().then(token => {
    //     this.setState({device_id:token});
    //   });
    // }

    // This method get all notification from server side.
    FCM.getInitialNotification().then((notif) => {
      FCM.setBadgeNumber(0);
      console.log('notopen>>>>>>===='+JSON.stringify(notif))
      // for android/ios app killed state
      if (notif.opened_from_tray) {
        console.log('KILLED>>>>>>===='+JSON.stringify(notif))
      // there are two parts of notif. notif.notification contains the notification payload, notif.data contains data payload 
        this.showLocalNotification();
      }
    });
    this.notificationListener = FCM.on(FCMEvent.Notification, async (notif) => {
        // app was in the background
        if (notif.opened_from_tray) { 
             console.log('background>>>>>>===='+JSON.stringify(notif))
             this.sendRemote(notif);
        } else{
          // app was in the foreground
          this.showLocalNotification(notif);
          console.log('foreground>>>>>>===='+JSON.stringify(notif))
      }
    });
    this.refreshTokenListener = FCM.on(FCMEvent.RefreshToken, (token) => {
      console.log(token);
     // fcm token may not be available on first load, catch it here
    });
  }

  // This method display the notification on mobile screen.
  sendRemote(notif) {
    FCM.presentLocalNotification({
      channel: 'default',
      id: 0,
      icon: "app_icon", // as FCM payload, you can relace this with custom icon you put in mipmap
      title: notif.title,
      body: notif.body, // as FCM payload (required)
      priority: "high",
      ongoing: false,
      wake_screen: true,
      show_in_foreground: true // notification when app is in foreground (local & remote)
    });
  }

  showLocalNotification() {
    FCM.presentLocalNotification({
      channel: 'default',
      id: new Date().valueOf().toString(), // (optional for instant notification)
      title: "Test Notification with action", // as FCM payload
      body: "Force touch to reply", // as FCM payload (required)
      sound: "bell.mp3", // "default" or filename
      priority: "high", // as FCM payload
      show_in_foreground: true // notification when app is in foreground (local & remote)
    });
  }

0 个答案:

没有答案