Skip to content

Instantly share code, notes, and snippets.

@domadev812
Forked from girliemac/cordova-01.html
Last active November 15, 2017 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save domadev812/0597c185d29f312b0ed61fbf8f0ecbce to your computer and use it in GitHub Desktop.
Save domadev812/0597c185d29f312b0ed61fbf8f0ecbce to your computer and use it in GitHub Desktop.
Snippets for blog, "Sending iOS Push Notifications via APNS in JavaScript using PhoneGap and PubNub"
<script type="text/javascript" src="js/PushNotification.js"></script>
<script src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.17.0.js"></scrip>
var pushNotification = window.plugins.pushNotification;
pushNotification.register(
tokenHandler,
errorHandler,
{
'badge':'false',
'sound':'false',
'alert':'true',
'ecb':'onNotificationAPN'
}
);
// You must install Cordova Device Plugin to be able to use the method
if(device.platform == 'iOS') {
// call the registration function for iOS
} else if (device.platform == 'Android' || device.platform == 'amazon-fireos') {
// Android
}
...
function tokenHandler (result) {
console.log('device token: '+ result);
// This is a device token you will need later to send a push
// Store this to PubNub to make your life easier :-)
}
/* 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 Deivce Plugin w/ CLI...
* $ cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-device.git
*
* Install Cordova Push Plugin w/ CLI...
* $ cordova plugin add https://github.com/phonegap-build/PushPlugin.git
*/
var channel = '';
var t = document.getElementById('temperature');
var pushNotification;
pubnub = new PubNub({
publishKey : 'demo',
subscribeKey : 'demo'
})
function initialize() {
bindEvents();
}
function bindEvents() {
document.addEventListener('deviceready', init, false);
}
function init() {
pushNotification = window.plugins.pushNotification;
if (device.platform == 'android' || device.platform == 'Android' || device.platform == 'amazon-fireos' ) {
pushNotification.register(successHandler, errorHandler, {
'senderID':'837099162939',
'ecb':'onNotificationGCM'
});
} else { // iOS
pushNotification.register(tokenHandler, errorHandler, {
'badge':'false',
'sound':'false',
'alert':'true',
'ecb':'onNotificationAPN'
});
}
}
// Android
function successHandler(result) {
console.log('Success: '+ result);
}
// iOS
function tokenHandler (result) {
console.log('device token: '+ result);
// Your iOS push server needs to know the token before it can push to this device
channel = result.substr(result.length - 7).toLowerCase();
var c = document.querySelector('.channel');
c.innerHTML = 'Your Device ID: <strong>' + channel + '</strong>';
c.classList.remove('blink');
var publishConfig = {
channel : channel,
message : {regid: result}
}
pubnub.publish(publishConfig, function(status, response) {
console.log(status, response);
})
pubnub.subscribe({
channels: [channel],
});
pubnub.addListener({message:redraw});
}
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;
}
}
function onNotificationAPN(e) {
// Event callback that gets called when your device receives a notification
console.log('onNotificationAPN called!');
console.log(e);
if (e.badge) {
pushNotification.setApplicationIconBadgeNumber(successHandler, e.badge);
}
if (e.sound) {
var sound = new Media(e.sound);
sound.play();
}
if (e.alert) {
navigator.notification.alert(e.alert);
}
}
// 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 : {regid: regid}
}
pubnub.publish(publishConfig, function(status, response) {
console.log(status, response);
})
pubnub.subscribe({
channels: [channel],
});
pubnub.addListener({message:redraw});
}
initialize();
pubnub = new PubNub({
publishKey : 'demo',
subscribeKey : 'demo'
})
function changeTemperature(e) {
var temp = input.value;
var publishConfig = {
channel : "hello_world",
message : "Hello from PubNub Docs!"
}
pubnub.publish(publishConfig, function(status, response) {
console.log(status, response);
})
if(temp >= 80) {
sendPush();
}
}
function sendPush() {
pubnub.mobile_gw_provision ({
device_id: '24c99cd5c39b283c58b554509c999999937aadb7b4bxxx0043cb363af6....', // Reg ID you got on your device
channel : 'apns-test',
op: 'add',
gw_type: 'apns',
error : function(msg){console.log(msg);},
callback : successCallback
});
}
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.apns = {
alert: '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