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!
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.