Skip to content

Instantly share code, notes, and snippets.

@aleemb
Created January 21, 2017 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aleemb/d0b27eda744766bb3f43b2712f0aac0c to your computer and use it in GitHub Desktop.
Save aleemb/d0b27eda744766bb3f43b2712f0aac0c to your computer and use it in GitHub Desktop.
Firebase IID persistence
// Request permission to receive notifications via dialog
messaging.requestPermission().then(function(result) {
// Permission granted.
messaging.getToken().then(function(currentToken) {
// Got IID. Persist to server and track state in localStorage
var persistedIID = localStorage.getItem('messaging.persistedIID');
if (persistedIID != currentToken) {
$.post('/iid', {'iid': currentToken}, function() {
localStorage.setItem('messaging.persistedIID', currentToken);
});
}
}).catch(function(err) {
// some messaging error
});
}).catch(function(err) {
// Permission blocked. Remove persistedIID from localStorage
localStorage.removeItem('messaging.persistedIID');
$.post('/iid', {'iid': null});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment