forked from ionic-team/ionic-service-deploy
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ionic-push.js
206 lines (176 loc) · 6 KB
/
ionic-push.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
angular.module('ionic.service.push', ['ngCordova', 'ionic.service.core'])
/**
* The Ionic Push service client wrapper.
*
* Example:
*
* angular.controller(['$scope', '$ionicPush', function($scope, $ionicPush) {
* }])
*
*/
.factory('$ionicPush', [
'$http', '$cordovaPush',
'$ionicApp', '$ionicPushActions',
'$ionicUser', '$rootScope', '$log', '$q',
function($http, $cordovaPush, $ionicApp, $ionicPushActions, $ionicUser, $rootScope, $log, $q) {
// Grab the current app
var app = $ionicApp.getApp();
//Check for required credentials
if(!app || !app.app_id) {
console.error('PUSH: Unable to initialize, you must call $ionicAppProvider.identify() first');
}
function init(options, metadata) {
var defer = $q.defer();
// TODO: This should be part of a config not a direct method
var gcmKey = $ionicApp.getGcmId();
var api = $ionicApp.getValue('push_api_server');
//Default configuration
var config = {
"senderID": gcmKey,
"badge": true,
"sound": true,
"alert": true
};
$cordovaPush.register(config).then(function(token) {
console.log('$ionicPush:REGISTERED', token);
defer.resolve(token);
if(token !== 'OK') {
$rootScope.$emit('$cordovaPush:tokenReceived', {
token: token,
platform: 'ios'
});
// Push the token into the user data
try {
$ionicUser.push('_push.ios_tokens', token, true);
} catch(e) {
console.warn('Received push token before user was identified and will not be synced with ionic.io. Make sure to call $ionicUser.identify() before calling $ionicPush.register.');
}
}
});
$rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) {
console.log('$ionicPush:RECEIVED', JSON.stringify(notification));
var callbackRet = options.onNotification && options.onNotification(notification);
// If the custom handler returns false, don't handle this at all in
// our code
if(callbackRet === false) {
return;
}
if (ionic.Platform.isAndroid() && notification.event == "registered") {
/**
* Android handles push notification registration in a callback from the GCM service (whereas
* iOS can be handled in a single call), so we need to check for a special notification type
* here.
*/
console.log('$ionicPush:REGISTERED', notification.regid);
$rootScope.$emit('$cordovaPush:tokenReceived', {
token: notification.regid,
platform: 'android'
});
androidInit(notification.regid, metadata);
}
// If we have the notification plugin, show this
if(options.canShowAlert && notification.alert) {
if (navigator.notification) {
navigator.notification.alert(notification.alert);
} else {
// Browser version
alert(notification.alert);
}
}
if(options.canPlaySound) {
if (notification.sound && window.Media) {
var snd = new Media(notification.sound);
snd.play();
}
}
if(options.canSetBadge) {
if (notification.badge) {
$cordovaPush.setBadgeNumber(notification.badge).then(function(result) {
// Success!
}, function(err) {
console.log('Could not set badge!', err);
// An error occurred. Show a message to the user
});
}
}
// Run any custom notification actions
if(options.canRunActionsOnWake) {
if(notification.foreground == "0" || notification.foreground === false) {
$ionicPushActions.run(notification);
}
}
});
return defer.promise;
}
function androidInit(token, metadata) {
// Push the token into the user data
try {
$ionicUser.push('_push.android_tokens', token, true);
} catch(e) {
console.warn('Received push token before user was identified and will not be synced with ionic.io. Make sure to call $ionicUser.identify() before calling $ionicPush.register.');
}
}
return {
/**
* Register for push notifications.
*
* Configure the default notification behavior by using the options param:
*
* {
* // Whether to allow notifications to pop up an alert while in the app.
* // Setting this to false lets you control the push behavior more closely.
* allowAlert: true/false (default: true)
*
* // Whether to allow notifications to update the badge
* allowBadge: true/false (default: true)
*
* // Whether to allow notifications to play a sound
* allowSound: true/false (default: true)
*
* // Whether to run auto actions, like navigating to a state, when a push
* // is opened outside of the app (foreground is false)
* canRunActionsOnWake: true/false (default: true)
*
* // A callback to do some custom task on notification
* onNotification: true/false (default: true)
* }
*/
register: function(options, metadata){
if(!app) { return; }
options = angular.extend({
canShowAlert: true,
canSetBadge: true,
canPlaySound: true,
canRunActionsOnWake: true,
onNotification: function() { return true; },
onTokenRecieved: function(token) { }
}, options);
return init(options, metadata);
},
unregister: function(options) {
return $cordovaPush.unregister(options);
}
}
}])
.factory('$ionicPushActions', [
'$rootElement',
'$injector',
function($rootElement, $injector) {
return {
run: function(notification) {
if(notification.$state) {
// Auto navigate to state
var injector = $rootElement.injector();
if(injector.has('$state')) {
$state = injector.get('$state');
var p = {};
try {
p = JSON.parse(notification.$stateParams);
} catch(e) {
}
$state.go(notification.$state, p);
}
}
}
}
}])