forked from nandarustam/fcm-push
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathexample.js
84 lines (68 loc) · 1.96 KB
/
example.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
/**
* Created by Leonardo on 02/08/2016.
*/
FCM = require('fcm-node');
var SERVER_API_KEY='your_api_key';//put your api key here
var validDeviceRegistrationToken = 'c1m7I:A ... bjj4SK-'; //put a valid device token here
var fcmCli= new FCM(SERVER_API_KEY);
var payloadOK = {
to: validDeviceRegistrationToken,
data: { //some data object (optional)
url: 'news',
foo:'fooooooooooooo',
bar:'bar bar bar'
},
priority: 'high',
content_available: true,
notification: { //notification object
title: 'HELLO', body: 'World!', sound : "default", badge: "1"
}
};
var payloadError = {
to: "4564654654654654", //invalid registration token
data: {
url: "news"
},
priority: 'high',
content_available: true,
notification: { title: 'TEST HELLO', body: '123', sound : "default", badge: "1" }
};
var payloadMulticast = {
registration_ids:["4564654654654654",
'123123123',
validDeviceRegistrationToken, //valid token among invalid tokens to see the error and ok response
'123133213123123'],
data: {
url: "news"
},
priority: 'high',
content_available: true,
notification: { title: 'Hello', body: 'Multicast', sound : "default", badge: "1" }
};
var callbackLog = function (sender, err, res) {
console.log("\n__________________________________")
console.log("\t"+sender);
console.log("----------------------------------")
console.log("err="+err);
console.log("res="+res);
console.log("----------------------------------\n>>>");
};
function sendOK()
{
fcmCli.send(payloadOK,function(err,res){
callbackLog('sendOK',err,res);
});
}
function sendError() {
fcmCli.send(payloadError,function(err,res){
callbackLog('sendError',err,res);
});
}
function sendMulticast(){
fcmCli.send(payloadMulticast,function(err,res){
callbackLog('sendMulticast',err,res);
});
}
sendOK();
sendMulticast();
sendError();