-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFcmNotificationsSender.java
More file actions
96 lines (65 loc) · 2.8 KB
/
FcmNotificationsSender.java
File metadata and controls
96 lines (65 loc) · 2.8 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
package com.apps6283.vrcreator;
import android.app.Activity;
import android.content.Context;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class FcmNotificationsSender {
String userFcmToken;
String title;
String body;
Context mContext;
Activity mActivity;
private RequestQueue requestQueue;
private final String postUrl = "https://fcm.googleapis.com/fcm/send";
private final String fcmServerKey ="AAAAXP5qWgs:APA91bFMMij4XEFJPZIQ8CQyoh-NJeAZfD0l1Fejj8M45-AtyN_0b0rUuRq_7codcFEJzev0Hu3cGnX8acA9LlqWK7a4tW886XHUXzIICLW2S3HBG_5ZH3Of93-SL1urTmmkvnGVRSHc";
public FcmNotificationsSender(String userFcmToken, String title, String body, Context mContext, Activity mActivity) {
this.userFcmToken = userFcmToken;
this.title = title;
this.body = body;
this.mContext = mContext;
this.mActivity = mActivity;
}
public void SendNotifications() {
requestQueue = Volley.newRequestQueue(mActivity);
JSONObject mainObj = new JSONObject();
try {
mainObj.put("to", userFcmToken);
JSONObject notiObject = new JSONObject();
notiObject.put("title", title);
notiObject.put("body", body);
notiObject.put("icon", "icon"); // enter icon that exists in drawable only
mainObj.put("notification", notiObject);
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, postUrl, mainObj, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// code run is got response
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// code run is got error
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> header = new HashMap<>();
header.put("content-type", "application/json");
header.put("authorization", "key=" + fcmServerKey);
return header;
}
};
requestQueue.add(request);
} catch (JSONException e) {
e.printStackTrace();
}
}
}