Problem/Motivation

From Google: Apps using the FCM legacy HTTP API should consider migrating to the HTTP v1 API.

For more information:
* https://firebase.googleblog.com/2017/11/whats-new-with-fcm-customizing-m...
* https://firebase.google.com/docs/cloud-messaging/migrate-v1

Steps to reproduce

N/A

Proposed resolution

Migrate to the new HTTP v1 API following the guide provided by Google:
https://firebase.google.com/docs/cloud-messaging/migrate-v1

Remaining tasks

# Update the server endpoint used for sending messages (group management likely still needs to use the legacy endpoint)
# Update the authorization strategy to use OAuth bearer tokens
# Update the payload of send requests

User interface changes

None.

API changes

There may be changes needed to the service API for sending messages.

Data model changes

Not sure.

Issue fork firebase-3163053

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

mediabounds created an issue. See original summary.

samtheman’s picture

Are there any other FCM modules which use v1 protocol?

mkrizaj’s picture

Hi,

Are there any plans to migrate the module to use HTTP v1 API?

Thanks

siarheinik’s picture

StatusFileSize
new27.05 KB

deprecated
Migrate from legacy HTTP to HTTP v1 patch

siarheinik’s picture

deprecated

siarheinik’s picture

StatusFileSize
new58.68 KB

deprecated
Migrate from legacy HTTP to HTTP v1 patch

siarheinik’s picture

StatusFileSize
new59.24 KB

Update PATCH file.
Migrate from legacy HTTP to HTTP v1 patch

mieg’s picture

hi SiarheiNik, thanks for your work! I tried to use your patch but i couldn't get it to work. It seems there is now a dependency on google-auth-library-php? I get this error:

Error Class "Google\Auth\ApplicationDefaultCredentials" not found.

[edit] solved this by adding google/auth to the project

gazsesz’s picture

Hi,
Can we expect an update to version 3.0.1 ?

siarheinik’s picture

Update the configuration form. Added file selection for credentials. The file is saved to the private folder 'file_private_path'

siarheinik’s picture

Fix error message.

drupalfan2’s picture

I recently got this reminder from google:

[Reminder] Update your apps to the latest Firebase Cloud Messaging APIs and SDKs

We’re writing to remind you that starting June 20, 2024 the legacy Firebase Cloud Messaging (FCM) APIs will be discontinued.

On June 20, 2024, we’re reducing the number of Firebase Cloud Messaging (FCM) legacy register APIs and legacy send APIs that provide similar functionality. This step will allow us to provide you with a more consistent experience and align with Google security standards to improve security, reliability and performance.

Because of these API decommissions, some already-deprecated SDKs and features will stop working after June 20, 2024.

What do you need to do?
Take the following actions before June 20, 2024, to ensure that you have access to the latest supported features and to reduce the risk of future decommissions affecting your usage:
•Follow the instructions described in Firebase FAQ to migrate your individual APIs.
•Update to the latest versions of Firebase SDKs:

Android >= 23.1.2
iOS >= 10.10.0
Web (Javascript) >= 9.22.1

Your Firebase project(s) that use Firebase Cloud Messaging APIs are listed below:
◦Your recent usage of impacted APIs/features: Legacy HTTP protocol
◦Your recent usage of impacted APIs/features: Server Keys

Will this patch provide the needed actions?
Will this patch update the "Legacy HTTP protocol" and also the "Server Keys" topic?
Will this patch change over to the latest api versions?

Thank you.

deadbeef’s picture

