Skip to content

Sending using the new FCM HTTP v1. Missing notification key #69

@joshua-redmond

Description

@joshua-redmond

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions