0
I am working on a project with Ionic 3 and backend in php/mysql. At the current stage I need to send push notifications and chose to work with firebase and phonegap-plugin-push. I installed and tested via firebase console and the notification was sent correctly. Having done that, I came up with a php template for sending notifications without using the firebase console, but I found a difficulty: I don’t know how to send a notification to all devices (and not by id).
Follow the PHP code used:
<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
// set only for one for safety
$registrationId = $_GET['id'];
$texto = $_GET['texto'];
// prep the bundle
$msg = array
(
'title' => 'Notificação',
'body' => $texto,
'icon' => 'myicon',
'sound' => 'default'
);
$fields = array
(
'priority' => 'high',
'to' => $registrationId,
'notification' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, true );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
?>
Note. 1: The firebase console has both options.
Note. 2: The above code works correctly when informed the id.
What to put in the 'to' => $registrationId,
so that all devices receive notification?
Why not inform the ID? and it is mandatory
– Rafael Augusto
Have you tried using the Onesignal platform? I use it and it’s really good. (https://onesignal.com/) If you need help using it, it says there that I can help you.
– Leonardo Belintani
What would the code look like to use the oneSignal platform?
– Elaynne Almeida