Skip to content

Instantly share code, notes, and snippets.

@slorber
Created July 5, 2016 11:21
Show Gist options
  • Save slorber/88cc95d16647fd73fa071f497fd1c6f9 to your computer and use it in GitHub Desktop.
Save slorber/88cc95d16647fd73fa071f497fd1c6f9 to your computer and use it in GitHub Desktop.
"use strict";
var MobilePushRepository = require("repositories/mobilePushRepository");
var AppEvents = require("appEvents");
var AppHistory = require('appHistory');
var pushNotificationPluginSingleton = undefined;
function pushPlugin() {
if ( typeof cordova === "undefined" ) {
throw new Error("This can only be used in a Cordova app context");
}
if ( typeof PushNotification === "undefined" ) {
throw new Error("The push plugin is not installed for this Cordova app. Note that this plugin version >= 1.6.x requires cordova ios platform >= 4.x.x");
}
if ( !pushNotificationPluginSingleton ) {
var cfg = {
"android": {"senderID": "xxx"},
"ios": {"alert": "true", "badge": "true", "sound": "true"},
"windows": {}
};
console.debug("Init push plugin with config ",cfg);
pushNotificationPluginSingleton = PushNotification.init(cfg);
}
return pushNotificationPluginSingleton;
}
function getPushDeviceType() {
return device.platform.toUpperCase();
}
function registerForPushs(atomReactContext) {
console.debug("Push.registerForPushs");
var push = pushPlugin();
push.on('registration', function(data) {
var deviceId = data.registrationId;
var deviceType = getPushDeviceType();
var deviceLog = deviceType+" -> " + deviceId;
console.debug("Will register mobile device for push notifications: "+deviceLog);
MobilePushRepository.registerDevice(deviceId,deviceType)
.then(function() {
localStorage["push_registered_device_id"] = deviceId;
console.debug("Device registered for push notifications!: "+deviceLog);
})
.fail(function(e) {
console.error("Device failed to register for push notifications!: "+deviceLog);
});
});
push.on('notification', function(data) {
console.debug("Push notification received",data);
if ( data.additionalData.foreground ) {
console.debug("Bypassing push message because app is currently active",data);
return;
}
const maybeUrl = data.additionalData.url;
if ( maybeUrl ) {
if ( maybeUrl ) {
console.debug("App is paused, and push has url. Will route app to url="+maybeUrl);
AppHistory.push(maybeUrl);
}
else {
console.debug("Push message has no url, so we don't know yet what to do with it",data);
}
}
atomReactContext.publishEvent(AppEvents.mobilePushReceived(data));
});
push.on('error', function(e) {
console.error(e);
});
}
exports.registerForPushs = registerForPushs;
function unregisterForPushs() {
console.debug("Push.unregisterForPushs");
var push = pushPlugin();
// Frontend should not receive pushs anymore
push.unregister(
function() {
console.debug("successfully unregistered for pushs")
},
function(e) {
console.error("failed to unregistered for pushs: " + (e ? e.message: "no-error"),e);
}
);
// And backend should not try to send push to this deviceId anymore
var deviceId = localStorage["push_registered_device_id"];
if ( deviceId ) {
var deviceType = getPushDeviceType();
var deviceLog = deviceType+" -> " + deviceId;
MobilePushRepository.unregisterDevice(deviceId,deviceType)
.then(function() {
console.debug("successfully unregistered for pushs in backend: " + deviceLog)
})
.fail(function(e) {
console.error("failed to unregistered for pushs in backend for device "+deviceLog+": " + (e ? e.message: "no-error"),e);
});
}
else {
console.error("Can't unregisterForPushs because it seems the device was not correctly registered");
}
}
exports.unregisterForPushs = unregisterForPushs;
function setBadgeNumber(number) {
var push = pushPlugin();
push.setApplicationIconBadgeNumber(function() {
console.debug('successfully setBadgeNumber='+number);
}, function(e) {
console.error('error in setBadgeNumber='+number,e);
}, number);
}
exports.setBadgeNumber = setBadgeNumber;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment