How to open an url on webview on click firebase notification? - php

I'm using php for to send notification. When I add click_action as a parameter, there is nothing on click notification. App doesn't open. But if I don't send click_action everything is okay, app is opening. But of course main page. I just want to specific url on webview on click to notification. How can I do that? Thanks.
$msg = array
(
'body' => 'Test Test',
'title' => 'Test',
'click_action' => 'https://google.com/',
'icon' => 'smallicon',/*Default Icon*/
'sound' => 'mySound'/*Default sound*/
);
$fields = array
(
'to' => 'token',
'notification' => $msg
);
$headers = array
(
'Authorization: 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, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );

Related

I have implemented an FCM server side, Server side not delivering

I am trying to implement a php curl fcm topic broadcasting and it just displays {"message_id":4731721763997571462} and does not deliver.
I have gone through a lot of search and to no end, I can't seem to find the problem.
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'Legacy_server_key' );
// prep the bundle
$msg = array
(
'message' => 'here is a message. message',
'title' => 'This is a title. title',
);
$fields = array
(
'to' => "/topics/hello",
'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, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ));
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
I just expect to get a notification on the device and maybe there is a way I can trace successful notifications on the google console?
You need to add registration ids with field parameter
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'Legacy_server_key' );
$registrationIds = array( "/topics/obajemusagmailcom" );
// prep the bundle
$msg = array
(
'message' => 'here is a message. message',
'title' => 'This is a title. title',
);
$fields = array
(
//'to' => "/topics/hello",
'registration_ids' => $registrationIds,
'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, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ));
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
and $registrationIds = array( "/topics/obajemusagmailcom" ); is not registration id of any app, please correct it.
Finally after much horror and tries, turned out the array fields were incorrect and were meant to be
$msg = array
(
'body' => $body,
'title'=>$title
);
$data = array
(
'to' => "/topics/".$topic,
'notification' => $msg
);

How to send firebase (fcm) to multiple users and different data per user

i want to send fcm to multiple users but i want to send different data to each user , like this.
$deviceid2 ="da10bCm38LU:APA91bHC-XB";
$deviceid1 = "dKF-20xsI9Y:APA91bE";
$API_ACCESS_KEY='AAAA_';
$registrationIds = array( "$deviceid1","$deviceid2" );
$title1="title1";
$title2="title2";
$message1="message1";
$message2="message2";
$title = array( "$title1","$title2" );
$body = array( "$message1","$message2" );
$fields = array
(
'registration_ids' => $registrationIds,
'data' => array(
'title' => $title,
'body' => $body
)
);
$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_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
$result = curl_exec($ch );
curl_close( $ch );
i tried it,the response was success from curl, but no notification appeared on my phone. can anyone help for this?
Then you need to send as many different FCM messages as users. You cannot have one message that is different for different users!

How to send notification to all devices that installed my app using Firebase and PHP?

I'm making an existing application which includes notification when there is new content.
Notification is sent to all the gadgets that have installed the application.
How to do it using FCM and PHP?
Here is the PHP code to send notification using FCM.
<?php
define('API_ACCESS_KEY', ''); // API access key from Firebase Console
$registrationIds = array(''); //Token IDs of devices
$msg = array
(
'text' => 'Test Text',
'title' => 'Test Title',
);
$fields = array
(
'to' => $registrationIds[0],
'notification' => $msg,
"priority"=> "high"
);
$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, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ));
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
?>

Sending POST REQUEST Firebase Cloud Messaging

EDITED. Solved at the end of the post.
I´m trying to update GCM to FCM and everything works perfect but the connection with the Firebase API.
In GCM I sent had this code from my PHP script:
define( 'API_ACCESS_KEY', 'myapikeynumber');
$registrationIds = array($registrationID);
$msg = array
(
'message' => $message,
'title' => 'You have new notification(s)',
'subtitle' => 'Please click to view your notification(s)',
'user_id' => $user_id,
'user_type' => $user_type,
'notification_id' => $notification_id
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg,
'time_to_live' => 3
);
$headers = array
(
'Authorization: key='.API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/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, false);
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
But now don´t know which fields or values I need send to the https://fcm.googleapis.com/fcm/send.
Any help?
SOLUTION
The problem was here:
$registrationIds = array($registrationID);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg,
'time_to_live' => 3
);
Because $registrationsIds had not JSON structure.
So the code is nearly the same, just change that and the URL of the API to https://fcm.googleapis.com/fcm/send.

Internal Server Error using Google API on PHP

Running this code on Godaddy, Plesk account returns 'Internal Server Error'. Should I make any changes to code or any configuration to run this code?
Code ran perfectly in easyphp local server. Please help. Thank you.
sendPush($to,$title,$message);
function sendPush($to,$title,$message)
{
// API access key from Google API's Console
// replace API
define( 'API_ACCESS_KEY', 'AIzlkplhm-KrtLlR-p-Mg');
$registrationIds = array($to);
$msg = array
(
'message' => $message,
'title' => $title,
'vibrate' => 1,
'sound' => 1
// you can also add images, additionalData
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/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, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
}

Categories