I am using FCM to send push notification to IOS app, IOS dev provide me device token and SERVER_KEY, I search on google and tried different solutions but nothing worked for me , So let me know what is wrong with my code, Getting error {
"multicast_id": 8569689262516537799,
"success": 0,
"failure": 1,
"canonical_ids": 0,
"results": [
{
"error": "InvalidRegistration"
}
]
}
Thanks in advance
$ch = curl_init("https://fcm.googleapis.com/fcm/send");
//The device token.
$token = "c2420d68a0838d8fb6b26ef06278e899de73a149e93c9fe13df11f70f3dd5cc1"; //token here
//Title of the Notification.
$title = "Carbon";
//Body of the Notification.
$body = "Bear island knows no king but the king in the north, whose name is stark.";
//Creating the notification array.
$notification = array('title' => $title, 'text' => $body);
//This array contains, the token and the notification. The 'to' attribute stores the token.
$arrayToSend = array('to' => $token, 'notification' => $notification, 'priority' => 'high');
//Generating JSON encoded string form the above array.
$json = json_encode($arrayToSend);
//print_r($json); die();
//Setup headers:
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: key=AAAAx6P1Nz0:APA91bEqkVCA9YRw9gpUvmF8UOVrYJ5T8672wfS_I7UAT3dA0g1QS7z-Z4fpn8JMiJ5kFRz9ZGc2K64hKZG-4__PAUqm733hqNDuFCDv9'; // key here
//Setup curl, add headers and post parameters.
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
//Send the request
$response = curl_exec($ch);
//Close request
print_r($response);
curl_close($ch);
//return $response;
token number ur sending is most probably wrong. try "/topics/all" this will send notification to all the users registered.
Related
I am receiving a "INVALID_BODY" error with the message "body could not be parsed as JSON" when sending a curl request through php to create a plaid link token.
I have the header and body formatted this way:
$ch=curl_init("https://development.plaid.com/link/token/create");
$username = array(
"client_user_id"=>"cus_L7tpXAO0PXsPsh"
);
$headers = array(
'Content-type: application/json'
);
$data = array(
'client_id'=>'ID',
'secret'=>'SECRET',
'client_name'=>'Plaid App',
'user'=>$username,
'products'=>'auth',
'country_codes'=>'US',
'language'=>'en',
'webhook'=>'https://webhook.sample.com'
);
$hstring = http_build_query($headers);
$string = http_build_query($data);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$token = curl_exec($ch);
echo $token;
$return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
?>
There is probably a very obvious formatting issue but I can't see it as is. Appreciate any suggestions or criticisms.
I should also mention building out the POST in Postman also gives invalid body error.
I was getting the same error, but for a different reason. The problem in your code is how you are sending your country codes and products. They are expected to be arrays of strings despite the documentation seeming to say otherwise. Also, I don't think you're sending the data in JSON either... Try this:
$data =[
"client_id" => $plaidID,
"secret" => $plaidSecret,
"client_name" => $clientName,
"user" => [
"client_user_id" => $userID
],
"products" => ["auth"],
"country_codes"=>["US"],
"language" => "en"
];
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"https://".$apiMode.".plaid.com/link/token/create");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch,CURLOPT_HTTPHEADER,["content-type: application/json"]);
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode((object)$data,JSON_HEX_APOS | JSON_HEX_QUOT ));
$response = curl_exec($ch);
I am using enablex.io to send SMS messages via PHP, I have included my given APP key with my POST request to the API endpoint, however I am receiving an authentication error.
But I have put
$apiKey = urlencode('your app key');
My Code:
<?php
// Account details
$numbers = 'phone number';
$var = '12345';
$sender = urlencode('TXTLCL');
$string = 'Dear user,'. $var.' is the OTP to sign-in to swulj.com - Thanks Team SWULJ ';
$message = rawurlencode($string);
$headers = array(
'Content-Type:application/json',
'Authorization: Basic '. base64_encode("your APP_ID:your APP_KEY")
);
$data =array(
'from'=> 'SWULJP',
'body'=> $string,
'direct'=> false,
'recipient'=> [
array(
'to'=> $numbers,
'body'=> 'This body supercedes with direct: true',
'uuid'=> 'String'
)
],
'type'=> 'sms',
'reference'=> 'XOXO',
'validity'=> '30',
'type_details'=> '',
'data_coding'=> 'plain',
'flash_message'=> false,
'campaign_id'=> '25550516',
'template_id'=> '1215'
);
// Message details
// Send the POST request with cURL
$ch = curl_init('https://api.enablex.io/sms/v1/messages/');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// Process your response here
echo $response;
?>
Response (Error):
{"result":0,"job_id":"617901d728665117a1656964"}
I have an APP_ID and an APP_KEY from enablex.io, I have tried both though neither work.
They say APP_key is the password and APP_ID is the login
The APP ID and APP KEY is should be part of the Authorization header. Please refer to this. So to put this you should doing like this:
$headers = array(
'Content-Type:application/json',
'Authorization: Basic '. base64_encode("APP_ID:APP_KEY")
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
I have built web version in Codeigniter and have been working in mobile version with react Native.
To make new order notification, I am going to use firebase clouding message.
how , where can I get this device token ?
public function sendFCMNotification($registration_ids, $message){
$SERVER_API_KEY ='my server api key';
// payload data, it will vary according to requirement
$data = [
"registration_ids" => $registration_ids,
"data" => $message
];
$dataString = json_encode($data);
$headers = [
'Authorization: key=' . $SERVER_API_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_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
In your React-Native app, you can get the token like this:
messaging().getToken().then(token => { saveTokenToDatabase(token);});
then listen for any token change
messaging().onTokenRefresh(token => {saveTokenToDatabase(token);});
The method saveTokenToDatabase is used to save the token to your backend so that you can access it later to send messages
In android you need to use this class to get device token
public class MyFirebaseMessagingService extends FirebaseMessagingService {
#Override
public void onNewToken(String token) {
super.onNewToken(token);
Log.e("newToken", token);
//Add your token in your sharefpreferences.
SharedPreferenceManager app_sp = new SharedPreferenceManager(this);
app_sp.putString(Constants.FIREBASE_TOKEN, token);
this service request which update device token on to your server
// updatetokenAPI.updateToken(token);
I couldn't fetch user's email address through linkedin v2 api. I added r_emailaddress permission in app settings and also in access token request as well. But it says.
{
"serviceErrorCode": 100,
"message": "Not enough permissions to access: GET-members /clientAwareMemberHandles",
"status": 403
}
My request url is:
https://api.linkedin.com/v2/clientAwareMemberHandles?q=members&projection=(elements*(primary,type,handle~))&oauth2_access_token=".$token
Anyone please help me to solve this.
You can access the linkedin user email address with below EP:
`https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))`
Make sure that you have defined the scope 'r_emailaddress' in your library.
You can use below curl request to fetch the data:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer '. $token,
'X-Restli-Protocol-Version: 2.0.0',
'Accept: application/json',
'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$response = curl_exec($ch);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($response, $headerSize);
$response_body = json_decode($body,true);
$response_body will return the following response:
Array (
[elements] => Array
(
[0] => Array
(
[handle] => urn:li:emailAddress:123456
[handle~] => Array
(
[emailAddress] => your#email.in
)
)
)
)
With this type of response "handle~" value is not easy to get the next step should be :
$email = [];
$response_body = json_decode($res->getBody());
$object = $response_data->elements[0];
foreach ($object as $value){
$email[] = $value->emailAddress;
}
print_r($email[0]);
i am trying yo send a push notification to multiple users, when i do it for 1 user it work perfectly the problem come when i try to send the message to multiple users.
I got the response 200 MissingRegistration (So i supposed the problem is that i dont send the ID on the correct way)
Both ids are correct because if i send the message to both individually it works
This is my code
$gcmcodes=array("one user key","the other user key");
if(isset($_GET['mensaje'])) $message = $_GET['mensaje'];
$message2 = new gcm();
$message2->sendMessageToPhone(2, $message,$gcmcodes);
function sendMessageToPhone($collapseKey, $messageText, $gcmcodes)
{
$apiKey = 'My apikey';
$headers = array('Authorization:key=' . $apiKey);
$data = array(
'registration_ids' => $gcmcodes,
'collapse_key' => $collapseKey,
'data.message' => $messageText);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send");
if ($headers)
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo"<pre>";
var_dump($gcmcode,$response);
echo"</pre>";
if (curl_errno($ch)) {
return 'fail';
}
if ($httpCode != 200) {
return 'status code 200';
}
curl_close($ch);
return "mensaje mandado con exito";
}
The format you are using can only be used for sending a single notification at a time.
In order to send the notification to multiple registration IDs, you have to send a JSON request of the form :
{ "collapse_key": "something",
"time_to_live": 108,
"delay_while_idle": true,
"data": {
"message": "some message"
},
"registration_ids":["4", "8", "15", "16", "23", "42"]
}
Oh, and the reason you got MissingRegistration error is that when you use the plain text format, GCM expects a single Registration ID to be the value of the registration_id key (not registration_ids as you tried).