fcm icon indicating copy to clipboard operation
fcm copied to clipboard

Can't send data only notifications because Notification object is required

Open bennnjamin opened this issue 1 year ago • 7 comments

The Notification object should be optional when creating a Message object. Currently, it is not possible to send data only notifications.

See https://firebase.google.com/docs/cloud-messaging/concept-options#data_messages

bennnjamin avatar Feb 14 '24 19:02 bennnjamin

Hello @bennnjamin, thanks for reporting this issue. It should be fix in 3.2. Be aware that this version introduce a BC break, see CHANGELOG for more details.

ker0x avatar Feb 15 '24 09:02 ker0x

I've been experimenting on a fork and there are some other issues too, specifically with array $data = [] being serialized to an empty JSON Object. The FCM API doesn't seem to like it. I'm wondering if you should consider making most constructor properties optional. The documentation isn't clear about what is strictly optional but trial and error seems to indicate most JSON keys are optional. I could open an issue but I think it's ultimately your call on how to handle optional/required keys.

Otherwise this is a great concise library.

bennnjamin avatar Feb 15 '24 16:02 bennnjamin

I'm not against making all parameters optional if there is a specific use case for sending an empty payload (no data and/or notification).

ker0x avatar Feb 16 '24 09:02 ker0x

It's either/or. When you send a data only message, there is no notification key, or title/body anywhere in the JSON object. Only custom keys (see https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages). I'm not sure the best way to handle that honestly. If every parameter optional, then it's on the developer to know how to build an alert or data only notification. Maybe some balance that satisfies the FCM V1 API and is also not overburdensome for the developer?

bennnjamin avatar Feb 16 '24 16:02 bennnjamin

We are running into this issue too, which caught us off-guard when running an initial "simple" test without specifying any data element.

Mike-Benoit avatar Jun 18 '24 16:06 Mike-Benoit

@Mike-Benoit I've forked this repo and fixed the issues on my end, happy to share the changes if you aren't able to get it working.

bennnjamin avatar Jun 18 '24 16:06 bennnjamin

@Mike-Benoit I've forked this repo and fixed the issues on my end, happy to share the changes if you aren't able to get it working.

I've worked around them too, just wanted to share my experiences here mostly.

Mike-Benoit avatar Jun 18 '24 18:06 Mike-Benoit