Skip to content

Instantly share code, notes, and snippets.

@katharinepadilha
Last active December 1, 2019 20:03
Show Gist options
  • Save katharinepadilha/d82502b4abbe6be3a244783ff5b8652e to your computer and use it in GitHub Desktop.
Save katharinepadilha/d82502b4abbe6be3a244783ff5b8652e to your computer and use it in GitHub Desktop.
App.js setup for firebase notifications
//...
async getToken() {
let fcmToken = await AsyncStorage.getItem('fcmToken');
if (!fcmToken) {
fcmToken = await firebase.messaging().getToken();
if (fcmToken) {
await AsyncStorage.setItem('fcmToken', fcmToken);
}
}
}
async checkPermission() {
const enabled = await firebase.messaging().hasPermission();
if (enabled) {
this.getToken();
} else {
this.requestPermission();
}
}
async requestPermission() {
try {
await firebase.messaging().requestPermission();
this.getToken();
} catch (error) {
console.log('permission rejected');
}
}
async createNotificationListeners() {
firebase.notifications().onNotification(notification => {
notification.android.setChannelId('insider').setSound('default')
firebase.notifications().displayNotification(notification)
});
}
componentDidMount() {
const channel = new firebase.notifications.Android.Channel('insider', 'insider channel', firebase.notifications.Android.Importance.Max)
firebase.notifications().android.createChannel(channel);
this.checkPermission();
this.createNotificationListeners();
}
//...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment