android - 无法在应用程序处于后台时收到 "open"通知

标签 android django react-native push-notification react-android

当我想处理应用程序在后台时收到的通知时遇到问题。我确实收到了通知,当我点击通知时,应用程序打开但是 FCM.on 方法根本没有执行,它似乎没有处理回调。

当应用程序在前台运行时,一切正常,并且它可以毫无问题地处理通知。

这是react-native中的代码

FCM.on(FCMEvent.Notification, notif => {
      switch(notif.fcm.action){
        case 'fcm.ACTION.HANDLEMESSAGE':
          var data = {
            thread: notif.pk,
            message: JSON.parse(notif.message)
          }

          this.props.dispatch(fcmMSG(data));
          this.props.dispatch(addNotification(notif.pk, notif.job_id));
          break;

        case "fcm.ACTION.HANDLEAPPLICATION":
          axios.get(newServerURL + "/jobs/companyJobDetails/" + notif.recruitJobId + "/")
              .then((response) => {
                  var jobMainRecruits = response.data;
                  const {navigator} = this.refs;
                  navigator.push({
                  jobMainRecruits,
                  });
            })
              .catch((error) => {
                  console.log("ERROR")
            });        
          break;

        default:
      }
    });  
FCM.getInitialNotification().then(notif => {
      switch(notif.fcm.action){
        case 'fcm.ACTION.HANDLEMESSAGE':

          const { navigator } = this.refs;

          var thread = {
            candidate: notif.candidate,
            company: notif.company,
            candidate_avatar: notif.candidate_avatar,
            candidate_first_name: notif.candidate_first_name,
            candidate_last_name: notif.candidate_last_name,

            job_id: notif.job_id,
            pk: notif.pk,
            subject: notif.subject,
            message: JSON.parse(notif.message)
          }

          navigator.push({
            thread,
          });

          break;
        case "fcm.ACTION.HANDLEAPPLICATION":
          axios.get(newServerURL + "/jobs/companyJobDetails/" + notif.recruitJobId + "/")
              .then((response) => {
                  var jobMainMessages = response.data;
                  const {navigator} = this.refs;
                  navigator.push({
                  jobMainMessages,
                  });
            })
              .catch((error) => {
                  console.log("ERROR")
            });          
          break;

        default:

      }
    })

编辑: 这是我在后端的代码

push_service = FCMNotification(api_key=FCM_API_KEY)
                    registration_id = token.token
                    message_title = jobInfo.title
                    message_body = "New application"
                    message_icon = 'ic_launcher'
                    sound = 'Default'
                    color = '#362F64'
                    click_action='fcm.ACTION.NEWJOBAPPLICATION'
                    data_message = {'recruitJobId': self.request.data['job'], 'message_title': message_title, 'message_body': message_body}
                    result = push_service.notify_single_device(click_action=click_action, registration_id=registration_id, message_title=message_title, message_body=message_body, message_icon=message_icon, sound=sound, data_message=data_message, color=color)

提前感谢您的帮助。

最佳答案

The initial notification contains the notification that launches the app. If the user launches the app by clicking the banner, the banner notification info will be here rather than through FCM.on event. sometimes Android kills activity when the app goes to background, and then resume it broadcasts notification before JS is run. You can use FCM.getInitialNotification() to capture those missed events. source

FCM.getInitialNotification().then(notif => {
    // do some logic here
   // I usually add this function inside my root page
});

关于android - 无法在应用程序处于后台时收到 "open"通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46544405/

相关文章:

android - 如何频繁地从api获取数据?

java - android 项目中外部 jar 文件的 Manifest 主要属性的签名文件摘要无效

java - Android:以编程方式更改 TextView 位置(左或右)?

python - Django 无法在 Windows 10 上运行

javascript - jsx : ReactJS 中的 if-else 语句

Android react native recyclerview

android - 我怎样才能让下载管理器覆盖现有文件而不是在android中重命名该文件

django:如何将静态文件放在根目录下?

django - Stripe 客户是在没有信用卡信息的情况下创建的

react-native - 更改导航标题中后退箭头的颜色