DEV Community

loading...

Discussion on: How to use OneSignal with a React Native Chat App | part three

Collapse
raza1122p profile image
Ali Raza • Edited

my app is crashed after receiving notification

export default class PushNotification extends Component {
constructor(props) {
super(props);

OneSignal.init('---------------------------------');
OneSignal.addEventListener("received", this.onReceived);
OneSignal.addEventListener("opened", this.onOpened);
OneSignal.addEventListener("ids", this.onIds);
OneSignal.configure();

}

onReceived = notification => {
console.log("Notification received: ", notification);
};

onOpened = openResult => {
console.log("Message: ", openResult.notification.payload.body);
console.log("Data: ", openResult.notification.payload.additionalData);
console.log("isActive: ", openResult.notification.isAppInFocus);
console.log("openResult: ", openResult);
};

onIds = device => {
console.log("Device info: ", device);
this.setState({ device });
};

sendNotification = data => {
let headers = {
"Content-Type": "application/json; charset=utf-8",
Authorization: "Basic 'OneSignal Server Key'"
};

let endpoint = "https://onesignal.com/api/v1/notifications";

let params = {
  method: "POST",
  headers: headers,
  body: JSON.stringify({
    app_id: "-------------------------------------",
    included_segments: ["All"],
    contents: { en: data }
  })
};
fetch(endpoint, params).then(res => console.log(res));

};
componentDidMount() {
this.sendNotification('Greeting from Chat App');
}

Forem Open with the Forem app