Commit f119af69 by giaogiao

FirebaseTest.java

parent 9a31da94
package io.geekidea.springbootplus.test;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
@Slf4j
public class FirebaseTest {
public static void main(String[] args) {
String to = "";
String key = "";
String jsonStr = null;
try {
URL url = new URL("https://fcm.googleapis.com/fcm/send");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setConnectTimeout(10000);
conn.setRequestMethod("POST");
//不设置默认发送文本格式。设置就是json
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", "key=" + key);
JSONObject json = new JSONObject();
//推送到哪台客户端机器
json.put("to", to);
JSONObject info = new JSONObject();
info.put("title", "新消息");
info.put("body", "点击查看");
//数据消息data 通知消息 notification
json.put("notification", info);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
jsonStr = json.toString();
wr.write(jsonStr);
wr.flush();
InputStream inputStream = conn.getInputStream();
InputStreamReader in = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(in);
// String line = null;
wr.close();
reader.close();
} catch (Exception e) {
log.error("FCM push failure: " + jsonStr, e);
}
}
}
......@@ -5,9 +5,17 @@ import com.wecloud.im.entity.ImClient;
import com.wecloud.im.entity.ImMessage;
import com.wecloud.im.push.PushUtils;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* 异步系统推送
*/
......@@ -15,6 +23,10 @@ import org.springframework.stereotype.Component;
@Slf4j
public class PushTask {
/**
* 谷歌推送地址
*/
private static final String API_URL_FCM = "https://fcm.googleapis.com/fcm/send";
/**
* 异步系统推送
......@@ -27,7 +39,6 @@ public class PushTask {
public void push(ImClient imClientReceiver, ImClient imClientSender, ImMessage imMessage, ImApplication imApplication) {
log.debug("push " + imClientReceiver.getClientId());
// 校验参数
if (imClientReceiver.getValid() == null || imClientReceiver.getDeviceToken() == null || imClientReceiver.getDeviceType() == null) {
......@@ -44,11 +55,11 @@ public class PushTask {
ios(imClientReceiver, imApplication);
} else {
android(imClientReceiver, imApplication);
android(imClientReceiver, imApplication, imMessage);
}
}
private void android(ImClient imClientReceiver, ImApplication imApplication) {
private void android(ImClient imClientReceiver, ImApplication imApplication, ImMessage imMessage) {
// 安卓推送通道,友盟:1;firebase:2; 信鸽3
if (imApplication.getAndroidPushChannel() == 1) {
log.debug("友盟");
......@@ -72,6 +83,41 @@ public class PushTask {
//firebase:2
log.debug("firebase");
String jsonStr = null;
try {
URL url = new URL(API_URL_FCM);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setConnectTimeout(10000);
conn.setRequestMethod("POST");
//不设置默认发送文本格式。设置就是json
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", "key=" + imApplication.getFirebaseSecret());
JSONObject json = new JSONObject();
//推送到哪台客户端机器
json.put("to", imClientReceiver.getDeviceToken());
JSONObject info = new JSONObject();
info.put("title", "新消息");
info.put("body", "点击查看");
//数据消息data 通知消息 notification
json.put("notification", info);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
jsonStr = json.toString();
wr.write(jsonStr);
wr.flush();
InputStream inputStream = conn.getInputStream();
InputStreamReader in = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(in);
// String line = null;
wr.close();
reader.close();
} catch (Exception e) {
log.error("FCM push failure: " + jsonStr, e);
}
} else if (imApplication.getAndroidPushChannel() == 3) {
// 信鸽3
......@@ -109,6 +155,43 @@ public class PushTask {
log.debug("firebase");
String jsonStr = null;
try {
URL url = new URL(API_URL_FCM);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setConnectTimeout(10000);
conn.setRequestMethod("POST");
//不设置默认发送文本格式。设置就是json
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", "key=" + imApplication.getFirebaseSecret());
JSONObject json = new JSONObject();
//推送到哪台客户端机器
json.put("to", imClientReceiver.getDeviceToken());
JSONObject info = new JSONObject();
info.put("title", "新消息");
info.put("body", "点击查看");
//数据消息data 通知消息 notification
json.put("notification", info);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
jsonStr = json.toString();
wr.write(jsonStr);
wr.flush();
InputStream inputStream = conn.getInputStream();
InputStreamReader in = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(in);
// String line = null;
wr.close();
reader.close();
} catch (Exception e) {
log.error("FCM push failure: " + jsonStr, e);
}
} else if (imApplication.getIosPushChannel() == 3) {
// apns原生:3
log.debug("apns原生");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment