Skip to content

Commit

Permalink
fix(messaging): RemoteMessage.data may be JSON-serializable object as…
Browse files Browse the repository at this point in the history
… well as string (#7316)
  • Loading branch information
ccorneliusHsv authored Sep 20, 2023
1 parent eede992 commit 7945a24
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/messaging/e2e/remoteMessage.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ describe('remoteMessage modular', function () {
await firebase.messaging().sendMessage({
data: {
foo: 'bar',
fooObject: { image: 'testURL' },
},
});
} else {
Expand Down Expand Up @@ -423,6 +424,7 @@ describe('remoteMessage modular', function () {
await sendMessage(getMessaging(), {
data: {
foo: 'bar',
fooObject: { image: 'testURL' },
},
});
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/messaging/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export namespace FirebaseMessagingTypes {
/**
* Any additional data sent with the message.
*/
data?: { [key: string]: string };
data?: { [key: string]: string | object };

/**
* Additional NotificationPayload data sent with the message
Expand Down
16 changes: 15 additions & 1 deletion packages/messaging/lib/remoteMessageOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,21 @@ export default function remoteMessageOptions(messagingSenderId, remoteMessage) {
} else if (!isObject(remoteMessage.data)) {
throw new Error("'remoteMessage.data' expected an object value");
} else {
out.data = remoteMessage.data;
// Serialize all objects to strings
out.data = {};
for (let key in remoteMessage.data) {
if (remoteMessage.data.hasOwnProperty(key)) {
if (
typeof remoteMessage.data[key] === 'object' &&
!Array.isArray(remoteMessage.data[key]) &&
remoteMessage.data[key] !== null
) {
out.data[key] = JSON.stringify(remoteMessage.data[key]);
} else {
out.data[key] = remoteMessage.data[key];
}
}
}
}

if (remoteMessage.collapseKey) {
Expand Down

1 comment on commit 7945a24

@vercel
Copy link

@vercel vercel bot commented on 7945a24 Sep 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.