Skip to content

Commit

Permalink
Merge pull request #76 from EFForg/fcm_topics
Browse files Browse the repository at this point in the history
Subscribe to FCM topic for broadcast messages
  • Loading branch information
wioux committed Jan 18, 2018
2 parents ab20b82 + 8edbdc0 commit a04696c
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 166 deletions.
2 changes: 1 addition & 1 deletion .env.example
@@ -1,2 +1,2 @@
SENTRY_DSN=
PUSH_SERVER=https://push.eff.org/api
FCM_TOPIC=eff-alerts
4 changes: 2 additions & 2 deletions config/custom-environment-variables.json
Expand Up @@ -3,7 +3,7 @@
"SENTRY_DSN": "SENTRY_DSN"
},

"API": {
"ENDPOINT": "PUSH_SERVER"
"NOTIFICATIONS": {
"TOPIC": "FCM_TOPIC"
}
}
9 changes: 4 additions & 5 deletions config/default.json
@@ -1,17 +1,16 @@
{

"API": {
"VERSION": 1,
"ENDPOINT": "http://127.0.0.1:5000/api"
},

"APP": {
"PUSH_CAPABLE_PLATFORMS": [
"ANDROID",
"IOS"
]
},

"NOTIFICATIONS": {
"TOPIC": ""
},

"SHARING": {
"URL": "https://www.eff.org/app",
"MESSAGES": {
Expand Down
5 changes: 2 additions & 3 deletions config/production.json
Expand Up @@ -2,8 +2,7 @@

"MODE": "production",

"API": {
"VERSION": 1,
"ENDPOINT": "https://push.eff.org/api"
"NOTIFICATIONS": {
"TOPIC": "eff-alerts"
}
}
2 changes: 1 addition & 1 deletion gulp/config.js
Expand Up @@ -20,7 +20,7 @@ module.exports = {
dest: BUILD_DIR,
fileName: 'app_settings.js',
settingsKeys: [
'API',
'NOTIFICATIONS',
'APP',
'CREDENTIALS',
'SHARING'
Expand Down
11 changes: 6 additions & 5 deletions www/js/app.js
Expand Up @@ -53,7 +53,6 @@ actionCenterMobile.controller('DonateCtrl', require('./controllers/donate'));
actionCenterMobile.controller('SettingsCtrl', require('./controllers/settings'));

actionCenterMobile.factory('acmUserDefaults', require('./services/user_defaults'));
actionCenterMobile.factory('acmAPI', require('./services/api'));
actionCenterMobile.factory('acmDeviceLanguage', require('./services/language'));
actionCenterMobile.factory('acmSharing', require('./services/sharing'));

Expand Down Expand Up @@ -167,15 +166,17 @@ actionCenterMobile.run(function (
$rootScope, $state, $ionicHistory, $ionicPlatform, acmPushNotification, acmUserDefaults) {

var registerForPush = function () {
// Do not register if user has disabled push notifications.
if (acmUserDefaults.getUserDefault(acmUserDefaults.keys.PUSH_ENABLED) === false) {
return;
}
var platform = ionic.Platform.platform().toUpperCase();

if (window.plugins !== undefined &&
appSettings['APP']['PUSH_CAPABLE_PLATFORMS'].indexOf(platform) !== -1) {
acmPushNotification.register();

if (acmUserDefaults.getUserDefault(acmUserDefaults.keys.PUSH_ENABLED) !== false) {
acmPushNotification.subscribe(function() {}, function(err) {
console.log("Couldn't subscribe to FCM topic: " + JSON.stringify(err));
});
}
}
};

Expand Down
15 changes: 11 additions & 4 deletions www/js/controllers/actionCenter.js
Expand Up @@ -22,12 +22,19 @@ var ActionCenterCtrl = function ($scope, $ionicPopover, acmPushNotification, acm

$scope.togglePushNotifications = function () {
acmUserDefaults.setUserDefault(acmUserDefaults.keys.PUSH_ENABLED, $scope.preferences.pushNotificationsEnabled);
var deviceId = acmUserDefaults.getUserDefault(acmUserDefaults.keys.REGISTERED_DEVICE_ID);
if (!$scope.preferences.pushNotificationsEnabled && deviceId) {
acmPushNotificationHelpers.unregisterDeviceId(deviceId);
if (!$scope.preferences.pushNotificationsEnabled) {
acmPushNotification.unsubscribe(function() {
acmUserDefaults.setUserDefault(acmUserDefaults.keys.REGISTERED_FOR_PUSH, false);
}, function(err) {
console.error('Unable to unsubscribe from push channel: ' + JSON.stringify(err));
});
}
else {
acmPushNotification.register();
acmPushNotification.subscribe(function() {
acmUserDefaults.setUserDefault(acmUserDefaults.keys.REGISTERED_FOR_PUSH, true);
}, function(err) {
console.error('Unable to subscribe to push channel: ' + JSON.stringify(err));
});
}
};

Expand Down
103 changes: 0 additions & 103 deletions www/js/services/api.js

This file was deleted.

26 changes: 12 additions & 14 deletions www/js/services/push.js
Expand Up @@ -13,6 +13,8 @@ import { Push } from '@ionic-native/push';
var PushNotificationService = function (
$rootScope, $state, $cordovaPush, acmGCMPushNotification, acmAPNSPushNotification) {

var pushObject;

var service = {

getPlatformPushService_: function (devicePlatform) {
Expand Down Expand Up @@ -51,7 +53,8 @@ var PushNotificationService = function (
pushConfig.vibrate = true;
pushConfig.forceShow = true;

var pushObject = $cordovaPush.init({ android: pushConfig });
pushObject = $cordovaPush.init({ android: pushConfig });

pushObject.on('registration', function(registrationid, registrationType) {
devicePushHandler.registrationSuccess(registrationid);
});
Expand All @@ -63,22 +66,17 @@ var PushNotificationService = function (
pushObject.on('notification', function(notification) {
service.handlePushNotification(notification);
});

pushObject.subscribe(null, function() {});

var ionicPush = new Push();
ionicPush.createChannel({
id: null,
description: "EFF Alerts",
importance: 3
}).then(function() {
console.log("Notification channel created.");
}, function(e) {
console.error("Error creating notification channel: " + JSON.stringify(e));
});
}
},

subscribe: function(success, error) {
pushObject.subscribe(appSettings.NOTIFICATIONS.TOPIC, success, error);
},

unsubscribe: function(success, error) {
pushObject.unsubscribe(appSettings.NOTIFICATIONS.TOPIC, success, error);
},

/**
* Top level handler for push notifications on all platforms.
*
Expand Down
2 changes: 1 addition & 1 deletion www/js/services/push/gcm.js
Expand Up @@ -8,7 +8,7 @@ var constants = require('./constants');


var GCMNotificationService = function (
$state, $cordovaLocalNotifications, acmUserDefaults, acmAPI, acmPushNotificationHelpers) {
$state, $cordovaLocalNotifications, acmUserDefaults, acmPushNotificationHelpers) {

var handleMessage = function (notification) {
var isForeground = notification.foreground;
Expand Down
31 changes: 4 additions & 27 deletions www/js/services/push/helpers.js
Expand Up @@ -5,7 +5,7 @@
var angular = require('angular');


var PushHelpersService = function (acmUserDefaults, acmAPI) {
var PushHelpersService = function (acmUserDefaults) {

var defaultsPayloadArr = [
['title', acmUserDefaults.keys.ACTION_TITLE],
Expand All @@ -29,32 +29,9 @@ var PushHelpersService = function (acmUserDefaults, acmAPI) {
},

registerDeviceId: function (deviceId) {
var success = function () {
console.log("successfully registered, saving device id: " + JSON.stringify(deviceId));
acmUserDefaults.setUserDefault(acmUserDefaults.keys.REGISTERED_FOR_PUSH, true);
acmUserDefaults.setUserDefault(acmUserDefaults.keys.REGISTERED_DEVICE_ID, deviceId);
};

var error = function (err) {
console.error('Unable to register device with push server: ' + JSON.stringify(err));
};

// This could be skipped by checking whether or not a locally held copy of the id matches that
// returned on registration. However, due to privacy concerns, the app is intended to record
// as little information as possible, so just ping the server each time.
acmAPI.registerDeviceForNotifications(deviceId, success, error);
},

unregisterDeviceId: function (deviceId) {
var success = function () {
acmUserDefaults.setUserDefault(acmUserDefaults.keys.REGISTERED_FOR_PUSH, false);
acmUserDefaults.setUserDefault(acmUserDefaults.keys.REGISTERED_DEVICE_ID, false);
};

var error = function (err) {
console.error('Unable to unregister device from push server: ' + err);
};
acmAPI.unregisterDeviceForNotifications(deviceId, success, error);
console.log("successfully registered, saving device id: " + JSON.stringify(deviceId));
acmUserDefaults.setUserDefault(acmUserDefaults.keys.REGISTERED_FOR_PUSH, true);
acmUserDefaults.setUserDefault(acmUserDefaults.keys.REGISTERED_DEVICE_ID, deviceId);
},

truncateString: function (string, length) {
Expand Down

0 comments on commit a04696c

Please sign in to comment.