-
Notifications
You must be signed in to change notification settings - Fork 0
/
push.js
111 lines (96 loc) · 3.02 KB
/
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
/*
* angular-phonegap-push-notification v0.0.3
* (c) 2014 Patrick Heneise, patrickheneise.com
* License: MIT
*/
'use strict';
angular.module('cordova', [])
.factory('cordovaReady', function ($rootScope, $q, $timeout) {
var loadingDeferred = $q.defer();
document.addEventListener('deviceready', function () {
$timeout(function() {
$rootScope.$apply(loadingDeferred.resolve);
});
});
return function cordovaReady() {
return loadingDeferred.promise;
};
})
.service('phone', function () {
this.isAndroid = function () {
var uagent = navigator.userAgent.toLowerCase();
return uagent.search('android') > -1 ? true : false;
};
})
.factory('push', function ($rootScope, phone, cordovaReady) {
return {
registerPush: function (fn) {
cordovaReady().then(function () {
var
pushNotification = window.plugins.pushNotification,
successHandler = function (result) {},
errorHandler = function (error) {},
tokenHandler = function (result) {
return fn({
'type': 'registration',
'id': result,
'device': 'ios'
});
};
app.onNotificationAPN = function (event) {
if (event.alert) {
navigator.notification.alert(event.alert);
}
if (event.sound) {
var snd = new Media(event.sound);
snd.play();
}
if (event.badge) {
pushNotification.setApplicationIconBadgeNumber(successHandler, errorHandler, event.badge);
}
};
app.onNotificationGCM = function (event) {
switch (event.event) {
case 'registered':
if (event.regid.length > 0) {
return fn({
'type': 'registration',
'id': event.regid,
'device': 'android'
});
}
break;
case 'message':
if (event.foreground) {
var my_media = new Media("/android_asset/www/" + event.soundname);
my_media.play();
} else {
if (event.coldstart) {
} else {
}
}
break;
case 'error':
break;
default:
break;
}
};
if (phone.isAndroid()) {
pushNotification.register(successHandler, errorHandler, {
'senderID': '{your_sender_id}',
'ecb': 'app.onNotificationGCM'
});
} else {
console.log('register ios');
pushNotification.register(tokenHandler, errorHandler, {
'badge': 'true',
'sound': 'true',
'alert': 'true',
'ecb': 'app.onNotificationAPN'
});
}
});
}
};
});