· 7 years ago · Jul 24, 2018, 03:06 PM
1const AWS = require ('aws-sdk');
2
3AWS.config.update({
4 accessKeyId: 'ACCESS_KEY',
5 secretAccessKey: 'SECRET_KEY',
6 region: 'REGION'
7});
8const sns = new AWS.SNS();
9
10sns.createPlatformEndpoint({
11 PlatformApplicationArn: 'PLATFORM_APPLICATION_ARN',
12 Token: 'DEVICE_TOKEN'
13}, (error, data) => {
14 if (error) {
15 console.log({ error });
16 } else {
17 const endpointArn = data.EndpointArn;
18 //Payload for android
19 let payload = {
20 default: 'Test',
21 GCM: {
22 notification: {
23 body: 'Test body',
24 title: 'Test title',
25 sound: 'default'
26 },
27 data: {
28 key: 'key value',
29 key2: 'key2 value'
30 }
31 }
32 }
33 payload.GCM = JSON.stringify(payload.GCM);
34 // then have to stringify the entire message payload
35 payload = JSON.stringify(payload);
36
37 //Payload for ios
38 // let payload = {
39 // default: 'Test',
40 // APNS: {
41 // aps: {
42 // alert: 'Test alert',
43 // sound: 'default',
44 // badge: 1
45 // }
46 // }
47 // };
48 // payload.APNS = JSON.stringify(payload.APNS);
49 // // then have to stringify the entire message payload
50 // payload = JSON.stringify(payload);
51
52 sns.publish({
53 Message: payload,
54 MessageStructure: 'json',
55 TargetArn: endpointArn
56 }, (error, data) => {
57 if (error) {
58 console.log({ error });
59 }
60 console.log({ data });
61 });
62 }
63});