Skip to content

Commit c277178

Browse files
ArtursKadikisCihad Tekin
authored andcommitted
Merge pull request #5598 from Countly/SER-1963-push-buttons-and-badges-are-not-working
[push] fixed migration related push payload issues
1 parent 89dfa02 commit c277178

File tree

1 file changed

+37
-33
lines changed
  • plugins/push/api/send/platforms

1 file changed

+37
-33
lines changed

plugins/push/api/send/platforms/a.js

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const key = 'a';
1818
* - has its own compilation part;
1919
* - has its own sending part;
2020
* - has no distinct representation in UI, therefore it's virtual.
21-
*
21+
*
2222
* Huawei push is only available on select Android devices, therefore it doesn't deserve a separate checkbox in UI from users perspective.
2323
* Yet notification payload, provider communication and a few other things are different, therefore it's a virtual platform. You can send to huawei directly using
2424
* API, but whenever you send to Android you'll also send to huawei if Huawei credentials are set.
@@ -27,7 +27,7 @@ const virtuals = ['h'];
2727

2828
/**
2929
* Extract token & field from token_session request
30-
*
30+
*
3131
* @param {object} qstring request params
3232
* @returns {string[]|undefined} array of [platform, field, token] if qstring has platform-specific token data, undefined otherwise
3333
*/
@@ -40,7 +40,7 @@ function extractor(qstring) {
4040

4141
/**
4242
* Make an estimated guess about request platform
43-
*
43+
*
4444
* @param {string} userAgent user-agent header
4545
* @returns {string} platform key if it looks like request made by this platform
4646
*/
@@ -104,8 +104,8 @@ class FCM extends Splitter {
104104
}
105105

106106
/**
107-
* Compile & send messages
108-
*
107+
* Compile & send messages
108+
*
109109
* @param {Object[]} data pushes to send, no more than 500 per function call as enforced by stream writableHighWaterMark
110110
* @param {integer} length number of bytes in data
111111
* @returns {Promise} sending promise
@@ -139,12 +139,26 @@ class FCM extends Splitter {
139139
const one = Math.ceil(bytes / pushes.length);
140140
let content = this.template(pushes[0].m).compile(pushes[0]);
141141

142+
// new fcm api doesn't allow objects or arrays inside "data" property
143+
if (content.data && typeof content.data === "object") {
144+
for (let prop in content.data) {
145+
switch (typeof content.data[prop]) {
146+
case "object":
147+
content.data[prop] = JSON.stringify(content.data[prop]);
148+
break;
149+
case "number":
150+
content.data[prop] = String(content.data[prop]);
151+
break;
152+
}
153+
}
154+
}
155+
142156
let printBody = false;
143157
const oks = [];
144158
const errors = {};
145159
/**
146160
* Get an error for given code & message, create it if it doesn't exist yet
147-
*
161+
*
148162
* @param {number} code error code
149163
* @param {string} message error message
150164
* @returns {SendError} error instance
@@ -158,16 +172,6 @@ class FCM extends Splitter {
158172
};
159173
if (!this.legacyApi) {
160174
const tokens = pushes.map(p => p.t);
161-
162-
// new fcm api doesn't allow objects or arrays inside "data" property
163-
if (content.data && typeof content.data === "object") {
164-
for (let prop in content.data) {
165-
if (content.data[prop] && typeof content.data[prop] === "object") {
166-
content.data[prop] = JSON.stringify(content.data[prop]);
167-
}
168-
}
169-
}
170-
171175
const messages = tokens.map(token => ({
172176
token,
173177
...content,
@@ -375,7 +379,7 @@ class FCM extends Splitter {
375379

376380
/**
377381
* Create new empty payload for the note object given
378-
*
382+
*
379383
* @param {Message} msg NMessageote object
380384
* @returns {object} empty payload object
381385
*/
@@ -385,7 +389,7 @@ function empty(msg) {
385389

386390
/**
387391
* Finish data object after setting all the properties
388-
*
392+
*
389393
* @param {object} data platform-specific data to finalize
390394
* @return {object} resulting object
391395
*/
@@ -414,7 +418,7 @@ const fields = [
414418
*/
415419
const map = {
416420
/**
417-
* Sends sound
421+
* Sends sound
418422
* @param {Template} t template
419423
* @param {string} sound sound string
420424
*/
@@ -425,7 +429,7 @@ const map = {
425429
},
426430

427431
/**
428-
* Sends badge
432+
* Sends badge
429433
* @param {Template} t template
430434
* @param {number} badge badge (0..N)
431435
*/
@@ -436,7 +440,7 @@ const map = {
436440
/**
437441
* Sends buttons
438442
* !NOTE! buttons & messagePerLocale are inter-dependent as buttons urls/titles are locale-specific
439-
*
443+
*
440444
* @param {Template} t template
441445
* @param {number} buttons buttons (1..2)
442446
*/
@@ -448,7 +452,7 @@ const map = {
448452

449453
/**
450454
* Set title string
451-
*
455+
*
452456
* @param {Template} t template
453457
* @param {String} title title string
454458
*/
@@ -458,7 +462,7 @@ const map = {
458462

459463
/**
460464
* Set message string
461-
*
465+
*
462466
* @param {Template} t template
463467
* @param {String} message message string
464468
*/
@@ -468,7 +472,7 @@ const map = {
468472

469473
/**
470474
* Send collapse_key.
471-
*
475+
*
472476
* @param {Template} template template
473477
* @param {boolean} ck collapseKey of the Content
474478
*/
@@ -480,7 +484,7 @@ const map = {
480484

481485
/**
482486
* Send timeToLive.
483-
*
487+
*
484488
* @param {Template} template template
485489
* @param {boolean} ttl timeToLive of the Content
486490
*/
@@ -492,7 +496,7 @@ const map = {
492496

493497
/**
494498
* Send notification-tap url
495-
*
499+
*
496500
* @param {Template} template template
497501
* @param {string} url on-tap url
498502
*/
@@ -503,7 +507,7 @@ const map = {
503507
/**
504508
* Send media (picture, video, gif, etc) along with the message.
505509
* Sets mutable-content in order for iOS extension to be run.
506-
*
510+
*
507511
* @param {Template} template template
508512
* @param {string} media attached media url
509513
*/
@@ -513,7 +517,7 @@ const map = {
513517

514518
/**
515519
* Sends custom data along with the message
516-
*
520+
*
517521
* @param {Template} template template
518522
* @param {Object} data data to be sent
519523
*/
@@ -523,7 +527,7 @@ const map = {
523527

524528
/**
525529
* Sends user props along with the message
526-
*
530+
*
527531
* @param {Template} template template
528532
* @param {[string]} extras extra user props to be sent
529533
* @param {Object} data personalization
@@ -539,7 +543,7 @@ const map = {
539543

540544
/**
541545
* Sends platform specific fields
542-
*
546+
*
543547
* @param {Template} template template
544548
* @param {object} specific platform specific props to be sent
545549
*/
@@ -575,7 +579,7 @@ const CREDS = {
575579
'fcm': class FCMCreds extends Creds {
576580
/**
577581
* Validation scheme of this class
578-
*
582+
*
579583
* @returns {object} validateArgs scheme
580584
*/
581585
static get scheme() {
@@ -588,7 +592,7 @@ const CREDS = {
588592

589593
/**
590594
* Check credentials for correctness, throw PushError otherwise
591-
*
595+
*
592596
* @throws PushError in case the check fails
593597
* @returns {undefined}
594598
*/
@@ -631,7 +635,7 @@ const CREDS = {
631635

632636
/**
633637
* "View" json, that is some truncated/simplified version of credentials that is "ok" to display
634-
*
638+
*
635639
* @returns {object} json without sensitive information
636640
*/
637641
get view() {

0 commit comments

Comments
 (0)