Skip to content

Commit 592485a

Browse files
committed
MOBILE-1494 push: Use new push notifications plugin
1 parent 0752e80 commit 592485a

File tree

4 files changed

+71
-120
lines changed

4 files changed

+71
-120
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"devDependencies": {
55
"ionic": "driftyco/ionic-bower#v1.1.1",
66
"ydn.db": ">=1.0.3",
7-
"ngCordova": ">=0.1.12-alpha",
7+
"ngCordova": ">=0.1.24-alpha",
88
"angular-mocks": "1.4.3",
99
"angular-md5": "0.1.8",
1010
"angular-translate": ">=2.7.0",

package.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@
4141
"cordova-plugin-splashscreen",
4242
"cordova-plugin-statusbar",
4343
"cordova-plugin-whitelist",
44-
{
45-
"locator": "https://github.com/phonegap-build/PushPlugin",
46-
"id": "com.phonegap.plugins.pushplugin"
47-
},
4844
{
4945
"locator": "https://github.com/Tunts/WebIntent",
5046
"id": "net.tunts.webintent"
@@ -62,10 +58,17 @@
6258
"id": "ch.ti8m.documenthandler"
6359
},
6460
"cordova-plugin-local-notifications-mm",
65-
"cordova-plugin-zip"
61+
"cordova-plugin-zip",
62+
{
63+
"variables": {
64+
"SENDER_ID": "694767596569"
65+
},
66+
"locator": "phonegap-plugin-push",
67+
"id": "phonegap-plugin-push"
68+
}
6669
],
6770
"cordovaPlatforms": [
6871
"android",
6972
"ios"
7073
]
71-
}
74+
}

www/addons/pushnotifications/main.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,8 @@ angular.module('mm.addons.pushnotifications', [])
2525
});
2626

