Created
December 20, 2016 02:45
-
-
Save tedshd/6edc130f5e0c58d2500a9de2cf3245c2 to your computer and use it in GitHub Desktop.
firebase cloud message (FCM) example
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
/** | |
* | |
* @authors Ted Shiu (ted@sitetag.us) | |
* @date 2016-12-15 17:29:18 | |
*/ | |
var config = { | |
apiKey: '', | |
authDomain: '', | |
databaseURL: '', | |
storageBucket: '', | |
messagingSenderId: '' | |
}; | |
firebase.initializeApp(config); | |
var messaging = firebase.messaging(); | |
messaging.onTokenRefresh(function() { | |
messaging.getToken().then(function(refreshedToken) { | |
console.log('Token refreshed.'); | |
setTokenSentToServer(false); | |
// Send Instance ID token to app server. | |
sendTokenToServer(refreshedToken); | |
}).catch(function(err) { | |
console.log('Unable to retrieve refreshed token ', err); | |
}); | |
}); | |
messaging.onMessage(function(payload) { | |
console.log("Message received. ", payload); | |
fcmFgUI(payload); | |
}); | |
function initToken() { | |
messaging.getToken().then(function(currentToken) { | |
if (currentToken) { | |
sendTokenToServer(currentToken); | |
} else { | |
console.log('No Instance ID token available. Request permission to generate one.'); | |
permissionRequiredUI(); | |
setTokenSentToServer(false); | |
} | |
}).catch(function(err) { | |
console.log('An error occurred while retrieving token. ', err); | |
setTokenSentToServer(false); | |
}); | |
} | |
function sendTokenToServer(currentToken) { | |
if (!isTokenSentToServer()) { | |
console.log('Sending token to server...'); | |
// TODO(developer): Send the current token to your server. | |
// get old token | |
// api del old token | |
// api metch | |
setTokenSentToServer(true); | |
// senf topic api to app server | |
subTopic(); | |
} else { | |
console.log('Token already sent to server so won\'t send it again unless it changes'); | |
} | |
} | |
function requestPermission() { | |
console.log('Requesting permission...'); | |
messaging.requestPermission().then(function() { | |
initToken(); | |
}).catch(function(err) { | |
console.log('Unable to get permission to notify.', err); | |
}); | |
} | |
function deleteToken() { | |
messaging.getToken().then(function(currentToken) { | |
messaging.deleteToken(currentToken).then(function() { | |
console.log('Token deleted.'); | |
setTokenSentToServer(false); | |
initToken(); | |
}).catch(function(err) { | |
console.log('Unable to delete token. ', err); | |
}); | |
}).catch(function(err) { | |
console.log('Error retrieving Instance ID token. ', err); | |
}); | |
} | |
function fcmFgUI(payload) { | |
console.log(payload); | |
// TODO | |
// { | |
// "from": "1021059308181", | |
// "collapse_key": "do_not_collapse", | |
// "notification": { | |
// "title": "this is title", | |
// "body": "Enter content", | |
// "icon": "demo/fcm/quickstart-js/messaging/firebase-logo.png", | |
// "click_action": "http://tedshd.io" | |
// } | |
// } | |
} | |
function subTopic() { | |
// TODO | |
} | |
function permissionRequiredUI() { | |
// TODO | |
} | |
function isTokenSentToServer() { | |
if (window.localStorage.getItem('sentToServer') == 1) { | |
return true; | |
} | |
return false; | |
} | |
function setTokenSentToServer(sent) { | |
if (sent) { | |
window.localStorage.setItem('sentToServer', 1); | |
} else { | |
window.localStorage.setItem('sentToServer', 0); | |
} | |
} | |
initToken(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment