use*_*403 32 firebase google-cloud-messaging firebase-cloud-messaging
最近我问了一个关于使用GCM 发送推送通知的问题:向Android发送推送通知.现在有了FCM,我想知道它与服务器端开发有多么不同.明智的编码,它们是一样的吗?在哪里可以找到显示从服务器向Android设备发送推送通知的示例FCM代码?
我是否需要下载任何JAR库以使用Java代码向FCM发送通知?向Android发送推送通知中的示例代码显示使用GCM发送推送通知,并且需要服务器端GCM JAR文件.
但是,另一个例子在https://www.quora.com/How-do-I-make-a-post-request-to-a-GCM-server-in-Java-to-push-a-notification-to -the-client-app显示使用GCM发送推送通知,并且不需要服务器端GCM JAR文件,因为它只是通过HTTP连接发送.FCM可以使用相同的代码吗?使用的网址是" https://android.googleapis.com/gcm/send ".什么是FCM的等效URL?
提前致谢.
AL.*_*AL. 33
服务器端编码有何不同?
由于没有太大区别,您也可以查看GCM的大多数示例服务器端代码.关于GCM和FCM的主要区别在于,当使用FCM时,您可以使用它的新功能(如本答案中所述).FCM还有一个控制台,您可以从中发送消息/通知,而无需拥有自己的应用服务器.
注意:创建自己的应用服务器取决于您.只是声明您可以通过控制台发送消息/通知.
使用的网址是" https://android.googleapis.com/gcm/send ".什么是FCM的等效URL?
FCM的等效网址是https://fcm.googleapis.com/fcm/send.您可以查看此文档以获取更多详细信息.
干杯! :d
小智 22
使用以下代码从FCM服务器发送推送通知:
public class PushNotifictionHelper {
public final static String AUTH_KEY_FCM = "Your api key";
public final static String API_URL_FCM = "https://fcm.googleapis.com/fcm/send";
public static String sendPushNotification(String deviceToken)
throws IOException {
String result = "";
URL url = new URL(API_URL_FCM);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "key=" + AUTH_KEY_FCM);
conn.setRequestProperty("Content-Type", "application/json");
JSONObject json = new JSONObject();
json.put("to", deviceToken.trim());
JSONObject info = new JSONObject();
info.put("title", "notification title"); // Notification title
info.put("body", "message body"); // Notification
// body
json.put("notification", info);
try {
OutputStreamWriter wr = new OutputStreamWriter(
conn.getOutputStream());
wr.write(json.toString());
wr.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
result = CommonConstants.SUCCESS;
} catch (Exception e) {
e.printStackTrace();
result = CommonConstants.FAILURE;
}
System.out.println("GCM Notification is sent successfully");
return result;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
74858 次 |
| 最近记录: |