Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
W
wecloud_im_server
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
hewei
wecloud_im_server
Commits
f119af69
Commit
f119af69
authored
Jun 04, 2021
by
giaogiao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
FirebaseTest.java
parent
9a31da94
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
145 additions
and
3 deletions
+145
-3
bootstrap/src/test/java/io/geekidea/springbootplus/test/FirebaseTest.java
+59
-0
common/src/main/java/com/wecloud/im/tillo/app_ws/sender/PushTask.java
+86
-3
No files found.
bootstrap/src/test/java/io/geekidea/springbootplus/test/FirebaseTest.java
0 → 100644
View file @
f119af69
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
);
}
}
}
common/src/main/java/com/wecloud/im/tillo/app_ws/sender/PushTask.java
View file @
f119af69
...
...
@@ -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原生"
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment