-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcordova-both.js
163 lines (132 loc) · 4.96 KB
/
cordova-both.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
Cordova = {};
if (Meteor.isCordova) {
NotificationClient = function(options) {
if (!options || !options.senderId) {
throw new Meteor.Error('required_options', 'senderId must be supplied as an option as a minimum');
}
var instance = {};
var successHandler = options.successHandler || function(data) {
console.log("Success: " + JSON.stringify(data));
};
var errorHandler = options.errorHandler || function(e) {
console.log("Error " + e);
};
var messageHandler = options.messageHandler || function(payload, foreground, coldstart) {
if (!payload) return null;
if (foreground && !coldstart) {
navigator.notification.alert(
payload.message,
options.alertCallback,
payload.title
);
} else {
window.plugin.notification.local.add(
_.extend({}, options.notificationOptions, {
message: payload.message,
title: payload.title,
autoCancel: true
})
);
}
};
Cordova.onNotificationGCM = options.onNotificationGCM || function(res) {
if (res.event === 'registered') {
if (res.regid) {
Meteor.call('cordova-notifications/updateRegid', res.regid, options.registeredCallback && options.registeredCallback.bind(res));
}
} else if (res.event === 'message') {
messageHandler(res.payload, res.foreground, res.coldstart);
}
}
Tracker.autorun(function(c) {
if (Meteor.user()) {
if (device.platform.toLowerCase() === 'android') {
window.plugins.pushNotification.register(successHandler, errorHandler, {
"senderID": options.senderId.toString(),
"ecb": "Cordova.onNotificationGCM"
});
} else {
// TODO: APN HANDLER REGISTRATION HERE
}
}
});
return instance
}
} else if (Meteor.isServer) {
NotificationClient = function(options) {
if (!options || !options.gcmAuthorization || !options.senderId) {
return false;
}
var Future = Npm.require('fibers/future'),
instance = {};
instance.sendNotification = function(users, data) {
if (typeof users === 'string')
users = Meteor.users.find(users).fetch();
else if (typeof users === "object" && users._id)
users = [users];
else if (users instanceof Mongo.Cursor)
users = users.fetch()
else if (!users instanceof Array)
throw new Meteor.Error('bad_users_argument', 'Supplied user(s) data is not one of: user id, user object, cursor, array of user objects.');
var regids = _.without(
_.pluck(users, 'regid'),
undefined),
payload = {
registration_ids: regids,
data: data
},
headers = {
'Content-Type': 'application/json',
'Authorization': 'key=' + options.gcmAuthorization
},
url = "https://android.googleapis.com/gcm/send",
fut = new Future();
if (regids.length) {
HTTP.post(url, {
headers: headers,
data: payload
},
function(err, res) {
if (err) {
fut.throw(err);
} else {
fut.return({
response: res,
userCount: regids.length
});
}
}
);
} else {
fut.return(null);
}
return fut.wait();
};
Meteor.methods({
'cordova-notifications/updateRegid': function(regid) {
check && check(regid, String);
Meteor.users.update(this.userId, {
$set: {
regid: regid
}
});
}
});
if (options.removeOnLogout) {
Meteor.users.find({
"status.online": true
}).observe({
removed: function(user) {
Meteor.users.update(user._id, {
$unset: {
regid: true
}
});
}
});
}
return instance;
}
} else {
NotificationClient = function() {};
}