I am currently trying to make use of cloud messaging for my Android app. The server is sending messages via PHP using php-curl.
The problem is that the server's response is always:
401: Unauthorized
I use the WebAPI Access key from the Firebase Console for my App, so this is definitively the right one. Below is the code I use to send the data:
<?php
$fields = array(
'to' => "<MY-RECIPIENT-TOKEN>",
'notification' => array(
'body' => 'Test message :)',
'title' => 'Test',
'icon' => 'myicon',
'sound' => 'mySound'
)
);
$headers = array(
'Authorization:key=<MY AUTH KEY IS HERE>',
'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;
The MY-RECIPIENT-TOKEN is received from the App and the MY AUTH KEY IS HERE is the key from the Firebase Console.
What am I missing here?
Nevermind. The Problem is not in the code, the code is fine. If someone stumbles upon this: There are 2 keys in your FCM Console. When you go to your Project Settings you will find the Web-API-Key. This is not the key you are looking for! You need to go to Settings -> Cloud Messaging. There you will see a much longer Server key. THIS is the key you want to use!
Related
I am playing around with the TRON api and trying to send transactions using the PHP curl method. I can successfully send the transaction in the TRON Developer sandbox but get and error when executing it on my server.
Error im receiving -
status_code: 200
{"result": {"code": "CONTRACT_VALIDATE_ERROR","message": "313a39343a20494e56414c49442068657820537472696e67"}}
My php Curl request -
$ch = curl_init();
$headers = [
'Content-Type: application/json'
];
$postData = [
'privateKey' => 'MY Private Key',
'toAddress' => 'To Address',
'amount' => '100000'
];
curl_setopt($ch, CURLOPT_URL,"https://api.trongrid.io/wallet/easytransferbyprivate");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec ($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo $statusCode;
print_r($result);
Here is the link to the TRON sandbox im using -
https://developers.tron.network/reference#east-transfer-by-private-key
The success code in the sandbox is 200 so not sure why im receiving that and I am not sure how to lookup the returned error message, any idea what im doing wrong?
This is the sample post request given from TRON -
curl -X POST https://api.trongrid.io/wallet/easytransferbyprivate -d '{"privateKey": "D95611A9AF2A2A45359106222ED1AFED48853D9A44DEFF8DC7913F5CBA727366", "toAddress":"4112E621D5577311998708F4D7B9F71F86DAE138B5","amount":10000}'
I figured it out, I was preparing the data wrong..
Instead creating an array -
$postData = [
'privateKey' => 'MY Private Key',
'toAddress' => 'To Address',
'amount' => '100000'
];
I needed to do the literal string and not encode it.
$postData = '{"privateKey": "xxx", "toAddress..etc
Just like in the example given in the sandbox.
I have written an API in PHP which will receive Firebase token and will send push notification to Android device.
I have tested Android side code and it receiving sample notification from Firebase console using token. But when I am trying to send from PHP api then it's not sending notification.
Here is my php API.
// send firebase push notifcaiton
$url = 'https://fcm.googleapis.com/fcm/send';
$api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxx';
$fields = array (
'registration_ids' => array (
$pnsToken
),
'data' => array (
"message" => $message,
"guestId" => $guestId,
"guestEntryId" => $guestEntryId,
"guestName" => $guestName,
"guestAddress" => $guestAddress,
"reason" => $reason
)
);
//header includes Content type and api key
$headers = array(
'Content-Type:application/json',
'Authorization:key='.$api_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);
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('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
I don't know why it's not sending push notification from PHP api.
Edit
Here is fcm error which I am receiving :
"fcmResult": "{\"multicast_id\":7084003691931660410,\"success\":0,\"failure\":1,\"canonical_ids\":0,\"results\":[{\"error\":\"NotRegistered\"}]}",
created a project on one signal.
I can send to php notification on my phone.
The problem comes when it arrives more than one notification. The new notification replaces the previous notification (not yet read ).
How do you get instead say that Android has 2 unread notifications?
I have try write the same android_group but the notification never stacked and the newest continue replace the previus.
This is my code:
<?php
function sendMessage(){
$content = array(
"en" => 'text message test'
);
$fields = array(
'app_id' => "XXxxxxXX-xxxxXX-XXxxX-Xxxx-XxxxXXx",
'included_segments' => array('All'),
'data' => array("foo" => "bar"),
'headings' => array("en" => "Test message!!"),
'android_group' => 'TESTGROUP',
'android_group_message' => array("en" => "message"),
'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',
'Authorization: Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'));
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();
?>
How can i solve this problem?
android_group used to enable notification stacking only works for Android apps.
If you are you're web push with Chrome for Android the replacement behavior your seeing is expected and isn't configurable. Same behavior on Chrome for Desktop and Firefox.
Also note android_group_message should contain $[notif_count] so the number of unread messages is seen. Example
array("en" => "You have $[notif_count] new messages")
I created a little app which uses Pushbullet's API.
It is writtent in PhP and uses cURL to proceed the different requests.
At first, I use oauth2 to recieve an access token.
Then it is /v2/pushes/ to send a push to the registred account.
The message is recieved on the Pusbullet account but nothing shows up on the smartphone. There is no notification.
I hope someone can help me.
Thanks.
My function which sends the push:
function send_push($token){
$data = array(
'type' => 'note',
'title' => 'Alert',
'body' => 'My body :)!'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.pushbullet.com/v2/pushes');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer '.$token
));
$data = curl_exec($ch);
curl_close($ch);
}
I want to push messages from my cloud server to Android emulator or Android device with phone number.
Is it possible? Do I need to use any gateway?
Check out this:
https://developer.android.com/google/gcm/index.html
https://developer.android.com/google/gcm/http.html
Basically you would have to make CURL call to
https://android.googleapis.com/gcm/send
Something like this:
$data = array(
'collapse_key' => $msgType,
'data' => array('message' => $news_text,
'title' => $news_title,
'url' => $messageUrl,
'message_id' => $news_id,
'notification_type' => $channel_name),
'registration_ids' => $devices
);
$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, json_encode($data));
$response_json = curl_exec($ch);
$response = json_decode($response_json);
print_r($response_json);
GCM / FCM is the goto solution. If every app which uses push notifications would implement some sort of listening or polling service the battery would drain way to fast. Thats why google and also Apple provide 1 single service for the whole Androidsystem and its apps to use.
This means that it isnt impossible todo, i think there are some third party push notifications-services.
I just dont think its a clever thing todo. Chances are that google build a reliable, fast and efficient service.