Firebase invalid registration token - php

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.

Related

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"
}
}

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']);
}

I am sending push notification from web using firebase

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

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