Reactjs 示例react本机应用程序使用Headless JS任务处理推送通知

Reactjs 示例react本机应用程序使用Headless JS任务处理推送通知,reactjs,push-notification,react-native,onesignal,Reactjs,Push Notification,React Native,Onesignal,我在我的webapp中使用onesignal提供推送通知 在我开始编写React-native应用程序时,我想知道是否有人可以提供一个有用的示例来演示如何使用React-native处理推送通知 目前我要做的是在根组件构造函数中注册推送通知。 但是,由于几乎没有关于何时运行的文档,我不确定是否应该将其移到AppRegistry.registerComponentcall旁边的index.android.js文件中?或者不保证js环境此时已完全加载 当前代码“ //index.android.js

我在我的webapp中使用onesignal提供推送通知

在我开始编写React-native应用程序时,我想知道是否有人可以提供一个有用的示例来演示如何使用React-native处理推送通知

目前我要做的是在根组件构造函数中注册推送通知。 但是,由于几乎没有关于何时运行的文档,我不确定是否应该将其移到
AppRegistry.registerComponent
call旁边的index.android.js文件中?或者不保证js环境此时已完全加载

当前代码“

//index.android.js,index.ios.js
从“React”导入React;
从“react native”导入{AppRegistry,Text,View};
从“react native OneSignal”导入OneSignal;
类应用程序扩展组件{
建造师(道具){
//配置oneSignal推送和执行启动任务。。
这个。$device={};
此.$push=[];//用于保存挂起通知的数组..仅用于
//防止在调用config时应用程序ui未完全加载时丢失它们
OneSignal.configure({
ONIDS可用:(设备)=>{
console.log('UserId=',device.UserId);
console.log('PushToken=',device.PushToken);
此。$device=device;//保存em。。
},
OnNotificationOpen:(消息、数据、isActive)=>{
常量通知={
消息
数据,
我很活跃,
};
此.$push.pending.push(通知);
},
});
OneSignal.clearOneSignalNotifications();
一个信号。启用振动(真);
OneSignal.enableSound(真);
OneSignal.requestPermissions({
警报:是的,
徽章:没错,
听起来:是的,
});
OneSignal.setSubscription(true);
}
render(){返回我的应用程序将在此处运行..;}
}
AppRegistry.registerComponent('AppName',()=>()=>);
将构造函数逻辑从我的根标记移到index.js文件的主体可以吗?还是最好保持这种方式

收到oneSignal通知时是否会调用AppRegistry

如果我要把这项任务转移到无头任务,它会是什么样子

// index.android.js,index.ios.js
import React from 'react';
import {AppRegistry,Text,View} from 'react-native';

import OneSignal from 'react-native-onesignal';

class App extends Component {
  constructor(props) {
    // config oneSignal push and do startup tasks..
    this.$device = {};
    this.$push = []; // array to hold pending notifications.. just to 
                     // prevent losing them case app ui was not fully loaded when config is called
    OneSignal.configure({
        onIdsAvailable: (device) => {
          console.log('UserId = ', device.userId);
          console.log('PushToken = ', device.pushToken);
          this.$device = device; //save em..
        },
        onNotificationOpened: (message, data, isActive) => {
          const notification = {
            message,
            data,
            isActive,
          };
          this.$push.pending.push(notification);
        },
      });
      OneSignal.clearOneSignalNotifications();
      OneSignal.enableVibrate(true);
      OneSignal.enableSound(true);
      OneSignal.requestPermissions({
        alert: true,
        badge: true,
        sound: true,
      });
      OneSignal.setSubscription(true);
  }
  render(){return <View><Text>My App will run here..</Text></View>;}
}
AppRegistry.registerComponent('AppName', () => ()=><App />);