Created
January 21, 2017 14:17
-
-
Save aleemb/d0b27eda744766bb3f43b2712f0aac0c to your computer and use it in GitHub Desktop.
Firebase IID persistence
This file contains hidden or 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
// 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