我想在不使用Firebase控制台的情况下发送通知(我想在服务器端制作)。为此,我正在尝试将设备令牌ID发送到Web服务。我在这个Android项目中使用Firebase Cloud Messaging。我怎么能这样做?
public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService{
private static final String TAG = "MyFirebaseIIDService";
@Override
public void onTokenRefresh() {
String token = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Token: " + token);
sendRegistrationToServer(token);
}
private void sendRegistrationToServer(String token) {
// send token to web service ??
}
谢谢!
答案 0 :(得分:3)
首先,您应该运行一些服务器来接收令牌和存储。然后,您可以从应用程序进行API调用,以将FCM令牌发布到您的服务器。
答案 1 :(得分:3)
1)创建firebase ref像这样
private void sendRegistrationToServer(String token) {
// send token to web service ??
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("server/saving-data/IDs");
// then store your token ID
ref.push().setvalue(token)
}
更多阅读this post
答案 2 :(得分:0)
将以下给出的代码粘贴到您的类中,此方法将自动从“onTokenRefresh()”方法执行。只需确保使用您自己的服务器的Web服务URL替换Web服务URL。
private void sendRegistrationToServer(String deviceToken) {
class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String deviceToken = params[0];
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(
"http://lib-dm.process9.com/libertydm/ValidateUserHandler.ashx");// replace with your url
httpPost.addHeader("Content-type",
"application/x-www-form-urlencoded");
BasicNameValuePair usernameBasicNameValuePair = new BasicNameValuePair(
"UserId", paramUsername); // Make your own key value pair
BasicNameValuePair passwordBasicNameValuePAir = new BasicNameValuePair(
"Password", paramPassword);// make your own key value pair
// You can add more parameters like above
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
nameValuePairList.add(usernameBasicNameValuePair);
nameValuePairList.add(passwordBasicNameValuePair);
try {
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(
nameValuePairList);
httpPost.setEntity(urlEncodedFormEntity);
try {
HttpResponse httpResponse = httpClient
.execute(httpPost);
InputStream inputStream = httpResponse.getEntity()
.getContent();
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream);
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
String bufferedStrChunk = null;
while ((bufferedStrChunk = bufferedReader.readLine()) != null) {
stringBuilder.append(bufferedStrChunk);
}
return stringBuilder.toString();
} catch (ClientProtocolException cpe) {
System.out
.println("First Exception coz of HttpResponese :"
+ cpe);
cpe.printStackTrace();
} catch (IOException ioe) {
System.out
.println("Second Exception coz of HttpResponse :"
+ ioe);
ioe.printStackTrace();
}
} catch (UnsupportedEncodingException uee) {
System.out
.println("An Exception given because of UrlEncodedFormEntity argument :"
+ uee);
uee.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}
SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
sendPostReqAsyncTask.execute(token);
}