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

时间:2016-11-27 14:46:39

标签: reactjs push-notification react-native onesignal

我在我的网络应用中使用信号提供推送通知。

当我开始编写React-native app时,我想知道是否有人可以提供一个有用的示例来展示如何使用react-native处理推送通知。

目前我做的是在我的根组件构造函数中注册推送通知。 但是由于关于这个运行的确切时间的文档非常少,我不确定是否应该将它移到AppRegistry.registerComponent调用旁边的index.android.js文件中?还是不保证此时js环境已满载?

当前代码"

// 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 />);

将构造函数逻辑从我的根Tag移动到index.js文件的主体是否可以?或者更好地保持这种方式?

收到oneSignal通知后会调用AppRegistry吗?

如果我要把它移到无头的任务上,它会是什么样子?

0 个答案:

没有答案