-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathFCMHelper.java
More file actions
156 lines (130 loc) · 4.82 KB
/
FCMHelper.java
File metadata and controls
156 lines (130 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package org.mosdev.fcmhelper;
import com.google.gson.JsonObject;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.HttpClientBuilder;
import java.io.IOException;
/**
* Created by Toni on 30.09.2016.
*/
public class FCMHelper {
/**
* Instance
**/
private static FCMHelper instance = null;
/**
* Google URL to use firebase cloud messenging
*/
private static final String URL_SEND = "https://fcm.googleapis.com/fcm/send";
/**
* STATIC TYPES
*/
public static final String TYPE_TO = "to"; // Use for single devices, device groups and topics
public static final String TYPE_CONDITION = "condition"; // Use for Conditions
/**
* Your SECRET server key
*/
private static final String FCM_SERVER_KEY = "<Enter your server key here>";
public static FCMHelper getInstance() {
if (instance == null) instance = new FCMHelper();
return instance;
}
private FCMHelper() {}
/**
* Send notification
* @param type
* @param typeParameter
* @param notificationObject
* @return
* @throws IOException
*/
public String sendNotification(String type, String typeParameter, JsonObject notificationObject) throws IOException {
return sendNotifictaionAndData(type, typeParameter, notificationObject, null);
}
/**
* Send data
* @param type
* @param typeParameter
* @param dataObject
* @return
* @throws IOException
*/
public String sendData(String type, String typeParameter, JsonObject dataObject) throws IOException {
return sendNotifictaionAndData(type, typeParameter, null, dataObject);
}
/**
* Send notification and data
* @param type
* @param typeParameter
* @param notificationObject
* @param dataObject
* @return
* @throws IOException
*/
public String sendNotifictaionAndData(String type, String typeParameter, JsonObject notificationObject, JsonObject dataObject) throws IOException {
String result = null;
if (type.equals(TYPE_TO) || type.equals(TYPE_CONDITION)) {
JsonObject sendObject = new JsonObject();
sendObject.addProperty(type, typeParameter);
result = sendFcmMessage(sendObject, notificationObject, dataObject);
}
return result;
}
/**
* Send data to a topic
* @param topic
* @param dataObject
* @return
* @throws IOException
*/
public String sendTopicData(String topic, JsonObject dataObject) throws IOException{
return sendData(TYPE_TO, "/topics/" + topic, dataObject);
}
/**
* Send notification to a topic
* @param topic
* @param notificationObject
* @return
* @throws IOException
*/
public String sendTopicNotification(String topic, JsonObject notificationObject) throws IOException{
return sendNotification(TYPE_TO, "/topics/" + topic, notificationObject);
}
/**
* Send notification and data to a topic
* @param topic
* @param notificationObject
* @param dataObject
* @return
* @throws IOException
*/
public String sendTopicNotificationAndData(String topic, JsonObject notificationObject, JsonObject dataObject) throws IOException{
return sendNotifictaionAndData(TYPE_TO, "/topics/" + topic, notificationObject, dataObject);
}
/**
* Send a Firebase Cloud Message
* @param sendObject - Contains to or condition
* @param notificationObject - Notification Data
* @param dataObject - Data
* @return
* @throws IOException
*/
private String sendFcmMessage(JsonObject sendObject, JsonObject notificationObject, JsonObject dataObject) throws IOException {
HttpPost httpPost = new HttpPost(URL_SEND);
// Header setzen
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("Authorization", "key=" + FCM_SERVER_KEY);
if (notificationObject != null) sendObject.add("notification", notificationObject);
if (dataObject != null) sendObject.add("data", dataObject);
String data = sendObject.toString();
StringEntity entity = new StringEntity(data);
// JSON-Object übergeben
httpPost.setEntity(entity);
HttpClient httpClient = HttpClientBuilder.create().build();
BasicResponseHandler responseHandler = new BasicResponseHandler();
String response = (String) httpClient.execute(httpPost, responseHandler);
return response;
}
}