php firebase notification sample [duplicate] - php

This question already has answers here:
How to send Notification to Android from php?
(3 answers)
Closed 4 years ago.
I've implemented a custom web application. an android application is gonna connected to the web application. I know I can use the google firebase to make it but I don't have any sample code.
any suggestion or comment or source code will be appreciated.

Simple PHP FireBase (FCM) script showing how to send an Android push notification. Be sure to replace the SERVER_API_ACCESS_KEY with a proper one from the Google API's Console page. To use the script, just call
http://sample.com/PhpFireBaseNotificationSample.php?id=THE_DEVICE_REGISTRATION_ID
source code :
#API access key from Google API's Console
define( 'API_ACCESS_KEY', 'YOUR-SERVER-API-ACCESS-KEY-GOES-HERE' );
$registrationIds = $_GET['id'];
#prep the bundle
$msg = array
(
'body' => 'Body Of Notification',
'title' => 'Title Of Notification',
'icon' => 'myicon',/*Default Icon*/
'sound' => 'mySound'/*Default sound*/
);
$fields = array
(
'to' => $registrationIds,
'notification' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
#Send Reponse To FireBase Server
$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 Of FireBase Server
echo $result;
GitHub link of php sample code : https://gist.github.com/MohammadaliMirhamed/7384b741a5c979eb13633dc6ea1269ce

Related

Send push notification using php and firebase console account

to all,
ehm i have this php code:
<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'YOUR_FIREBASE_API_ACCESS_KEY' );
$registrationIds = array( $_GET['id'] );
// prep the bundle
$msg = array
(
'body' => $_GET['body'],
'title' => $_GET['title'],
'vibrate' => 1,
'sound' => 1,
);
$fields = array
(
'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;
?>
I've just searched but what i would want is:
1) Which is the YOUR_FIREBASE_API_ACCESS_KEY? In settings of my app in firebase
i see SERVER KEY, PREVIOUS SERVER KEY (translated from italian) and SENDER ID.
2) How can i get registration ids from firebase account app? If i want send push notification to all device that have installed this app how can i DO it?
3) When button clicked:
Connect to firebase account throught API key (Which is?)
Send to all devices that have my app installed
(topic or not?)
Please, help me.
I'm confused.
More than clear of that i can't.
Thanks
Davide

Unable to Send FCM Notification using PHP When app is closed

i am using the following php code to send push notifications using Firebase.
I have received notification on Android app successfully if app is opened, but in case if app is closed, then unable to receive notification. But echo display result success.
Is there any changes required in php code?
Anybody, Have an idea?
Here is my PHP code :-
function send_notification($token,$title,$desc){
$msg = array
(
'body' => $desc,
'title' => $title,
'icon' => 'myicon',/*Default Icon*/
'sound' => 'mySound'/*Default sound*/
);
$fields = array
(
'to' => $token,
'data' => $msg
);
$headers = array
(
'Authorization: key=AIzaSyBP9nF9ntawf_sirR9c2eQ0CQUHTVaBpRs',
'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 Of FireBase Server
$new_result = json_decode($result);
echo($result);
}
FCM Token : dvbsfdkjNH8:APA91bGfvS61gL2XBCq4p0kHOJAjG-ReWpZWO4oWHHflV-baFpSsjm4FjBQCS8dWs_vpbvXJJr7yvOuEmg7rAWNsZfL2r3C0WKrib2dDZSzKZNXaFkHHifYjSvCMntD2bUveovkcidHS
Your php code is fine. This needs to be handled from the app side.
You need to check the FCM intent listner.
An app developer would help you better in this.

Send firebase push notification to multiple users not working -php

I'm trying to send push notification to multiple devices using below PHP script.when i send notification to one device ,it's working.but not working for multiple devices.
protected function pushNotification($reg_id){
define( 'API_ACCESS_KEY', 'AAAAgUh9ZSQ:APA91bEjQ2nO3Po9_-T_kNey4q3Eizcoc3gLsD04Bjh0KYXQu_33O2gpLBvmMV_DtbE3xP2evZ1Dwki6WRqgqu8ZYhOG2CYi-6Wpxq2CLy3SQPb1nD6QkzwNN0249pt-H2mAgB8L-sjm' );
$registrationIds = array($reg_id);
$msg = array
(
'body' => 'Hey! You have a new order in queue',
'title' => 'New Order Receieved',
'icon' => 'myicon',/*Default Icon*/
'sound' => 'mySound'/*Default sound*/
);
$fields = array
(
'to' => $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 );
error_log("Result ".$result) ;
}
The problem lies in your use of to in $fields. to is to be used only when you have an individual String FCM ID. If you want to send notifications to an array of FCM IDs (which is your case), you must use registration_ids in place of to. Please note that you can only pass an array of size lesser than 1000 at a time in this operation.
For more information, https://firebase.google.com/docs/cloud-messaging/http-server-ref .

How to implement web push notifications using FCM in php

I'm working on web push notifications using service worker. But I struck while sending notifications from server using FCM(Firebase Cloud Messaging). Here is my php script and I'm not getting how should I call this in service worker. Please guide me.
define( 'API_ACCESS_KEY', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
$msg = array(
'message' => 'Wakeup Wakeup!!',
'title' => 'Wakeup call !',
);
$fields = array(
'registration_id' => xxxxxxxxxx,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, '//gcm-http.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;
You could use the web-push library for PHP, which, as the name says, makes it easier to use the Web Push protocol via PHP.

Firebase Notification not received on device when sent via cURL/PHP

I have an iOS app which is live on the app store. I am able to send push notifications to iOS devices which have the app installed, but only when I send them from the Firebase console.
When I try to send push notifications via a cURL request, the response from the server indicates that I was successful but the message isn't received on the device. I have tried this with both multicast and single recipient payloads.
I must be missing something more fundamental, but I can't see it.
Here is my PHP code:
<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'AI*****4LPGkx8xtDG2tBl*****7KWJfmp1szcA' );
$registrationIds = array( $_GET['id'] );
// prep the bundle
$msg = array
(
'message' => 'here is a message. message',
'title' => 'This is a title. title'
);
$fields = array
(
'to' => "cUxd-iTVVWo:APA*****kQTuqJ5RREKhhlJjm27NCuVfUn5APg3lBFqh-YWjgx*****iOpAQeLB14CzM2YTwIJo_75jzCmbFLKj0_zpKSHvAEbmJz*****BBezGJIng-N4H-cAD6ahY7mQNYZtEJLAIE",
'data' => $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;
Here is the response I get when running this code:
{"multicast_id":5814921248239922706,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1476193002715692%a4ddee3cf9fd7ecd"}]}
There were two issues:
I needed to include a notification section in the payload instead of data
Somehow the payload wasn't being formatted properly by PHP.
In the end I used the PHP function shell_exec() to do a cURL request over SSH instead. This isn't ideal but it got the job done.
Example code:
shell_exec('curl -X POST --header "Authorization: key=<key here>" --header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"to\":\"'.$to.'\",\"priority\":\"high\",\"notification\":{\"body\": \"'.stripslashes($message).'\"}}"');
From the Firebase documentation you have the choice of using either data or notification in the message payload. But when you use data, you have the responsibility of handling the receipt of the notification yourself. In other words it will not be sent straight away to your app client. You handle it in the didReceiveRemoteNotification: for ios and onMessageReceived() in case of Android.
If however you use notification in the payload, firebase will send the message straight away to your client App.
That is why you will not receive the message(in case you used data) even if your curl request tells you it has succeeded in making the request.
Try to add { priority : high }
$fields = array
(
'to' => "cUxd-iTVVWo:APA*****kQTuqJ5RREKhhlJjm27NCuVfUn5APg3lBFqh-YWjgx*****iOpAQeLB14CzM2YTwIJo_75jzCmbFLKj0_zpKSHvAEbmJz*****BBezGJIng-N4H-cAD6ahY7mQNYZtEJLAIE",
'data' => $msg,
'priority' =>'high'
);
this is what i used :
<?php
//Server keys is now the requirement not the legacy key since its not available
define( 'API_ACCESS_KEY', 'AAAA9....sQ:APA..HKb_Qfz.....q_k4NJonXd...9r-Fof....dJ_ylYLbR....NFz-uQRve.....0GoNISAA......SKx8G_4M...n' );
$messsage = array
(
'body' => 'msg',
'title' => 'title',
'key1' => 'val1'
);
$fields = array
(
'to' => "MdS.....Jg:APA91bEhZ........wkZ7rVmt6UmTc4T189M6ie39COvVmqsIA2DEijt_o9lHUGZbSFez......rPqNo4W_Ru............BEGEo6cuWOKu2vasQju",
'notification' => $message
);
$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;
?>
you can add keys and values like example and handle it in your program

Categories