Apple Push notifications. Trying something else. again

This commit is contained in:
Martin Donnelly 2016-04-29 12:09:43 +01:00
parent 1ea5285973
commit 0945cb17ac
3 changed files with 64 additions and 58 deletions

44
app.js
View File

@ -32,44 +32,6 @@ var heating_v1 = require('./routes/heating_v1');
var projector_v1 = require('./routes/projector_v1'); var projector_v1 = require('./routes/projector_v1');
var isProduction = false; var isProduction = false;
var iosTokens = [];
var apnTimer = 0;
var apn = require('apn');
var apnService = new apn.connection({ key: 'conf/key.pem',
cert: 'conf/cert.pem', production: false });
apnService.on('connected', function() {
logger.debug('APN Connected');
});
apnService.on('transmitted', function(notification, device) {
logger.debug('APN Notification transmitted to:' + device.token.toString('hex'));
});
apnService.on('transmissionError', function(errCode, notification, device) {
logger.error('APNNotification caused error: ' + errCode + ' for device ', device, notification);
if (errCode === 8) {
logger.error('A error code of 8 indicates that the device token is invalid. This could be for a number of reasons - are you using the correct environment? i.e. Production vs. Sandbox');
}
});
apnService.on('timeout', function() {
logger.debug('Connection Timeout');
});
apnService.on('disconnected', function() {
logger.debug('Disconnected from APNS');
});
apnService.on('socketError', logger.error);
mqttConnect.setEmitter(busEmitter); mqttConnect.setEmitter(busEmitter);
mqttConnect.doConnection(); mqttConnect.doConnection();
@ -260,7 +222,9 @@ app.post('/api/v1/register/ios', function(req, res) {
registrationId = body['registrationId']; registrationId = body['registrationId'];
logger.debug(registrationId); logger.debug(registrationId);
if (iosTokens.indexOf(registrationId) === -1) { cal.registeriOSToken(id);
/* if (iosTokens.indexOf(registrationId) === -1) {
iosTokens.push(registrationId); iosTokens.push(registrationId);
logger.info('IOS Device registered.'); logger.info('IOS Device registered.');
@ -281,7 +245,7 @@ app.post('/api/v1/register/ios', function(req, res) {
}, 30000); }, 30000);
} }
*/
} }
}); });

View File

@ -18,7 +18,7 @@ var SOController = (function() {
var lastWeatherRequest = 0; var lastWeatherRequest = 0;
var soWebSocket; var soWebSocket;
var iosConfig = { var iosConfig = {
"badge": true, "sound": true, "alert": true, badge: true, sound: true, alert: true,
}; };
var lightsList = { var lightsList = {
@ -42,7 +42,7 @@ var SOController = (function() {
var iOS = ['iPad', 'iPhone', 'iPod'].indexOf(navigator.platform) >= 0; var iOS = ['iPad', 'iPhone', 'iPod'].indexOf(navigator.platform) >= 0;
if (iOS) { if (iOS) {
$('#iosTaskbar').show(); $('#iosTaskbar').show();
// path = 'http://localhost:3000/'; // path = 'http://localhost:3000/';
} }
var module = {lights: lightsList}; var module = {lights: lightsList};
@ -301,32 +301,31 @@ var SOController = (function() {
}; };
var cordovaNotify = function(d) { var cordovaNotify = function(d) {
console.log('+++ CORDOVA NOTIFY +++');
try { try {
navigator.notification.beep(1); navigator.notification.beep(1);
} catch(e) } catch (e) {
{
console.log(e); console.log(e);
} }
try { try {
navigator.notification.confirm( navigator.notification.confirm(
d.msg, // message d.msg, // Message
notifyConfirm, // callback to invoke with index of button pressed notifyConfirm, // Callback to invoke with index of button pressed
'Smartoffice Console', // title 'Smartoffice Console', // Title
['Extend','Ignore'] // buttonLabels ['Extend','Ignore'] // ButtonLabels
); );
} catch(e) } catch (e) {
{
console.log(e); console.log(e);
} }
}; };
var setupPush = function() { var setupPushNotifications = function() {
console.log('Setting up push notifications..'); console.log('Setting up iOS push notifications..');
var push = PushNotification.init({"android":{},"ios":iosConfig,"windows":{}}); var push = PushNotification.init({android: {},ios: iosConfig,windows: {}});
push.on('registration', function(d) { push.on('registration', function(d) {
console.log('*PUSH* Registration: ', JSON.stringify(d)); console.log('*PUSH* Registration: ', JSON.stringify(d));
@ -365,7 +364,11 @@ var SOController = (function() {
module.notify = function(d) { module.notify = function(d) {
if (window.cordova && !(window.cordova instanceof HTMLElement)) { if (window.cordova && !(window.cordova instanceof HTMLElement)) {
cordovaNotify(d); if (device.platform === 'android' || device.platform === 'Android') {
cordovaNotify(d);
} else if (device.platform === 'iOS' || device.platform === 'ios') {
setupPushNotifications();
}
} else { } else {
browserNotify(d); browserNotify(d);
} }

View File

@ -8,13 +8,43 @@ var events = require('events');
var eventBus ; var eventBus ;
var util = require('util'); var util = require('util');
/*var pushNotifications = require('node-pushnotifications'); var apn = require('apn');
var apnService = new apn.connection({ key: 'conf/key.pem',
cert: 'conf/cert.pem', production: false });
apnService.on('connected', function() {
logger.debug('APN Connected');
});
apnService.on('transmitted', function(notification, device) {
logger.debug('APN Notification transmitted to:' + device.token.toString('hex'));
});
apnService.on('transmissionError', function(errCode, notification, device) {
logger.error('APNNotification caused error: ' + errCode + ' for device ', device, notification);
if (errCode === 8) {
logger.error('A error code of 8 indicates that the device token is invalid. This could be for a number of reasons - are you using the correct environment? i.e. Production vs. Sandbox');
}
});
apnService.on('timeout', function() {
logger.debug('Connection Timeout');
});
apnService.on('disconnected', function() {
logger.debug('Disconnected from APNS');
});
apnService.on('socketError', logger.error);
var gcmSender = new gcm.Sender({
key: 'AIzaSyBl1vJ00d_lMb5yKGp97jZ94Oc70GRpZiw'
});*/
var officeController = function(neweventbus) { var officeController = function(neweventbus) {
this.iosTokens = [];
logger.debug((typeof neweventbus)); logger.debug((typeof neweventbus));
if ((typeof neweventbus !== 'undefined') && (neweventbus !== null)) { if ((typeof neweventbus !== 'undefined') && (neweventbus !== null)) {
@ -282,6 +312,15 @@ officeController.prototype.returnCalendar = function() {
}; };
officeController.prototype.registeriOSToken = function(id) {
if (this.iosTokens.indexOf(id) === -1) {
this.iosTokens.push(id);
logger.info('C - IOS Device registered.');
}
};
//OfficeController.prototype.getRoomStatus = function () { //OfficeController.prototype.getRoomStatus = function () {
// this.calendarControl.getRoomStatus(); // this.calendarControl.getRoomStatus();
// console.log('Checking Calendar.'); // console.log('Checking Calendar.');