php-firebase-cloud-messaging
php-firebase-cloud-messaging copied to clipboard
Background android notification
If I sent the notification to an individual devices as shown in the documentation, if the app is background and we click on it in the noitification section the app does not open.
I have tried everything, please help!
How are sending the notification? Can you paste your code?
FCM documentation states that only Data Notifications are caught by the app when in background. Instead Message Notifications are only when app in in foreground.
Like that:
$client = new Client();
$client->setApiKey($this->key);
$client->injectGuzzleHttpClient(new ClientHttp());
$message = new Message();
$message->setPriority('high');
$notif = new Notification($title, $messageText);
$notif->setClickAction('MainApplication');
$notif->setSound("default");
$message->addRecipient(new Device($user->getFirebaseToken()));
$message
->setNotification($notif)
->setData($data);
$response = $client->send($message);
The issue is in the fact that you send the notification as a Message Notification instead of Data Notification.
$notif = new Notification($title, $messageText);
Here you are setting the title and body of the notification and this is handled automatically by the device to show the notifications. Instead if you need to make the notification show also when the app is in background you need to leave this 2 properties empty and set title and body in the Data property.
$notif = new Notification();
.... // add all your settings of Notification then:
$message
->setNotification($notif)
->setData([
'title' => 'this is a title',
'body' => 'this is a body'
]);
This will require you to create a listener in the mobile app which will get the title and body and manually generate the notification.