push-receiver
push-receiver copied to clipboard
Sending using the new FCM HTTP v1. Missing `notification` key
Firebase Cloud Messaging (FCM) introduced a new API for sending messages (HTTP v1).
Using the new HTTP v1 API, push-receiver
receives the message, but not the payload. E.g, the notification
key is missing:
{
from: '999999999999',
priority: 'normal',
fcmMessageId: '3458a373-9c7d-43a6-a0cd-88a08e76188c'
}
Using the old legacy API the notification
key is still there (as expected):
{
from: '999999999999',
priority: 'normal',
notification: { title: 'Hello world', body: 'Test' },
fcmMessageId: '8742c37f-d702-4b6c-9919-39bfdb0808a6'
}
The old legacy API has been deprecated, and will be removed on June 20th, 2024. See: https://firebase.google.com/docs/cloud-messaging/migrate-v1
To reproduce
The repo's test notification script doesn't work with FCM's new HTTP v1 API (Sending authentication has changed, and doesn't use a "Server key" anymore).
Instead, send the notification using OneSignal:
- Sign up for OneSignal.
- Connect OneSignal to Firebase.
- Setup
push-receiver
- (This'll give you the
$FCMTokenCode
you'll need below)
- (This'll give you the
- Create a OneSignal user using OneSignal's API (I used PHP):
<?php $FCMTokenCode = 'dwj7dLxrm7g:APA91bEreSxR71lQhfjq1o4y9yBT_CYGz_zpwrBqUyw95cmO6aDuYcCIE1jrDGr1s_FgNF27XgeePQScUVAjIDE_RdgAp-JzlYnoUsrMU83JBthg2dHKf1Po-Of6RzNVp7ox2spVRO6T'; $fieldsArr = [ 'identity' => [ 'external_id' => 'fcmTestId', ], 'subscriptions' => [ [ 'type' => 'AndroidPush', 'token' => $FCMTokenCode, ] ], ]; $fieldsArrJson = json_encode($fieldsArr, JSON_THROW_ON_ERROR); // Allow no $Z->devOnlyJsonPrettyPrintNum. This gets saved in Redis. $curlObj = curl_init(); curl_setopt($curlObj, CURLOPT_URL, 'https://onesignal.com/api/v1/apps/5b625de5-6934-41b4-afa4-95b94df5c73a/users'); curl_setopt($curlObj, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, true); curl_setopt($curlObj, CURLOPT_HEADER, true); curl_setopt($curlObj, CURLOPT_POST, true); curl_setopt($curlObj, CURLOPT_POSTFIELDS, $fieldsArrJson); $responseArrJson = curl_exec($curlObj); curl_close($curlObj); echo $responseArrJson;
- Copy the subscription ID from the result of the API call.
- Mark the new user's subscription ID as a "Test subscription":
- Open OneSignal.
- Go to: Audience > Subscriptions.
- Search for the subscription ID.
- Click: Three dots > Add to Test Subscriptions.
- Create a new "Test Users" segment.
- Open OneSignal.
- Go to: Audience > Segments > New Segment.
- Click: "Test Users"
- Click: "Create Segment"
- Send a OneSignal notification:
- Open OneSignal.
- Messages > Push > New Message > New Push.
- Click: "Send to particular segment(s)"
- Choose: "Test Users" (the segment you've just created)
- Send.
- Watch
push-receiver
for the received notification, and the missingnotification
key.
Other notes
OneSignal recently switched to FCM's new HTTP v1 API exclusively, so my Electron app's notifications don't work anymore 😢.
Thanks for this amazing package! OneSignal covers all my platforms except Electron, so I'm very grateful this package bridges the gap.
Hey @Mike-Wood , I rewrote the push-receiver library from scratch, I did try it with notifications sent from the new v1 API so you should not have this issue with my library!
Documentation: Aracna FCM Docs Source: Aracna FCM Source
Let me know how it goes for you 👍
As a side note, strangely enough, Even when using HTTP v1, the data contained in the "data" payload still appears to be transmitted well.
for example,
{
from: '999999999999',
priority: 'normal',
data: { title: 'Hello world', body: 'Test' },
fcmMessageId: '8742c37f-d702-4b6c-9919-39bfdb0808a6'
}
The above json is perfectly received in this library.