I solved my problem, the error was I was sending notifications to wrong API (FCM). Now I’m sending notifications to Ionic Api, passing my API Key, generated in the Ionic App Dashboard. Here the code:
/* Notification */
function push_notification($notification_id) {
if ( ! defined('API_ACCESS_KEY') ) {
define( "API_ACCESS_KEY", "Firebase SENDER ID" );
}
if ( ! defined('GOOGLE_FCM_URL') ) {
define( "GOOGLE_FCM_URL", "https://api.ionic.io/push/notifications" );
}
global $wpdb;
$message = "New push";
$title = "Hey, listen!";
$registrationIds = array();
$devices_data = $wpdb->get_results( "SELECT * FROM wp_devices_notifications where status = 1" );
foreach ($devices_data as $data) {
array_push($registrationIds, $data->registration_id);
}
$fields = array(
'tokens' => $registrationIds,
'profile' =>'<tag here>' (you can find id in Ionic Dashboard -> Settings -> Certificates),
'notification' => array( "title" => $title, "message" => $message )
);
var_dump($fields);
$headers = array(
'Authorization: Bearer <here App Token, generated in Ionic Dashboard -> App -> Settings -> API Keys>,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, GOOGLE_FCM_URL );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec( $ch );
if ( $result === FALSE ) {
$result = curl_error( $ch );
}
curl_close( $ch );
return $result;
}
I hope this helps.
Greetings!