I am sending push notification from web using firebase - php

I am sending push notification from web using firebase, but I am not able to send push notification, while I am getting only name. How can I send push notification from web using firebase help me.
This is my php code.
<?php
$DEFAULT_URL = 'https://pushnotificatioexample.firebaseio.com/.json';
$mytoken = $_POST['token'];
$mymessage = $_POST['message'];
echo $mytoken;
echo '<br>'.'</br>';
echo $mymessage;
$registrationIds = array($mytoken);
$message = array
(
'message' => 'My awesome message',
'title' => 'My awesome title',
'subtitle' => 'My awesome subtitle',
'tickerText' => 'My awesome Ticker text',
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
);
$fields = array(
'registration_ids' => $registrationIds,
'data' => $message
);
$headers = array(
'Authorization:key = AIzaSyC9M4KFwqdy3vjcIBx9TNO8M7IysADbY_s',
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $DEFAULT_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) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
echo $result;
?>

Its look like you are sending a cloud message from your service side script. According to Firebase (FCM) official documentation, you don't have to use the url from Realtime database and all. So you have to change your url
From
$DEFAULT_URL = 'https://pushnotificatioexample.firebaseio.com/.json';
TO
$DEFAULT_URL = 'https://fcm.googleapis.com/fcm/send';
See the Firebase Cloud Messaging HTTP Protocol and here for more details

Related

FCM Push for iOS error InvalidRegistration

