Tracking tokens
A token is a unique identifier for your user's device. It helps Firebase figure out where to send push notifications. In order for our notifications to be sent properly, we need to keep a record of all current device tokens in our database, and ensure that it's up to date.
We can access the token of our user's device via the Firebase messaging
library. Two methods in particular will be useful: onTokenRefresh
and getToken
. Both have fairly self-explanatory names, so we’ll dive right into the implementation:
export default class NotificationResource {
constructor(messaging) {
this.messaging = messaging;
try {
this.messaging
.requestPermission()
.then(res => {
console.log('Permission granted');
})
.catch(err => {
console.log('no access', err);
});
} catch(err) {
console.log('No notification support.', err);
}
};
this.messaging.getToken().then(res => {...