Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
FCM Cloud messaging validate tokens

I have developed a web app with FCM Push Notifications.

It is sending and running fine except the following problem I have:

When someone subscribes, the token I get by the response, I save it into my mySQL database with some other user info I want.

When someone unsubscribes, after I get a successful response of unsubscribe, I delete the entry in my database.

BUT when someone unsubscribes by himself by not using my web application but by using the client (Chrome, Firefox etc) and by removing and not getting any more notifications from my web application, then I don't know that he did it in order to remove him from my database.

I don't see there is an FCM function which gives you all the active tokens in order to check and compare them with my database.

Is there any way to remove from my database all the inactive tokens?

Thank you!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

One can validate these in batches of 500 registration ID each:

use Kreait\Firebase\Factory;
use Kreait\Firebase\Contract\Messaging;
use Kreait\Firebase\Exception\MessagingException;
use Kreait\Firebase\Exception\FirebaseException;

class SomeController {

    /** Constructor */
    public function __construct() {
        $factory = (new Factory)->withServiceAccount(getenv('GOOGLE_APPLICATION_CREDENTIALS'));
        $this->messaging = $factory->createMessaging();

    }

    /**
     * It validates device registration ID.
     *
     * @param string|string[] $token
     * @return array|bool|Exception|MessagingException|FirebaseException the validation results.
     */
    public function validate_fcm_token( string|array $token=[] ): bool|Exception|MessagingException|array|FirebaseException {
        if ($token == null|| is_array($token) && (sizeof($token) === 0 || sizeof($token) > 500)) {return false;}
        else if (is_string($token)) {$token = [ $token ];}
        try {
            return $this->messaging->validateRegistrationTokens($token);
        } catch ( MessagingException | FirebaseException $e ) {
            return $e;
        }
    }
}

This generally splits the input array into three output arrays, of which one is called invalid.

... also see the corresponding documentation.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!