forked from max-berman/phonegap-push-angular
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathphonegap-push.js
More file actions
144 lines (117 loc) · 4.38 KB
/
phonegap-push.js
File metadata and controls
144 lines (117 loc) · 4.38 KB
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
'use strict';
/**
AngularJS PhoneGap Push Notifications Service
=============================================
Copyright 2013 Anim Yeboah
https://github.com/anim/angular-phonegap-push/
Allows using the [PushPlugin](https://github.com/bobeast/PushPlugin) in
[PhoneGap](http://phonegap.com/) apps built with AngularJS.
Provides a service for registering the device for push notifications and
getting callbacks when notifications are received.
Example
-------
Register device:
```javascript
new pgPushNotificationsFactory(
'0123456789', // GCM Sender ID
function registeredCallback (deviceToken, platform) {
// Send `deviceToken` to your push server here
// `platform` is either 'APNS' (iOS) or 'GCM' (Android)
},
function pushNotificationCallback (data, platform) {
// Push message received
}
);
```
Listen for push notifications:
```javascript
$scope.$on('phonegapPush.notification', function (notification) {
// Notification received:
// `notification.data` raw notification data/payload
// `notification.provider` either 'APNS' or 'GCM'
});
```
*/
angular.module('phonegapPush', [])
.factory('pgPushNotificationsFactory', function ($rootScope, $log) {
var pushNotificationsFactory = function (gcmSenderId, registeredCallback) {
var pushNotification;
/* Setup and register device */
// Check if phonegap and plugins are loaded
if (typeof(window.plugins) === 'undefined') {
$log.error('PhoneGap plugins not found. Push notifications not initialized.');
return false;
}
// Initialize push notifications
pushNotification = window.plugins.pushNotification;
if (typeof(pushNotification) === 'undefined') {
$log.error('Push plugin not found. Push notifications not initialized.');
return false;
}
var gcmSuccessHandler = function (result) {
$log.info(
'Successfully registered with GCM push server. ' +
'Waiting for device registration ID via notification. ' +
'Registration result:', result
);
};
var apnsSuccessHandler = function (deviceToken) {
$log.info('Successfully registered with APNS push server. Device token:', deviceToken);
registeredCallback(deviceToken, 'APNS');
};
var genericErrorHandler = function (error) {
$log.error('Error registering with push server:', error);
};
// Register device with push server
if (device.platform === 'Android') {
pushNotification.register(gcmSuccessHandler, genericErrorHandler, {
'senderID': gcmSenderId,
'ecb': 'onNotificationGCM'
});
} else if (device.platform === 'iOS') {
pushNotification.register(apnsSuccessHandler, genericErrorHandler, {
'badge': 'true',
'sound': 'true',
'alert': 'true',
'ecb': 'onNotificationAPN'
});
}
/* Bind notification functions to window (called by phonegapPush plugin) */
// iOS notification received
window.onNotificationAPN = function (notification) {
$log.info('APNS push notification received:', notification);
$rootScope.$broadcast('phonegapPush.notification', {
data: notification,
provider: 'APNS'
});
};
// Android notification received
window.onNotificationGCM = function (notification) {
switch (notification.event) {
case 'registered':
if (notification.regid.length > 0) {
$log.info('Got GCM device registration ID:', notification.regid);
registeredCallback(notification.regid, 'GCM');
} else {
$log.error('Error registering with GCM push server: No device registration ID received.');
}
break;
case 'message':
$log.info('GCM push notification received (only payload forwarded):', notification);
$rootScope.$broadcast('phonegapPush.notification', {
data: notification.payload,
provider: 'GCM'
});
break;
case 'error':
$log.error('Error while receiving GCM push notification:', notification);
break;
default:
$log.error('Unknown GCM push notification received:', notification);
break;
}
};
return true;
};
return pushNotificationsFactory;
});