-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
CloudMessagingModule.java
256 lines (227 loc) 路 7.2 KB
/
CloudMessagingModule.java
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
* This file was auto-generated by the Titanium Module SDK helper for Android
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2017 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*
*/
package firebase.cloudmessaging;
import android.os.Build;
import android.app.Activity;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioAttributes;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.KrollObject;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.kroll.common.TiConfig;
import org.appcelerator.titanium.util.TiConvert;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.RemoteMessage;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.common.Log;
import java.util.HashMap;
import org.appcelerator.kroll.KrollFunction;
import java.util.Map;
import android.content.Intent;
import org.json.JSONObject;
@Kroll.module(name = "CloudMessaging", id = "firebase.cloudmessaging")
public class CloudMessagingModule extends KrollModule
{
private static final String LCAT = "FirebaseCloudMessaging";
private static CloudMessagingModule instance = null;
private String notificationData = "";
public CloudMessagingModule()
{
super();
instance = this;
}
@Kroll.onAppCreate
public static void onAppCreate(TiApplication app)
{
// put module init code that needs to run when the application is created
}
// clang-format off
@Kroll.method
@Kroll.getProperty
private KrollDict getLastData()
// clang-format on
{
KrollDict data = new KrollDict();
try {
Intent intent = TiApplication.getAppRootOrCurrentActivity().getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
for (String key : extras.keySet()) {
data.put(key, extras.get(key));
}
data.put("inBackground", true);
} else {
Log.d(LCAT, "Empty extras in Intent");
if (notificationData != "") {
data = new KrollDict(new JSONObject(notificationData));
data.put("inBackground", true);
}
}
} catch (Exception ex) {
Log.e(LCAT, "getLastData" + ex);
}
return data;
}
// Methods
@Kroll.method
public void registerForPushNotifications()
{
FirebaseInstanceId.getInstance().getToken();
parseBootIntent();
}
@Kroll.method
public void subscribeToTopic(String topic)
{
FirebaseMessaging.getInstance().subscribeToTopic(topic);
Log.d(LCAT, "subscribe to " + topic);
}
@Kroll.method
public void unsubscribeFromTopic(String topic)
{
FirebaseMessaging.getInstance().unsubscribeFromTopic(topic);
Log.d(LCAT, "unsubscribe from " + topic);
}
@Kroll.method
public void appDidReceiveMessage(KrollDict opt)
{
// empty
}
@Kroll.method
public void sendMessage(KrollDict obj)
{
FirebaseMessaging fm = FirebaseMessaging.getInstance();
String fireTo = obj.getString("to");
String fireMessageId = obj.getString("messageId");
int ttl = TiConvert.toInt(obj.get("timeToLive"), 0);
RemoteMessage.Builder rm = new RemoteMessage.Builder(fireTo);
rm.setMessageId(fireMessageId);
rm.setTtl(ttl);
// add custom data
Map<String, String> data = (HashMap) obj.get("data");
for (Object o : data.keySet()) {
rm.addData((String) o, data.get(o));
}
if (fireTo != "" && fireMessageId != "") {
fm.send(rm.build());
} else {
Log.e(LCAT, "Please set 'to' and 'messageId'");
}
}
public void onTokenRefresh(String token)
{
try {
if (hasListeners("didRefreshRegistrationToken")) {
KrollDict data = new KrollDict();
data.put("fcmToken", token);
fireEvent("didRefreshRegistrationToken", data);
}
} catch (Exception e) {
Log.e(LCAT, "Can't refresh token: " + e.getMessage());
}
}
public void onMessageReceived(HashMap message)
{
try {
if (hasListeners("didReceiveMessage")) {
KrollDict data = new KrollDict();
data.put("message", new KrollDict(message));
fireEvent("didReceiveMessage", data);
}
} catch (Exception e) {
Log.e(LCAT, "Message exception: " + e.getMessage());
}
}
@Kroll.method
public void createNotificationChannel(KrollDict options)
{
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return;
}
Log.d(LCAT, "createNotificationChannel " + options.toString());
Context context = Utils.getApplicationContext();
String sound = (String) options.optString("sound", "default");
String importance = (String) options.optString("importance", sound.equals("silent") ? "low" : "default");
String channelId = (String) options.optString("channelId", "default");
String channelName = (String) options.optString("channelName", channelId);
int importanceVal = NotificationManager.IMPORTANCE_DEFAULT;
if (importance.equals("low")) {
importanceVal = NotificationManager.IMPORTANCE_LOW;
} else if (importance.equals("high")) {
importanceVal = NotificationManager.IMPORTANCE_HIGH;
}
Uri soundUri = null;
if (sound.equals("default")) {
soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
} else if (!sound.equals("silent")) {
String path = ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/"
+ Utils.getResourceIdentifier("raw", sound);
Log.d(LCAT, "createNotificationChannel with sound " + sound + " at " + path);
soundUri = Uri.parse(path);
}
NotificationChannel channel = new NotificationChannel(channelId, channelName, importanceVal);
if (soundUri != null) {
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
.build();
channel.setSound(soundUri, audioAttributes);
}
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
}
@Kroll.getProperty
public String fcmToken()
{
return FirebaseInstanceId.getInstance().getToken();
}
@Kroll.setProperty
public void apnsToken(String str)
{
// empty
}
public static CloudMessagingModule getInstance()
{
if (instance != null)
return instance;
else
return new CloudMessagingModule();
}
public void setNotificationData(String data)
{
notificationData = data;
}
public void parseBootIntent()
{
try {
Intent intent = TiApplication.getAppRootOrCurrentActivity().getIntent();
String notification = intent.getStringExtra("fcm_data");
if (notification != null) {
HashMap<String, Object> msg = new HashMap<String, Object>();
msg.put("data", new KrollDict(new JSONObject(notification)));
onMessageReceived(msg);
intent.removeExtra("fcm_data");
} else {
Log.d(LCAT, "Empty notification in Intent");
}
} catch (Exception ex) {
Log.e(LCAT, "parseBootIntent" + ex);
}
}
}