2727
// Notification received.
28-
$rootScope.$on('$cordovaPush:notificationReceived', function(e, notification) {
29-
if (ionic.Platform.isAndroid()) {
30-
$mmaPushNotifications.onGCMReceived(notification);
31-
} else if (ionic.Platform.isIOS()) {
32-
$mmaPushNotifications.onMessageReceived(notification);
33-
}
28+
$rootScope.$on('$cordovaPushV5:notificationReceived', function(e, notification) {
29+
$mmaPushNotifications.onMessageReceived(notification);
3430
});
3531

3632
// Register device on Moodle site when login.

www/addons/pushnotifications/services/pushnotifications.js

Lines changed: 59 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ angular.module('mm.addons.pushnotifications')
2121
* @ngdoc service
2222
* @name $mmaPushNotifications
2323
*/
24-
.factory('$mmaPushNotifications', function($mmSite, $log, $cordovaPush, $mmText, $q, $cordovaDevice, $mmUtil, mmCoreConfigConstants,
24+
.factory('$mmaPushNotifications', function($mmSite, $log, $cordovaPushV5, $mmText, $q, $cordovaDevice, $mmUtil, mmCoreConfigConstants,
2525
$mmApp, $mmLocalNotifications, $mmPushNotificationsDelegate, $mmSitesManager, mmaPushNotificationsComponent) {
2626
$log = $log.getInstance('$mmaPushNotifications');
2727

@@ -48,50 +48,14 @@ angular.module('mm.addons.pushnotifications')
4848
* @module mm.addons.pushnotifications
4949
* @ngdoc method
5050
* @name $mmaPushNotifications#notificationClicked
51-
* @param {Object} data Notification data.
51+
* @param {Object} notification Notification.
5252
*/
53-
self.notificationClicked = function(data) {
53+
self.notificationClicked = function(notification) {
5454
$mmApp.ready().then(function() {
55-
$mmPushNotificationsDelegate.clicked(data);
55+
$mmPushNotificationsDelegate.clicked(notification);
5656
});
5757
};
5858

59-
/**
60-
* This function is called from the PushPlugin when we receive a Notification from GCM.
61-
* The app can be in foreground or background,
62-
* if we are in background this code is executed when we open the app clicking in the notification bar.
63-
*
64-
* @module mm.addons.pushnotifications
65-
* @ngdoc method
66-
* @name $mmaPushNotifications#onGCMReceived
67-
* @param {Object} notification Notification data.
68-
*/
69-
self.onGCMReceived = function(notification) {
70-
$log.debug('GCM notification received. Type: '+notification.event);
71-
72-
switch (notification.event) {
73-
case 'registered':
74-
if (notification.regid.length > 0) {
75-
pushID = notification.regid;
76-
return self.registerDeviceOnMoodle();
77-
} else {
78-
$log.debug('Device NOT registered in GCM, invalid regid');
79-
break;
80-
}
81-
82-
case 'message':
83-
notification.payload.foreground = notification.foreground;
84-
return self.onMessageReceived(notification.payload);
85-
86-
case 'error':
87-
$log.debug('Push messages error');
88-
break;
89-
90-
default:
91-
$log.debug('Push unknown message');
92-
}
93-
};
94-
9559
/**
9660
* This function is called when we receive a Notification from APNS or a message notification from GCM.
9761
* The app can be in foreground or background,
@@ -100,12 +64,13 @@ angular.module('mm.addons.pushnotifications')
10064
* @module mm.addons.pushnotifications
10165
* @ngdoc method
10266
* @name $mmaPushNotifications#onMessageReceived
103-
* @param {Object} data Notification data.
67+
* @param {Object} notification Notification received.
10468
*/
105-
self.onMessageReceived = function(data) {
106-
var promise;
69+
self.onMessageReceived = function(notification) {
70+
var promise,
71+
data = notification ? notification.additionalData : {};
10772

108-
if (data && data.site) {
73+
if (data.site) {
10974
promise = $mmSitesManager.getSite(data.site); // Check if site exists.
11075
} else {
11176
promise = $q.when(); // No site specified, resolve.
@@ -115,25 +80,39 @@ angular.module('mm.addons.pushnotifications')
11580
if ($mmUtil.isTrueOrOne(data.foreground)) {
11681
// If the app is in foreground when the notification is received, it's not shown. Let's show it ourselves.
11782
if ($mmLocalNotifications.isAvailable()) {
83+
var localNotif = {
84+
id: 1,
85+
at: new Date(),
86+
smallIcon: 'res://icon',
87+
data: {
88+
notif: data.notif,
89+
site: data.site
90+
}
91+
},
92+
promises = [];
93+
11894
// Apply formatText to title and message.
119-
$mmText.formatText(data.title, true, true).then(function(formattedTitle) {
120-
$mmText.formatText(data.message, true, true).then(function(formattedMessage) {
121-
var localNotif = {
122-
id: 1,
123-
title: formattedTitle,
124-
message: formattedMessage,
125-
at: new Date(),
126-
smallIcon: 'res://icon',
127-
data: {
128-
notif: data.notif,
129-
site: data.site
130-
}
131-
};
132-
$mmLocalNotifications.schedule(localNotif, mmaPushNotificationsComponent, data.site);
133-
});
95+
promises.push($mmText.formatText(notification.title, true, true).then(function(formattedTitle) {
96+
localNotif.title = formattedTitle;
97+
}).catch(function() {
98+
localNotif.title = notification.title;
99+
}));
100+
101+
promises.push($mmText.formatText(notification.message, true, true).then(function(formattedMessage) {
102+
localNotif.message = formattedMessage;
103+
}).catch(function() {
104+
localNotif.message = notification.message;
105+
}));
106+
107+
$q.all(promises).then(function() {
108+
$mmLocalNotifications.schedule(localNotif, mmaPushNotificationsComponent, data.site);
134109
});
135110
}
136111
} else {
112+
// The notification was clicked. For compatibility with old push plugin implementation
113+
// we'll merge all the notification data in a single object.
114+
data.title = notification.title;
115+
data.message = notification.message;
137116
self.notificationClicked(data);
138117
}
139118
});
@@ -149,59 +128,32 @@ angular.module('mm.addons.pushnotifications')
149128
*/
150129
self.registerDevice = function() {
151130
try {
152-
if (ionic.Platform.isIOS()) {
153-
return self._registerDeviceAPNS();
154-
} else if (ionic.Platform.isAndroid()) {
155-
return self._registerDeviceGCM();
156-
}
131+
var options = {
132+
android: {
133+
senderID: mmCoreConfigConstants.gcmpn
134+
},
135+
ios: {
136+
alert: true,
137+
badge: true,
138+
sound: true
139+
}
140+
};
141+
return $cordovaPushV5.initialize(options).then(function() {
142+
// Start listening for notifications and errors.
143+
$cordovaPushV5.onNotification();
144+
$cordovaPushV5.onError();
145+
146+
// Register the device in GCM or APNS.
147+
return $cordovaPushV5.register().then(function(token) {
148+
pushID = token;
149+
return self.registerDeviceOnMoodle();
150+
});
151+
});
157152
} catch(ex) {}
158153

159154
return $q.reject();
160155
};
161156

162-
/**
163-
* Register a device in Apple APNS (Apple Push Notificaiton System) using the Phonegap PushPlugin.
164-
* It also registers the device in the Moodle site using the core_user_add_user_device WebService.
165-
* We need the device registered in Moodle so we can connect the device with the message output Moode plugin airnotifier.
166-
*
167-
* @module mm.addons.pushnotifications
168-
* @ngdoc method
169-
* @name $mmaPushNotifications#_registerDeviceAPNS
170-
* @return {Promise} Promise resolved when the device is registered.
171-
* @protected
172-
*/
173-
self._registerDeviceAPNS = function() {
174-
var options = {
175-
alert: 'true',
176-
badge: 'true',
177-
sound: 'true'
178-
};
179-
return $cordovaPush.register(options).then(function(token) {
180-
pushID = token;
181-
return self.registerDeviceOnMoodle();
182-
}, function(error) {
183-
return $q.reject();
184-
});
185-
};
186-
187-
/**
188-
* Register a device in Google GCM using the Phonegap PushPlugin.
189-
*
190-
* @module mm.addons.pushnotifications
191-
* @ngdoc method
192-
* @name $mmaPushNotifications#_registerDeviceGCM
193-
* @return {Promise} Promise resolved when the device is registered.
194-
* @protected
195-
*/
196-
self._registerDeviceGCM = function() {
197-
if (mmCoreConfigConstants.gcmpn) {
198-
return $cordovaPush.register({
199-
senderID: mmCoreConfigConstants.gcmpn
200-
});
201-
}
202-
return $q.reject();
203-
};
204-
205157
/**
206158
* Registers a device on current Moodle site.
207159
*

0 commit comments

Comments
 (0)