Last active
December 1, 2019 20:03
-
-
Save katharinepadilha/d82502b4abbe6be3a244783ff5b8652e to your computer and use it in GitHub Desktop.
App.js setup for firebase notifications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//... | |
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