Here's an update which fixes among other things:

  • Allows Google credentials path to be set in the environment through $_ENV or otherwise ( credentials files inside the webroot aren't really desirable ).
  • Fixes sendToTopic() to use the condition field if topics field is not set
  • Fixes the jsonSerialize function declaration of FCMOptions to return array and avoid warning
  • Misc coding standards fixes.
ptmkenny’s picture

Let's use a merge request to handle this because the changes are complicated and extensive. This should probably be a new major version of the module since the API is changing.

ptmkenny’s picture

Version: 8.x-2.0 » 3.0.x-dev
Category: Feature request » Task
Priority: Normal » Major

ptmkenny’s picture

Status: Active » Needs work
deadbeef’s picture

@ptmkenny If it's ok with @SiarheiNik I will refactor using config as soon as I get time and try to keep the interface as close to the legacy module as possible. We just needed this to work like yesterday with our credentials policy.

Another thing to discuss is whether typing the various Android/iOS bits and pieces adds enough value. The message itself is a simple, well-documented JSON string and just building it and sending it is very easy to just template, rather than instantiating loads of objects to later then just simply render it out anyway.

ptmkenny’s picture

@deadbeef Thank you for the additional information.

Another thing to discuss is whether typing the various Android/iOS bits and pieces adds enough value. The message itself is a simple, well-documented JSON string and just building it and sending it is very easy to just template, rather than instantiating loads of objects to later then just simply render it out anyway.

I think it would be better to build the string than to instantiate lots of objects; this API migration makes the code more complex, so I think we should try to keep it as simple as possible.

ptmkenny’s picture

@deadbeef Interesting. As you suggest, it might make more sense to make a wrapper around that package than to rework the code in this module when that package has already done the work.

At the same time, the big problem is this issue was opened three years ago and never got a single comment from any of the listed maintainers, so if such a wrapper was going to be written, it might be better as a new module than as a new major version of this one, since this module is effectively unmaintained.

I emailed @thalles, the last maintainer to commit to this module, on April 11 but haven't heard back.

leopaccanaro’s picture

Hello @ptmkenny.

Unfortunately I am focused more on Java projects and with a special daughter. Sorry to hear @thalles and @renatog are not active around here.
I know Renato and I will talk to him later. I think is on vacation right now.
In the mean time, any of you want to maintain the module? Let me know.

ptmkenny’s picture

@leopaccanaro Thank you for the response.

As suggested by @deadbeef, firebase-php does 90% of what we want to do already, so it makes sense to use that going forward.

To explore this option, I am currently working on https://www.drupal.org/project/firebase_php.

waropd’s picture

Version: 3.0.x-dev » 3.0.1
StatusFileSize
new32.18 KB

Pending an update on https://www.drupal.org/project/firebase_php I'm currently using the patch.
When using firebase in config split and removing the credentials_path locally in the settings, the upload works but the json credentials file isn't saved to the field anymore.

Remove the logger and give the option to return NULL.

waropd’s picture

Include new entities in patch.

rares petru samartean’s picture

Perhaps this is not proper reporting but I should mention it.

The google credentials file does not stay set properly. I added it in the configuration form, the form saved properly and I could see it saved.
After a bit of time I noticed I was no longer getting notifications from my local env, even though I was getting them before.
I went to check the form and saw that a different, random PNG file was assigned instead of the credentials file.
I readded it and moved on.
After a while, again, instead of being reassinged randomly, it got deleted completely and was no longer appearing as set in the form.

I haven't looked into why it happens yet. I used the v10 patch from #13.
Also, setting the config value programatically using something like

$settings['firebase.settings']['credentials_path'] = 'private://firebase/inkjin-e4091-84c8a56e2a6c.json';

doesn't seem to work, or it at least doesn't get recognised in the form. I don't know which one exactly yet as I'm pretty overwhelmed.

Hope this helps you guys. I'll post a fix if I ever find the time to I hope.

rares petru samartean’s picture

Hi again. I'm not sure about setting the path using settings.php since it's not actually a path but a file ID, but I still think I found the issue.

The generated file was being set as temporary. What I did was loading the file by ID in the submit handler of the configuration form, setting it as permanent, then saving it, then setting the file id as the credentials_path config.

Hope this helps. I can't contribute code from my workplace but that should be good enough for you guys to have at it. Thank you for your contributions!

venezonano’s picture

Hello Team.

I also need to fix the module, Can you let me know if I need to apply all patches or only the last one?

john.karam’s picture

Thank you! I have applied the latest patch in #24 and i have succesfully uploaded the file with the credentials.
However, this broke the functionality and it doesn't send notifications anymore. The code I use is similar to the modules documentation:

// Token is generated by app. You'll have to send the token to Drupal.
$fakeToken = 'e3vUiwcvkpY:APA91bEDZzKTIkaL0e-UTwiV6EGi1m5J5PrDMxejm6-d85vdwAgd';
$messageService = \Drupal::service('firebase.message');
$messageService->setRecipients($fakeToken);
$messageService->setNotification([
  'title' => 'Title goes here',
  'body' => 'Body goes here',
  'badge' => 1,
  'icon' => 'optional-icon',
  'sound' => 'optional-sound',
  'click_action' => 'optional-action',
]);
$messageService->setData([
  'score' => '3x1',
  'date' => '2017-10-10',
  'optional' => 'Data is used to send silent pushes. Otherwise, optional.',
]);
$messageService->setOptions(['priority' => 'normal']);
$messageService->send();

It seems that the setNotification does not exist anymore and I am not sure what other changes are done.

Could someone provide me with an example (instructions/code) on how to send a notification after applying the patch?

tri tran’s picture

Should be like this:

 /** @var FirebaseMessageService $messageService */
 $messageService = \Drupal::service('firebase.message');
 $messageService->setRecipients($fcmToken);
 // Notification
 $notification = new Notification('Title goes here');
 $notification->setBody('Body goes here');
 // Message data
 $messageData = MessageData::fromArray([
  'score' => '3x1',
  'date' => '2017-10-10',
  'optional' => 'Data is used to send silent pushes. Otherwise, optional.',
]);
 // Message
 $message = new Message($notification);
 $message->setData($messageData);
 $messageService->setMessage($message);
 // Android config
 $android_config = new AndroidConfig();
 $android_config->setPriority('high');
 $message->setAndroidConfig($android_config);
 // Apple config
 $apns_config = new ApnsConfig();
 $apns_config->setHeaders(['apns-priority' => '5']);
 $apns_config->setPayload([
      'aps'=> [
        'mutable-content' => 1,
        'content-available' => 1
      ]
    ]);
$message->setApnsConfig($apns_config);

$result = $messageService->sendToTokens();
spheresh’s picture

The credentials path contains the id file. This can become a problem when migrating configs as for me.

This is an initial solution that uses the key module, which works well with http-v1
https://git.drupalcode.org/issue/firebase-3163053/-/tree/http-v1

The only problem is that GOOGLE_APPLICATION_CREDENTIALS requires a file path.

That's why

'#type' => 'key_select',
'#key_filters' => ['provider' => 'file'],

We can only use the file provider.
I have added a @todo to research if we can use $key_entity->getKeyValue() instead.

Because the patch does not allow you to use all the functionality of the keys module for now.

john.karam’s picture

Tri Tran thank you for your code!

I am now receiving the following error: "Failure code: 403; Failure status: PERMISSION_DENIED; Failure message: Permission 'cloudmessaging.messages.create' denied on resource '//cloudresourcemanager.googleapis.com/projects/MYPROJECTNAME' (or it may not exist)."

Any ideas?

waropd’s picture

Extension of patch #24
Set uploaded file to permanent

Ana Bozhilova’s picture

Hi,
I have similar problem. This patch from #32 comment was not working for me.

divanova’s picture

Hi everyone,
I have installed the latest patch (#32) and updated the new configuration page accordingly.
I've uploaded a .json file which I was given from the Firebase, but it's not working.

Here is the structure of the .json file I received:

type
project_id
private_key_id
private_key
client_email
client_id
auth_uri
token_uri
auth_provider_x509_cert_url
client_x509_cert_url
universe_domain

For the "Project ID" field in the "Firebase Push Notification Configuration" page, I've used the "project_id" from the .json file.
For "Credentials" field, I have uploaded the provided .json file.

The old code is:

function _eutb_firebase_push_message($topic, $val) {
  /** @var FirebaseMessageService $messageService */
  $messageService = \Drupal::service('firebase.message');
  $messageService->setTopics($topic);

  // Notification
  $notification = new Notification($val['title']);
  $notification->setBody($val['body']);

  // Message data
  $messageData = MessageData::fromArray([
    'type' => 'axp-content',
    'url'  => $val['url'],
  ]);

  // Message
  $message = new Message($notification);
  $message->setData($messageData);
  $messageService->setMessage($message);

  // Android config
  $android_config = new AndroidConfig();
  $android_config->setPriority('normal');
  $message->setAndroidConfig($android_config);

  // Apple config
  $apns_config = new ApnsConfig();
  $apns_config->setHeaders(['apns-priority' => '5']);
  $apns_config->setPayload([
    'aps' => [
      'mutable-content' => 1,
      'content-available' => 1
    ]
  ]);
  $message->setApnsConfig($apns_config);

  try {
    $messageService->sendToTopic();
    }
  } catch (Exception $e) {
    $error = 'Push-Message failed: ' . $val['title'] . ' - ' . $val['body'] . ' URL: ' . $val['url'] . ' Error: ' . $e->getMessage();
  }
}

The new code is:

/** @var FirebaseMessageService $messageService */
  $messageService = \Drupal::service('firebase.message');
  $messageService->setTopics($topic);

  // Notification
  $notification = new Notification($val['title']);
  $notification->setBody($val['body']);

  // Message data
  $messageData = MessageData::fromArray([
    'type' => 'axp-content',
    'url'  => $val['url'],
  ]);

  // Message
  $message = new Message($notification);
  $message->setData($messageData);
  $messageService->setMessage($message);

  // Android config
  $android_config = new AndroidConfig();
  $android_config->setPriority('normal');
  $message->setAndroidConfig($android_config);

  // Apple config
  $apns_config = new ApnsConfig();
  $apns_config->setHeaders(['apns-priority' => '5']);
  $apns_config->setPayload([
    'aps' => [
      'mutable-content' => 1,
      'content-available' => 1
    ]
  ]);
  $message->setApnsConfig($apns_config);

  try {
    $messageService->sendToTopic();
  
  } catch (Exception $e) {
    $error = 'Push-Message failed: ' . $val['title'] . ' - ' . $val['body'] . ' URL: ' . $val['url'] . ' Error: ' . $e->getMessage();
  }

After I try to send a push message, I get this error:
Error: Class "Google\Auth\ApplicationDefaultCredentials" not found in Drupal\firebase\Service\FirebaseServiceBase->getHandler()

I saw that mieg (#8) reported the same issue, but this dependancy is already integrated in the latest patch and I have it locally and on my DEV system, but it still complains about it.
Does anyone can help with ideas what might be wrong?

I also tried the example from Tri Tran (#29) with the $messageService->sendToTokens();, but then I have another error: Error: The message should contain target.

Any ideas/thoughts would be highly appreciated!

Regards,
Daniela

Ana Bozhilova’s picture

Hi,
Install "google/cloud-firestore": "^0.1.0"

Ana Bozhilova’s picture

There is a comment below. Please ignore this.

Ana Bozhilova’s picture

Hi,
Here is and a fix for this warning from patch 31

Deprecated function: Return type of Drupal\firebase\Entity\MessageData::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in include()

Ana Bozhilova’s picture

divanova’s picture

Hi Ana Bozhilova,

thanks for your response. Installing "google/cloud-firestore" and applying your latest patch worked for me.
The push messages are sent without any errors.

apparatchik’s picture

Thank you to everyone involved in the successive patches, I can confirm I was able to have notifications working again in my Drupal web app, though not with some work.

A few tips for other people struggling with this:

- After you apply the #38 patch to the latest 3.0.1 version, don't forget to run drush updb or update.php to get all the changes to the module's configuration in the db.

- composer require "google/cloud-firestore"

- The credentials json is found not on the firebase console like usual but on the google cloud console, search for "google service account credentials json"

- This might be obvious, but don't forget to import the needed classes at the top of your file:

use Drupal\firebase\Entity\Message;
use Drupal\firebase\Entity\MessageData;
use Drupal\firebase\Entity\Notification;
use Drupal\firebase\Entity\AndroidConfig;
use Drupal\firebase\Entity\ApnsConfig;

- This is the code that I'm using at the moment:

public static function firebase_message($token, $title, $body, $data)
    {
        $service = Drupal::service('firebase.message');
        $service->setRecipients($token);
        
        $notification = new Notification($title);
        $notification->setBody($body);
        
        $data = MessageData::fromArray($data);
          
        // Message
         $message = new Message($notification);
         $message->setData($data);
         $service->setMessage($message);
         // Android config
         $android_config = new AndroidConfig();
         $android_config->setPriority('high');
         $message->setAndroidConfig($android_config);
         // Apple config
         $apns_config = new ApnsConfig();
         $apns_config->setHeaders(['apns-priority' => '5']);
         $apns_config->setPayload(['aps'=> ['mutable-content' => 1, 'content-available' => 1]]);
         $message->setApnsConfig($apns_config);
        
          $result = $service->sendToTokens();
    }

Regards.

mieg’s picture

Did anybody succeed in sending data-only messages with the latest patches? I couldn't find a way to send a message without a notification.

chandreshgiri gauswami’s picture

Anyone has implemented sendMulticast() method to send common notification to multiple devices or any reference?

https://firebase-php.readthedocs.io/en/7.16.0/cloud-messaging.html#send-...

drupalfan2’s picture

How can I help to create a new dev version that is working with the new API and contains the last patch?

I need a working solution as soon as possible and I am willing to help write some code or do the tests.

What are the next steps to do?

ptmkenny’s picture

@drupalfan2 This module is not being actively maintained, so you can start the process to become a maintainer. If you email the current maintainers, you may get access quickly, or it may take as long as a couple months.

Alternately, I have created the Firebase PHP module, which takes a different approach than this one (using the Kreait library, which is more widely used outside Drupal in the PHP world). Although in alpha, my module works now and I am happy to accept MRs for anything that is not working.

drupalfan2’s picture

@ptmkenny
Thank you for your answer and your info.

In the README file of your firebase_php module I did not find any docs how I can send my Firebase Notifications with custom code in my custom module.
How does it work, where can I find an exmple code etc.?`
Thank you.

ptmkenny’s picture

@drupalfan2 Please open issues with firebase_php in that module's issue queue. The API is in https://git.drupalcode.org/project/firebase_php/-/blob/7.0.x/src/Firebas...

aalin’s picture

Updated #38 patch to apply against the latest dev branch.
I also attached a diff (v34-v48.diff) showing changes from #38 patch.

aalin’s picture

StatusFileSize
new61.83 KB

Fix previous patch file name

aalin’s picture

The previous patch didn’t work on Drupal 11. Attaching an updated patch for D11

lonalore’s picture

Version: 3.0.1 » 3.0.x-dev
StatusFileSize
new65.68 KB
new47.92 KB

I merged the changes from the patch file above (#50) into the MR code then I fixed the following:

  1. I fixed the error that occurred after submitting the config form:

    Warning: Undefined array key 0 in Drupal\firebase\Form\ConfigurationForm->submitForm() (line 116 of modules/contrib/firebase/src/Form/ConfigurationForm.php).

  2. I replaced a deprecated function call with a new service introduced in Drupal v10.2.0:

    system_get_module_admin_tasks() is deprecated (see https://www.drupal.org/node/3038972)

  3. Due to the use of the new service (introduced in Drupal v10.2.0), I increased the minimum Drupal version dependency to 10.2.

I attached 2 new patch files:

  1. diff-merge-request-3163053-51.patch
    This file contains the diff compared to the MergeRequest.
  2. diff-dev-3163053-51.patch
    This file contains the diff compared to the dev branch.
florianj’s picture

Thanks for contributing all these patches.

I’m using the MR and patch file #51, and it seems to work fine.
Is this MR still being reviewed for merging (and possibly the #51 patch, which includes Drupal 11 compatibility) ?