-
-
Save domadev812/c7a6fa3f4a5d2d45490f3cfbb2bf2ffb to your computer and use it in GitHub Desktop.
Snippets for blog, "Sending Android Push Notifications via GCM in JavaScript using PhoneGap and PubNub"
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
<script type="text/javascript" src="js/PushNotification.js"></script> | |
<script src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.17.0.js"></script> |
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
var pushNotification = window.plugins.pushNotification; | |
this.push.register().then((t: PushToken) => { | |
return this.push.saveToken(t); | |
}).then((t: PushToken) => { | |
console.log('Token saved:', t.token); | |
}); |
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
function successHandler(result) { | |
console.log('Success: '+ result); | |
} |
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
function errorHandler(error) { | |
console.log('Error: '+ error); | |
} |
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
function onNotificationGCM(e) { | |
switch(e.event){ | |
case 'registered': | |
if (e.regid.length > 0){ | |
deviceRegistered(e.regid); | |
} | |
break; | |
case 'message': | |
if (e.foreground){ | |
// When the app is running foreground. | |
alert('The room temperature is set too high') | |
} | |
break; | |
case 'error': | |
console.log('Error: ' + e.msg); | |
break; | |
default: | |
console.log('An unknown event was received'); | |
break; | |
} | |
} |
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
/* Cordova Push Notification Demo | |
* index.js - this file should be in your_cordova_app_root/www/js | |
* Also, you need PushNotification.js and PubNub.js in the dir too. | |
* Install Cordova Push Plugin w/ CLI... | |
* $ cordova plugin add https://github.com/phonegap-build/PushPlugin.git | |
*/ | |
var channel = ''; | |
var t = document.getElementById('temperature'); | |
var pubnub = new PubNub({ | |
publishKey : 'demo', | |
subscribeKey : 'demo' | |
}) | |
function initialize() { | |
bindEvents(); | |
} | |
function bindEvents() { | |
document.addEventListener('deviceready', init, false); | |
} | |
function init() { | |
var pushNotification = window.plugins.pushNotification; | |
pushNotification.register(successHandler, errorHandler, {'senderID':'8370991XXXXX','ecb':'onNotificationGCM'}); | |
// Get your Sender ID at cloud.google.com/console | |
} | |
function successHandler(result) { | |
console.log('Success: '+ result); | |
} | |
function errorHandler(error) { | |
console.log('Error: '+ error); | |
} | |
function onNotificationGCM(e) { | |
switch( e.event ){ | |
case 'registered': | |
if ( e.regid.length > 0 ){ | |
console.log('regid = '+e.regid); | |
deviceRegistered(e.regid); | |
} | |
break; | |
case 'message': | |
console.log(e); | |
if (e.foreground){ | |
alert('The room temperature is set too high') | |
} | |
break; | |
case 'error': | |
console.log('Error: '+e.msg); | |
break; | |
default: | |
console.log('An unknown event was received'); | |
break; | |
} | |
} | |
// Publish the channel name and regid to PubNub | |
function deviceRegistered(regid) { | |
channel = regid.substr(regid.length - 8).toLowerCase(); | |
var c = document.querySelector('.channel'); | |
c.innerHTML = 'Your Device ID: <strong>' + channel + '</strong>'; | |
c.classList.remove('blink'); | |
var publishConfig = { | |
channel : channel, | |
message : message: {regid: regid} | |
} | |
pubnub.publish(publishConfig, function(status, response) { | |
console.log(status, response); | |
}) | |
pubnub.addListener({ | |
status: function(statusEvent) { | |
if (statusEvent.category === "PNConnectedCategory") { | |
publishSampleMessage(); | |
} | |
}, | |
message: function(message) { | |
t.textContent = "" + message; | |
}, | |
presence: function(presenceEvent) { | |
// handle presence | |
} | |
}) | |
console.log("Subscribing.."); | |
pubnub.subscribe({ | |
channels: [channel] | |
}); | |
} | |
initialize(); |
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
pubnub = new PubNub({ | |
publishKey : 'demo', | |
subscribeKey : 'demo' | |
}) |
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
function changeTemperature(e) { | |
var temp = input.value; | |
var publishConfig = { | |
channel : "gcm-test", | |
message: { | |
temperature: temp | |
} | |
} | |
pubnub.publish(publishConfig, function(status, response) { | |
console.log(status, response); | |
}) | |
if(temp >= 80) { | |
sendPush(); | |
} | |
} |
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
function sendPush() { | |
pubnub.mobile_gw_provision ({ | |
device_id: 'APA91bERgbCFiAaR5awbHISeMDlCYfJB7pe95StxP8zNETEkBxgWY-HkxTXkB....', // Reg ID you got on your device | |
channel : channel, | |
op: 'add', | |
gw_type: 'gcm', | |
error : function(msg){console.log(msg);}, | |
callback : successCallback | |
}); | |
} |
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
function successCallback() { | |
var message = PNmessage(); | |
message.pubnub = pubnub; | |
message.callback = function(msg){ console.log(msg); }; | |
message.error = function (msg){ console.log(msg); }; | |
message.channel = channel; | |
message.gcm = { | |
title: 'Push Demo', | |
message: 'The room temperature is set too high' | |
}; | |
message.publish(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment