PHP Firebase Clood Messaging (FCM) error missing registration - php

I am building a push notification from my server to FCM server. I getting following errors: Error=MissingRegistration
My PHP code is given below.
function send($id,$title,$text){
$msg = [
'title' => $title,
'body' => $text,
'icon' => 'myicon',
'sound' => 'mySound'
];
$fields = [
'to' => $id,
'notification' => $msg
];
$headers = [
'Authorization: key=' . $api_key,
'Content-Type: application/json'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );
$result = curl_exec ( $ch );
curl_close ( $ch );
echo $result;
}
I am calling function like this:
send($id,$title,$text);

For sending data to FCM you need to create json data. Please add this line after fields
$fields = json_encode($fields);

Related

FCM message not delivered to device when sent from php

I am trying to implement FCM in my app. Right now, I am able to receive messages when i send them from firebase app console. But when i try to send messages from my php code on the server, the message does not get delivered to the phone. However, I get the message_id with numbers whenever the call to FCM is completed and no indication of any errors. Any help would be appreciated.
PHP code:
public function send_fcm() {
$API_ACCESS_KEY = '*****************************';
$msg = array ('message' => 'here is a message. message',
'title' => 'This is a title. title',
'subtitle' => 'This is a subtitle. subtitle',
'tickerText' => 'Ticker text here...',
'vibrate' => 1,
'sound' => 1
);
$fields = array('to' => '/topics/mytopic',
'priority' => 'high',
'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));
$pushResult = curl_exec($ch);
curl_close($ch);
}
you should provide notification with fields array , I use this method to send notification using PHP
public static function toDevice($token , $message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
'to' => $token,
'notification' => [
"body" => $message ,
...
] ,
"data" => [
...
]
);
$fields = json_encode ( $fields );
$headers = array (
'Authorization: key=' . "XXXXXXXXXXXXXXXXXXX",
'Content-Type: application/json'
);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );
$result = curl_exec ( $ch );
curl_close ( $ch );
return $result;
}
Your correction code this..it is tested code.
or follow this link : https://inducesmile.com/android/android-firebase-cloud-messaging-push-notification-with-server-admin-in-php-and-mysql-database/
public function send_fcm($token) {
$url = "https://fcm.googleapis.com/fcm/send";
$msg = array('message' => 'here is a message. message',
'title' => 'This is a title. title',
'subtitle' => 'This is a subtitle. subtitle',
'tickerText' => 'Ticker text here...',
'vibrate' => 1,
'sound' => 1
);
$fields = array('to' => $token,
'priority' => 'high',
'data' => array('message' => $msg)
);
$headers = array(
'Authorization:key=*****************************',
'Content-Type:application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $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));
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$result = curl_exec($ch);
if ($result === FALSE) {
die('CURL FAILED ' . curl_error($ch));
}
$info = curl_getinfo($ch);
curl_close($ch);
return array('result' => $result, 'status' => $info['http_code']);
}

firebase messaging api php subscribe to topic 411 error

I am trying to implement notifications into my web app. I have this php file in which I send notifications:
<?php
function sendGCM($title,$message, $id) {
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
'registration_ids' => array (
$id
),
"notification" => array(
"title" => $title,
"body" => $message,
"click_action" => "https://google.com"
)
);
$fields = json_encode ( $fields );
$headers = array (
'Authorization: key=' . $MY_KEY,
'Content-Type: application/json'
);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );
$result = curl_exec ( $ch );
echo $result;
curl_close ( $ch );
}
?>
Now what I want to do is to send a notification to a topic instead of individual ids. On this page it shows how you can subscribe an id to a topic. Here is the function I created to do that:
function createTopic($topic,$id) {
$url = 'https://iid.googleapis.com/iid/v1/' . $id . '/rel/topics/' . $topic;
$headers = array (
'Authorization: key=MY_KEY'
);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
$result = curl_exec ( $ch );
echo $result;
curl_close ( $ch );
}
I am getting this error from Google and I can't figure out what is going wrong:
That’s an error.
POST requests require a Content-length header. That’s all we know.
Any help?
The example in the documentation shows use of a Content-Length header:
https://iid.googleapis.com/iid/v1/nKctODamlM4:CKrh_PC8kIb7O...clJONHoA/rel/topics/movies
Content-Type:application/json
Content-Length: 0
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
Add that to your request:
$headers = array (
'Authorization: key=' . $MY_KEY,
'Content-Length: 0'
);

Firebase PHP don't work for IOS

I have a small problem
I use FireBase for push notification on iOS and Android.
On Android it work, on iOS it work only when i send message by the console when i send message with PHP it's not good
My php code
function sendGCM($message, $id) {
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
'registration_ids' => array (
$id
),
'data' => array (
"message" => $message
)
);
$fields = json_encode ( $fields );
$headers = array (
'Authorization: key=' . "*****",
'Content-Type: application/json'
);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );
$result = curl_exec ( $ch );
curl_close ( $ch );
}
Do you know why ?
Thanks :)

Firebase how to send Topic Notification

I am using below script to send notification to particular users:
<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'My_API_KEY' );
$registrationIds = array( TOKENS );
// prep the bundle
$msg = array
(
'body' => "abc",
'title' => "Hello from Api",
'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;
?>
Script is working fine but how can i send notification to all user who installed my app. I created a topic in my app (alerts), and i can send notification to all users via firebase console. Can anyone guide me to update above script for topic.
I fixed by replacing
$fields = array
(
'registration_ids' => $registrationIds,
'notification' => $msg
);
To
$fields = array
(
'to' => '/topics/alerts',
'notification' => $msg
);
You can send the notifications without curl (which was not available on my server).
I prepared a function which can send a notification to a specified topic:
sendNotification("New post!", "How to send a simple FCM notification in php", ["new_post_id" => "605"], "new_post", "YOUR_SERVER_KEY");
function sendNotification($title = "", $body = "", $customData = [], $topic = "", $serverKey = ""){
if($serverKey != ""){
ini_set("allow_url_fopen", "On");
$data =
[
"to" => '/topics/'.$topic,
"notification" => [
"body" => $body,
"title" => $title,
],
"data" => $customData
];
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode( $data ),
'header'=> "Content-Type: application/json\r\n" .
"Accept: application/json\r\n" .
"Authorization:key=".$serverKey
)
);
$context = stream_context_create( $options );
$result = file_get_contents( "https://fcm.googleapis.com/fcm/send", false, $context );
return json_decode( $result );
}
return false;
}
i am work with google firebase many time and i suggest my simple code for send notification on topic.
public function test()
{
// Method - 1
// $fcmUrl = 'https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1';
// $notification = [
// "message" => [
// "topic" => "foo-bar",
// "notification" => [
// "body" : "This is a Firebase Cloud Messaging Topic Message!",
// "title" : "FCM Message",
// ]
// ]
// ];
// Method - 2
$fcmUrl = 'https://fcm.googleapis.com/fcm/send';
$notification = [
"to" => '/topics/cbtf',
"data" => [
"message" : "Messaging Topic Message!",
]
]
];
$headers = [
'Authorization: key=AIza...............klQ5SSgJc',
'Content-Type: application/json'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$fcmUrl);
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($notification));
$result = curl_exec($ch);
curl_close($ch);
return true;
}
You can send notification to any of a topic in firebase. And you can do it from any language it's just a http request but Always you have to maintain the JSON format so that you can catch notification from your android part from
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Timber.d("Data Payload: " + remoteMessage.getData());
}
So You need to send below JSON format
{
"to": "\/topics\/general",
"data": {
"data": {
"title": "Database Notification",
"message": "New Data Added"
}
}
}
So that you can get this data set from remoteMessage.getData()
With particular Topic
<?php
print "testing";
function sendPushnotification($data = array()) {
$apiKey = '';
$fields = array('to' => '/topics/EWAP' , 'notification' => $data);
$headers = array('Authorization: key=' .$apiKey, 'Content-Type: application/json', 'priority' => 10);
$url = 'https://fcm.googleapis.com/fcm/send';
// var_dump($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
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);
return json_encode($result, true);
}
$data = array(
'title' => 'Today topic',
'body' => 'done buddy'
);
var_dump(sendPushnotification($data));
?>
With device token.
<?php
print "testing";
function sendPushnotification($to = '',$data = array()) {
$apiKey = '';
$fields = array('to' => $to , 'notification' => $data);
$headers = array('Authorization: key=' .$apiKey, 'Content-Type: application/json');
$url = 'https://fcm.googleapis.com/fcm/send';
// var_dump($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
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);
return json_encode($result, true);
}
$to = "add device here token ";
$data = array(
'title' => 'Today topic',
'body' => 'done buddy'
);
var_dump(sendPushnotification($to, $data));
?>

Google Firebase iOS push works in console but not in API

I'm using https://github.com/arnesson/cordova-plugin-firebase/ to receive Google Firebase messages on a ionic based app.
After set certificates, install plugin and setup Firebase account I was able to receive notifications (on both android and ios devices) sended through the Firebase Console.
But when I send through the Firebase API (https://firebase.google.com/docs/cloud-messaging/http-server-ref) only android devices receive the notification. I'm using the following code:
$data = Array
(
[to] => <token>
[notification] => Array
(
[title] => My Title
[text] => Notification test
[sound] => default
[vibrate] => 1
[badge] => 0
)
)
$jsonData = json_encode($data);
$ch = curl_init("https://fcm.googleapis.com/fcm/send");
$header = array(
'Content-Type: application/json',
"Authorization: key=".$gcmApiKey
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, true );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
No errors are returned:
{"multicast_id":904572753471539870406,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1477063422322568734%3d5c78243d5c7824"}]}
What can be wrong?
For iOS, try adding in parameter priority set to high and content_available set to true in your payload.
See the parameter details here.
try this code
function sendGCM($message, $id) {
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
'registration_ids' => array (
$id
),
'data' => array (
"message" => $message
)
);
$fields = json_encode ( $fields );
$headers = array (
'Authorization: key=' . "YOUR_KEY_HERE",
'Content-Type: application/json'
);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );
$result = curl_exec ( $ch );
echo $result;
curl_close ( $ch );
}
?>
also try it in curl terminal
curl -X POST --header "Authorization: key=<API_ACCESS_KEY>" --Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"to\":\"<YOUR_DEVICE_ID_TOKEN>\",\"notification\":{\"body\":\"Yellow\"},\"priority":10}"
a little late but with working example,
I'm using the code below for ios and android push, you are missing the priority and content_available fields
example :
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'to' => $token,
'notification' => array('body' => $message , "sound" => "default"),
'data' => $message,
"sound"=> "default",
'priority' => "high" ,
'content_available' => false
);
$headers = array(
'Authorization:key = your-key',
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $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);

Categories