Skip to content

Instantly share code, notes, and snippets.

@ShaileshPrajapati-BTC
Created May 23, 2017 17:28
Show Gist options
  • Save ShaileshPrajapati-BTC/29326770cff28cee2cb01958fd79b5fe to your computer and use it in GitHub Desktop.
Save ShaileshPrajapati-BTC/29326770cff28cee2cb01958fd79b5fe to your computer and use it in GitHub Desktop.
import React, { Component } from "react";
import FCM from "react-native-fcm";
export default class PushNotification extends Component {
constructor(props) {
super(props);
}
componentDidMount() {
FCM.requestPermissions();
FCM.getFCMToken().then(token => {
console.log("TOKEN (getFCMToken)", token);
});
FCM.getInitialNotification().then(notif => {
console.log("INITIAL NOTIFICATION", notif)
});
this.notificationUnsubscribe = FCM.on("notification", notif => {
console.log("a", notif);
if (notif && notif.local_notification) {
return;
}
this.sendRemote(notif);
});
this.refreshUnsubscribe = FCM.on("refreshToken", token => {
console.log("TOKEN (refreshUnsubscribe)", token);
this.props.onChangeToken(token);
});
}
sendRemote(notif) {
console.log('send');
FCM.presentLocalNotification({
title: notif.title,
body: notif.body,
priority: "high",
click_action: notif.click_action,
show_in_foreground: true,
local: true
});
}
componentWillUnmount() {
this.refreshUnsubscribe();
this.notificationUnsubscribe();
}
render() {
return null;
}
}
@linuissac
Copy link

Got a warning error when i used this in my code "invalid FCM Event subscription".
31

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment