There is a php code to prepare a bundle for sending notification to firebae :
$registrationIds = array( $_GET['id'] );
// prep the bundle
$msg = array
(
'body' => $_GET['body'],
'title' => $_GET['title'],
'vibrate' => 1,
'sound' => 1,
);
$fields = array
(
'registration_ids' => $registrationIds,
'notification' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
I want same in java, i did all well and getting response 200 from firebase.
but i dont know how to get registration-id . As done in php like :
$registrationIds = array( $_GET['id'] );
Please help me to find out the registration-id.
To get the registrationId of a device(iOS or Android) the device needs to send/store it in a DB some where. I recommend using the realtime database provided by firebase.
First to get registrationId from the iOS App. Use let token = FIRInstanceID.instanceID().token(). This Token is known as the registrationId
Take this token and store it in your DB.
Server side you need to get that token out of your DB and place it as the value for the registration_ids or as the value for the "to" key. That should be it.
What about the "to"
So it's well laid out on how to use "to" in this StackoverflowPost
You don't get the "to" parameter from the client.
Related
I need to send string data to an external url which is an API for clients, but I always get the server sending back an error, and then I checked the log in the server which is written in JAVA, it throws an exception
"java.lang.NumberFormatException: For input string: "{"success":false,"errorCode":"500","message":"Unknown Error!","value":null,"totalPageCount":0,"footerValue":null}"
and here's my piece of php code:
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'content' => $params
),
));
$received = file_get_contents(URL, false, $context);
echo $received.'<br>';
I'm pretty sure the data I send to the server is in correct format as I have the access to the log, and I know that all response parameters from the server are in JSON, UTF-8 and post method, is there something I missed?
If more information needed please let me know, thanks.
You need to parse the string. You are getting NumberFormatException and this is because your server is receiving data which is not a numeric.
Integer.parseInt() // in java
You can parse the content in server side this way or send numeric data.
Another method:
Integer.valueOf() // in java
I want to send a background notification from fcm to all my users. This is my code, the only problem I've faced is that I have to put a token id. I need to send notification to all of my users without defining the token values
This is my code :
<?php
define('API_ACCESS_KEY','Api');
$fcmUrl = 'https://fcm.googleapis.com/fcm/send';
$token='token';
$notification = [
'title' =>'title',
'body' => 'body of message.',
'icon' =>'myIcon',
'sound' => 'mySound'
];
$extraNotificationData = ["message" => $notification,"moredata" =>'dd'];
$fcmNotification = [
//'registration_ids' => $tokenList, //multple token array
'to' => $token, //single token
'notification' => $notification,
'data' => $extraNotificationData
];
$headers = [
'Authorization: key=' . API_ACCESS_KEY,
'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($fcmNotification));
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
It works fine and send notifications successfully, but how can I send notification to everyone?
It looks like there is no such feature to send notifications to all devices.
However, there is a workaround for your problem using topics. For this you will need to subscribe all users to a specific topic during the app startup.
FirebaseMessaging.getInstance().subscribeToTopic("your_topic");
Then you can send the notification to that topic so that the notification will be sent to all users.
'to' => '/topics/your_topic', // using topic instead of token
If i get your question.
You need to subscribe a Topic and then you just need to use that Topic.
The users who subscribed to it, will get the notification.
Docs:-
Based on the publish/subscribe model, FCM topic messaging allows you to send a message to multiple devices that have opted in to a particular topic. You compose topic messages as needed, and FCM handles routing and delivering the message reliably to the right devices.
https://firebase.google.com/docs/cloud-messaging/android/topic-messaging
There are two ways to send notification to multiple users.
Create a topic with all the users in it and send the push to that topic.
Example: https://firebase.google.com/docs/cloud-messaging/android/send-multiple
Send notification to application the package id. But this will send notification even to the non authenticated user.
I'm migrating a project from PHP to Java.
I had no problem doing this, but the project contains a HTTP request which I couldn't set up yet. It has to grab a JSON from an API (Authorisation required) and then save it as a variable (which gets passed on to my parser class).
This is the code I've used in php:
$result = file_get_contents($url, null, stream_context_create(array(
'http' => array(
'method' => 'GET',
'header' => 'Accept: application/json' . "\r\n"
. 'Authorization: Bearer ' . $token . "\r\n",
),
)));
$url is the request url and $token the authorisation for the API.
I've already tried a couple of solutions and libraries from StackOverflow, but couldn't get any of these to work in the Java Version. Also I haven't found code which includes the response code, which would actually allow the program to notify the user what went wrong.
Update: I've tried to use the libery linked in the comments, but encoding the url just doesn't work, I get a java.io.UnsupportedEncodingException:
String url = URLEncoder.encode(requestURL,"UTF-8");
I also tried:
Url url = URLEncoder.encode(requestURL,"UTF-8");
which resulted in the same error.
Documentation doesn't help to fix this. Can someone explain what I'm doing wrong or give me some example code?
I sent this request in my Java application: https://www.facebook.com/dialog/oauth?client_id=CLIENT_ID&redirect_uri=https://www.facebook.com/connect/login_success.html&scope=email.
Next I showed the authorization page which was received as response. After submitting login info nothing happens
In redirect_uri you should set link on your site(for example: http://localhost/facebook-auth).
So you will get request like this one:
http://localhost/facebook-auth/?code=AQD5q80zafjvVZoZex87ROxkCvWT9rZhjwZtkBLajYwH20KztPOI0jpb5lHZisPd3mA49Wu_onAeEioU5K6KVuoCliznf61B5bDfZSLFaIn6E7E49zqs4fO6NjTYyxN43LBttCvsSlirJOAtbOpB3oyMrl3bbjlPhGHsCyJzA-DypEIZ1c_36WAEBPmfSq3TroekvTLme3jIzZk0C-93cu8z#_=_
This code param you need to get the token.
Your next query will be like:
$params = array(
'client_id' => $client_id,
'redirect_uri' => $redirect_uri,
'client_secret' => $client_secret,
'code' => $_GET['code']
);
$url = 'https://graph.facebook.com/oauth/access_token';
And in response you will get the token
I've been using CURL and php to send the following:
$ch = curl_init($url);
curl_setopt_array($ch, array(
CURLOPT_POST => count($postfields),
CURLOPT_POSTFIELDS => $postfields,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_VERBOSE => true,
CURLOPT_SSLCERT => 'C:\mycert.pem',
CURLOPT_SSLCERTPASSWD => 'mypass',
CURLOPT_SSLCERTTYPE => 'PEM'
));
I've not included the postfields to keep it brief.
This works very well. However after much googling I can't find a simple way to do this using HTTPClient. I just want to include a certificate with the POST.
TIA
You need to create a trust store containing your certificate.
My blog post at http://blog.chariotsolutions.com/2013/01/https-with-client-certificates-on.html contains a little more than you need (it includes supporting self-signed client and server certs), but should give you everything you need.