push-receiver icon indicating copy to clipboard operation
push-receiver copied to clipboard

Sending using the new FCM HTTP v1. Missing `notification` key

Open joshua-redmond opened this issue 1 year ago • 2 comments

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:

  1. Sign up for OneSignal.
  2. Connect OneSignal to Firebase.
  3. Setup push-receiver
    • (This'll give you the $FCMTokenCode you'll need below)
  4. 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.
  5. Mark the new user's subscription ID as a "Test subscription":
    1. Open OneSignal.
    2. Go to: Audience > Subscriptions.
    3. Search for the subscription ID.
    4. Click: Three dots > Add to Test Subscriptions.
  6. Create a new "Test Users" segment.
    1. Open OneSignal.
    2. Go to: Audience > Segments > New Segment.
    3. Click: "Test Users"
    4. Click: "Create Segment"
  7. Send a OneSignal notification:
    1. Open OneSignal.
    2. Messages > Push > New Message > New Push.
    3. Click: "Send to particular segment(s)"
    4. Choose: "Test Users" (the segment you've just created)
    5. Send.
  8. Watch push-receiver for the received notification, and the missing notification 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.

joshua-redmond avatar Dec 21 '23 04:12 joshua-redmond

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 👍

alchemicas avatar Dec 25 '23 18:12 alchemicas

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.

choiman1559 avatar Jan 23 '24 14:01 choiman1559