5

i'm using onesignal in my webapp for provide push notification.

as i'm starting to write React-native app i was wondering if someone can provide a useful example to show to how to handle push notifications with react-native.

currently whay i do is i register for push notifications in my root component constructor. but since there is very little doc about exactly when this will run i'm not sure should i move it out to the index.android.js file next to AppRegistry.registerComponent call ? or is it not garanteed that the js enviroment is fully loaded at this point ?

current code"

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

is it ok to move constructor logic out of my root Tag to the body of index.js file ? or better keep it this way ?

will AppRegistry get called when oneSignal notification is received ?

if i'm is to move this to headless task how would it look like ?

Zalaboza
  • 8,899
  • 16
  • 77
  • 142

0 Answers0