I am trying to send push notifications through PHP script via Firebase Cloud Messaging to iOS App.
Currently I'm using following test PHP script.
$url = "https://fcm.googleapis.com/fcm/send";
$token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$serverKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$title = "Title";
$body = "Body of the message";
$notification = array('title' =>$title , 'text' => $body, 'sound' => 'default', 'badge' => '1');
$arrayToSend = array('to' => $token, 'notification' => $notification,'priority'=>'high');
$json = json_encode($arrayToSend);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: key='. $serverKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
//Send the request
$response = curl_exec($ch);
//Close request
if ($response === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
However, I am receiving following error message when I try to run script via browser.
{"multicast_id":5417898622260949101,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
But when I use the same device token from FCM console, it is working without any issues.
I have tried many PHP scripts on the internet, but end up with the same results, what would be the issue here ?
Any help would be highly appreciate.
The token you are using does not seem to be a valid registration token for FCM, hence the InvalidRegistration error.
$apiKey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$title = 'Title';
$message = 'hello message';
$notification =[
"text" => "Test",
'badge' => "1",
'sound' => 'shutter.mp3',
"android_channel_id"=>"test01",
'body'=>$message,
'icon'=>'notif_icn',
'title'=>$title,
'priority'=>'high',
'vibrate'=> 1,
'alert'=> $message
];
$msg = [
'message'=> $message,
'title'=> $title
];
$fields =[
"content_available" => true,
"priority" => "high",
'registration_ids'=> $deviceToken,
'data'=> $msg
];
$headers = array('Authorization: key=' . $apiKey,'Content-Type: application/json');
if ($headers){
$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_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$response = curl_exec($ch);
if ($response === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
}

Firebase notification gives me "Success" but I don't get the notification on IOS

I get notifications on Android but when I send the notification to an IOS device, it doesn't get received, even though it says success.
I added badges, sound, vibration, priority.. etc but none of this solved the problem.
function sendNotification($title, $body, $aid,$destination)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$message = array(
'title' => $title,
'body' => strip_tags($body),
'vibrate' => 1,
'sound' => 1,
"action" => "url",
'sound' => 'default',
'badge' => '1',
'priority'=>'high',
"action_destination" => $destination,
);
$fields = array(
'registration_ids' => $aid,
'data' => $message
);
$headers = array(
'Authorization: key=KEYHERE',
'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_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
curl_close($ch);
}
The mobile developer says that theres a new firebase update and I frankly think hes bsing... Can someone explain to me whats going on? I think my part here is done, but you guys might see something wrong with my code...

How to add image to android push notification from firebase. developed using ionic 3

I want to add an image to push notification. am using firebase and app is developed using ionic 3 frameworks. I have a web app to push the notification. this is the PHP code. Please help me to add an image to push notification.
public function pushNotification($title, $body, $image) {
//FCM API end-point
$url = 'https://fcm.googleapis.com/fcm/send';
//api_key available in Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key
$server_key = 'xxxx';
$target = [];
$targets = $this->db->get('device_ids')->result();
foreach($targets as $val) {
array_push($target, $val->device_id);
}
$fields = array();
$fields['notification'] = array(
'title' => $title,
'body' => $body
);
if(is_array($target)){
$fields['registration_ids'] = $target;
}else{
$fields['to'] = 'news';
}
//header with content_type api key
$headers = array(
'Content-Type:application/json',
'Authorization:key='.$server_key
);
//CURL request to route notification to FCM connection server (provided by Google)
$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);
if ($result === FALSE) {
die('Oops! FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
print_r(json_encode($fields));
}
$tokenListAndroid = "YOUR_TOKEN";
define( 'API_ACCESS_KEY', 'YOUR_KEY');
$Message='YOUR_MESSAGE';
$offerId=$_POST['offerId'];
$iOSids = explode(",",$tokenListiOS);
$androidIds = explode(",",$tokenListAndroid);
$msg = array
(
'title' => 'TEXT',
'message' => 'TEXT',
'summaryText' => 'TEXT',
'orderId' => 'TEXT',
'tickerText' => 'Ticker text here...Ticker text here...Ticker text here',
'image' => 'YOUR_ICON_PATH ex. https://carboncostume.com/wordpress/wp-content/uploads/2016/08/blackpanther.jpg',
'style' => 'picture',
'picture' => 'YOUR_IMAGE_PATH ex. https://carboncostume.com/wordpress/wp-content/uploads/2016/08/blackpanther.jpg',
//'ongoing' => true,
'vibrate' => 1,
'sound' => 1
);
$fields = array
(
'registration_ids' => $androidIds,
'data' => $msg
);
//echo json_encode( $fields ); die;
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
Thats all XD
This is for IONIC 3
notification with Image and Text from firebase
Implement Phonegap Push Plugin and create your firebase project
ionic cordova plugin add phonegap-plugin-push
//Now we send the Notification
Post service
URL=https://fcm.googleapis.com/fcm/send
Header: Authorization: key={Your Firebase Key}
Content-Type: application/json
then send Body
Body:
***
{
"to": "/topics/college",
"data": {
"title": "Notification With Image",
"message": "This is my Notification with Image",
"style": "picture",
"picture": https://appsolzone.com/wp-content/uploads/2018/07/Scholar-Wings-2-480x336.jpg",
"summaryText": "This is image notification with Firebase in IONIC"
}
}

Firebase invalid registration token

I am a web developer and hired ios developer to create native app for my client. The client wants to have push notifications. We are using firebase but the problem is that i can't send push notifications from my php script, because of the InvalidRegistration error. I've read all over stackoverflow about this error and still dont have a clue how to fix it.
I am saving the device_token in a mysql table CHAR(64).
This is my script:
if (isset($_POST['submit'])) {
// API access key from Google API's Console
define('API_ACCESS_KEY', 'API');
$users = $db->getResults("device_id", "users", "device_id != '' AND travel_stay = '" . $_POST['group'] . "'");
foreach ($users as $user) {
$registrationIds = (string)$user['device_id'];
// prep the bundle
$msg = array(
'message' => $_POST['description'],
'title' => $_POST['title'],
'subtitle' => 'This is a subtitle. subtitle',
'tickerText' => 'Ticker text here...Ticker text here...Ticker text here',
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon');
$fields = array('to' => $registrationIds, '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;
}
}
?>
I have no idea what is wrong with it, is the device_token sent wrong? How can i fix it.

How to Send push notifications using One Signal + PHP + Server API?

I am using one signal to send push notification for android app. My question is
How Can I setup send push notifications using server rest api?
<?PHP
function sendMessage(){
$content = array(
"en" => 'Testing Message'
);
$fields = array(
'app_id' => "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx",
'included_segments' => array('All'),
'data' => array("foo" => "bar"),
'large_icon' =>"ic_launcher_round.png",
'contents' => $content
);
$fields = json_encode($fields);
print("\nJSON sent:\n");
print($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
'Authorization: Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$response = sendMessage();
$return["allresponses"] = $response;
$return = json_encode( $return);
print("\n\nJSON received:\n");
print($return);
print("\n");
?>
You can always refer to the official docs:
https://documentation.onesignal.com/reference#section-example-code-create-notification
'app_id' is currently known as (OneSignal App ID) in the OneSignal
Settings->Keys and IDs
in 'Authorization: Basic xxx...' past the "REST API Key" just below the App ID
I see you have set isAndroid=true, but OneSignal is returning an error that shows that the application with ID eec33e8e-5774-4b74-9aae-37370778c4b2 does not have Android notifications enabled.
Make sure your app ID is correct, and if it is, that Android notifications are enabled in your OneSignal settings.
$to - Device ID
$title - Notification Title
$message - Notification Message
$img - Full image link
Usage:
sendnotification($to, $title, $message, $img);
With Demo Values :
sendnotification("Device_ID","Test Notification","Test Message","https://www.google.co.in/images/branding/googleg/1x/googleg_standard_color_128dp.png");
function sendnotification($to, $title, $message, $img)
{
$msg = $message;
$content = array(
"en" => $msg
);
$headings = array(
"en" => $title
);
if ($img == '') {
$fields = array(
'app_id' => 'YOUR_APP_ID',
"headings" => $headings,
'include_player_ids' => array($to),
'large_icon' => "https://www.google.co.in/images/branding/googleg/1x/googleg_standard_color_128dp.png",
'content_available' => true,
'contents' => $content
);
} else {
$ios_img = array(
"id1" => $img
);
$fields = array(
'app_id' => 'YOUR_APP_ID',
"headings" => $headings,
'include_player_ids' => array($to),
'contents' => $content,
"big_picture" => $img,
'large_icon' => "https://www.google.co.in/images/branding/googleg/1x/googleg_standard_color_128dp.png",
'content_available' => true,
"ios_attachments" => $ios_img
);
}
$headers = array(
'Authorization: key=**APP_KEY**',
'Content-Type: application/json; charset=utf-8'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://onesignal.com/api/v1/notifications');
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 $result;
}

Categories