cordova-plugin-appice 2.0.2 → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,348 +1,348 @@
1
- package org.apache.cordova.firebase;
2
-
3
- import android.app.NotificationChannel;
4
- import android.app.NotificationManager;
5
- import android.app.PendingIntent;
6
- import android.content.Context;
7
- import android.content.Intent;
8
- import android.graphics.BitmapFactory;
9
- import android.media.RingtoneManager;
10
- import android.net.Uri;
11
- import android.os.Build;
12
- import android.os.Bundle;
13
- import androidx.core.app.NotificationCompat;
14
- import android.util.Log;
15
- import android.app.Notification;
16
- import android.text.TextUtils;
17
- import android.content.ContentResolver;
18
- import android.graphics.Color;
19
-
20
- import com.google.firebase.crashlytics.FirebaseCrashlytics;
21
- import com.google.firebase.messaging.FirebaseMessagingService;
22
- import com.google.firebase.messaging.RemoteMessage;
23
-
24
- import java.util.Map;
25
- import java.util.Random;
26
-
27
- public class FirebasePluginMessagingService extends FirebaseMessagingService {
28
-
29
- private static final String TAG = "FirebasePlugin";
30
-
31
- static final String defaultSmallIconName = "notification_icon";
32
- static final String defaultLargeIconName = "notification_icon_large";
33
-
34
-
35
- /**
36
- * Called if InstanceID token is updated. This may occur if the security of
37
- * the previous token had been compromised. Note that this is called when the InstanceID token
38
- * is initially generated so this is where you would retrieve the token.
39
- */
40
- @Override
41
- public void onNewToken(String refreshedToken) {
42
- try{
43
- super.onNewToken(refreshedToken);
44
- Log.d(TAG, "Refreshed token: " + refreshedToken);
45
- FirebasePlugin.sendToken(refreshedToken);
46
- }catch (Exception e){
47
- FirebasePlugin.handleExceptionWithoutContext(e);
48
- }
49
- }
50
-
51
-
52
- /**
53
- * Called when message is received.
54
- * Called IF message is a data message (i.e. NOT sent from Firebase console)
55
- * OR if message is a notification message (e.g. sent from Firebase console) AND app is in foreground.
56
- * Notification messages received while app is in background will not be processed by this method;
57
- * they are handled internally by the OS.
58
- *
59
- * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
60
- */
61
- @Override
62
- public void onMessageReceived(RemoteMessage remoteMessage) {
63
- try{
64
- // [START_EXCLUDE]
65
- // There are two types of messages data messages and notification messages. Data messages are handled
66
- // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
67
- // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
68
- // is in the foreground. When the app is in the background an automatically generated notification is displayed.
69
- // When the user taps on the notification they are returned to the app. Messages containing both notification
70
- // and data payloads are treated as notification messages. The Firebase console always sends notification
71
- // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
72
- // [END_EXCLUDE]
73
-
74
- // Pass the message to the receiver manager so any registered receivers can decide to handle it
75
- boolean wasHandled = FirebasePluginMessageReceiverManager.onMessageReceived(remoteMessage);
76
- if (wasHandled) {
77
- Log.d(TAG, "Message was handled by a registered receiver");
78
-
79
- // Don't process the message in this method.
80
- return;
81
- }
82
-
83
- if(FirebasePlugin.applicationContext == null){
84
- FirebasePlugin.applicationContext = this.getApplicationContext();
85
- }
86
-
87
- // TODO(developer): Handle FCM messages here.
88
- // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
89
- String messageType;
90
- String title = null;
91
- String body = null;
92
- String id = null;
93
- String sound = null;
94
- String vibrate = null;
95
- String light = null;
96
- String color = null;
97
- String icon = null;
98
- String channelId = null;
99
- String visibility = null;
100
- String priority = null;
101
- boolean foregroundNotification = false;
102
-
103
- Map<String, String> data = remoteMessage.getData();
104
-
105
- if (remoteMessage.getNotification() != null) {
106
- // Notification message payload
107
- Log.i(TAG, "Received message: notification");
108
- messageType = "notification";
109
- id = remoteMessage.getMessageId();
110
- RemoteMessage.Notification notification = remoteMessage.getNotification();
111
- title = notification.getTitle();
112
- body = notification.getBody();
113
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
114
- channelId = notification.getChannelId();
115
- }
116
- sound = notification.getSound();
117
- color = notification.getColor();
118
- icon = notification.getIcon();
119
- }else{
120
- Log.i(TAG, "Received message: data");
121
- messageType = "data";
122
- }
123
-
124
- if (data != null) {
125
- // Data message payload
126
- if(data.containsKey("notification_foreground")){
127
- foregroundNotification = true;
128
- }
129
- if(data.containsKey("notification_title")) title = data.get("notification_title");
130
- if(data.containsKey("notification_body")) body = data.get("notification_body");
131
- if(data.containsKey("notification_android_channel_id")) channelId = data.get("notification_android_channel_id");
132
- if(data.containsKey("notification_android_id")) id = data.get("notification_android_id");
133
- if(data.containsKey("notification_android_sound")) sound = data.get("notification_android_sound");
134
- if(data.containsKey("notification_android_vibrate")) vibrate = data.get("notification_android_vibrate");
135
- if(data.containsKey("notification_android_light")) light = data.get("notification_android_light"); //String containing hex ARGB color, miliseconds on, miliseconds off, example: '#FFFF00FF,1000,3000'
136
- if(data.containsKey("notification_android_color")) color = data.get("notification_android_color");
137
- if(data.containsKey("notification_android_icon")) icon = data.get("notification_android_icon");
138
- if(data.containsKey("notification_android_visibility")) visibility = data.get("notification_android_visibility");
139
- if(data.containsKey("notification_android_priority")) priority = data.get("notification_android_priority");
140
- }
141
-
142
- if (TextUtils.isEmpty(id)) {
143
- Random rand = new Random();
144
- int n = rand.nextInt(50) + 1;
145
- id = Integer.toString(n);
146
- }
147
-
148
- Log.d(TAG, "From: " + remoteMessage.getFrom());
149
- Log.d(TAG, "Id: " + id);
150
- Log.d(TAG, "Title: " + title);
151
- Log.d(TAG, "Body: " + body);
152
- Log.d(TAG, "Sound: " + sound);
153
- Log.d(TAG, "Vibrate: " + vibrate);
154
- Log.d(TAG, "Light: " + light);
155
- Log.d(TAG, "Color: " + color);
156
- Log.d(TAG, "Icon: " + icon);
157
- Log.d(TAG, "Channel Id: " + channelId);
158
- Log.d(TAG, "Visibility: " + visibility);
159
- Log.d(TAG, "Priority: " + priority);
160
-
161
-
162
- if (!TextUtils.isEmpty(body) || !TextUtils.isEmpty(title) || (data != null && !data.isEmpty())) {
163
- boolean showNotification = (FirebasePlugin.inBackground() || !FirebasePlugin.hasNotificationsCallback() || foregroundNotification) && (!TextUtils.isEmpty(body) || !TextUtils.isEmpty(title));
164
- sendMessage(remoteMessage, data, messageType, id, title, body, showNotification, sound, vibrate, light, color, icon, channelId, priority, visibility);
165
- }
166
- }catch (Exception e){
167
- FirebasePlugin.handleExceptionWithoutContext(e);
168
- }
169
- }
170
-
171
- private void sendMessage(RemoteMessage remoteMessage, Map<String, String> data, String messageType, String id, String title, String body, boolean showNotification, String sound, String vibrate, String light, String color, String icon, String channelId, String priority, String visibility) {
172
- Log.d(TAG, "sendMessage(): messageType="+messageType+"; showNotification="+showNotification+"; id="+id+"; title="+title+"; body="+body+"; sound="+sound+"; vibrate="+vibrate+"; light="+light+"; color="+color+"; icon="+icon+"; channel="+channelId+"; data="+data.toString());
173
- Bundle bundle = new Bundle();
174
- for (String key : data.keySet()) {
175
- bundle.putString(key, data.get(key));
176
- }
177
- bundle.putString("messageType", messageType);
178
- this.putKVInBundle("id", id, bundle);
179
- this.putKVInBundle("title", title, bundle);
180
- this.putKVInBundle("body", body, bundle);
181
- this.putKVInBundle("sound", sound, bundle);
182
- this.putKVInBundle("vibrate", vibrate, bundle);
183
- this.putKVInBundle("light", light, bundle);
184
- this.putKVInBundle("color", color, bundle);
185
- this.putKVInBundle("icon", icon, bundle);
186
- this.putKVInBundle("channel_id", channelId, bundle);
187
- this.putKVInBundle("priority", priority, bundle);
188
- this.putKVInBundle("visibility", visibility, bundle);
189
- this.putKVInBundle("show_notification", String.valueOf(showNotification), bundle);
190
- this.putKVInBundle("from", remoteMessage.getFrom(), bundle);
191
- this.putKVInBundle("collapse_key", remoteMessage.getCollapseKey(), bundle);
192
- this.putKVInBundle("sent_time", String.valueOf(remoteMessage.getSentTime()), bundle);
193
- this.putKVInBundle("ttl", String.valueOf(remoteMessage.getTtl()), bundle);
194
-
195
- if (showNotification) {
196
- Intent intent = new Intent(this, OnNotificationOpenReceiver.class);
197
- intent.putExtras(bundle);
198
- PendingIntent pendingIntent = PendingIntent.getBroadcast(this, id.hashCode(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
199
-
200
- // Channel
201
- if(channelId == null || !FirebasePlugin.channelExists(channelId)){
202
- channelId = FirebasePlugin.defaultChannelId;
203
- }
204
- if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
205
- Log.d(TAG, "Channel ID: "+channelId);
206
- }
207
-
208
- NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
209
- notificationBuilder
210
- .setContentTitle(title)
211
- .setContentText(body)
212
- .setStyle(new NotificationCompat.BigTextStyle().bigText(body))
213
- .setAutoCancel(true)
214
- .setContentIntent(pendingIntent);
215
-
216
- // On Android O+ the sound/lights/vibration are determined by the channel ID
217
- if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O){
218
- // Sound
219
- if (sound == null) {
220
- Log.d(TAG, "Sound: none");
221
- }else if (sound.equals("default")) {
222
- notificationBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
223
- Log.d(TAG, "Sound: default");
224
- }else{
225
- Uri soundPath = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/raw/" + sound);
226
- Log.d(TAG, "Sound: custom=" + sound+"; path="+soundPath.toString());
227
- notificationBuilder.setSound(soundPath);
228
- }
229
-
230
- // Light
231
- if (light != null) {
232
- try {
233
- String[] lightsComponents = color.replaceAll("\\s", "").split(",");
234
- if (lightsComponents.length == 3) {
235
- int lightArgb = Color.parseColor(lightsComponents[0]);
236
- int lightOnMs = Integer.parseInt(lightsComponents[1]);
237
- int lightOffMs = Integer.parseInt(lightsComponents[2]);
238
- notificationBuilder.setLights(lightArgb, lightOnMs, lightOffMs);
239
- Log.d(TAG, "Lights: color="+lightsComponents[0]+"; on(ms)="+lightsComponents[2]+"; off(ms)="+lightsComponents[3]);
240
- }
241
-
242
- } catch (Exception e) {}
243
- }
244
-
245
- // Vibrate
246
- if (vibrate != null){
247
- try {
248
- String[] sVibrations = vibrate.replaceAll("\\s", "").split(",");
249
- long[] lVibrations = new long[sVibrations.length];
250
- int i=0;
251
- for(String sVibration: sVibrations){
252
- lVibrations[i] = Integer.parseInt(sVibration.trim());
253
- i++;
254
- }
255
- notificationBuilder.setVibrate(lVibrations);
256
- Log.d(TAG, "Vibrate: "+vibrate);
257
- } catch (Exception e) {
258
- Log.e(TAG, e.getMessage());
259
- }
260
- }
261
- }
262
-
263
-
264
- // Icon
265
- int defaultSmallIconResID = getResources().getIdentifier(defaultSmallIconName, "drawable", getPackageName());
266
- int customSmallIconResID = 0;
267
- if(icon != null){
268
- customSmallIconResID = getResources().getIdentifier(icon, "drawable", getPackageName());
269
- }
270
-
271
- if (customSmallIconResID != 0) {
272
- notificationBuilder.setSmallIcon(customSmallIconResID);
273
- Log.d(TAG, "Small icon: custom="+icon);
274
- }else if (defaultSmallIconResID != 0) {
275
- Log.d(TAG, "Small icon: default="+defaultSmallIconName);
276
- notificationBuilder.setSmallIcon(defaultSmallIconResID);
277
- } else {
278
- Log.d(TAG, "Small icon: application");
279
- notificationBuilder.setSmallIcon(getApplicationInfo().icon);
280
- }
281
-
282
- if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
283
- int defaultLargeIconResID = getResources().getIdentifier(defaultLargeIconName, "drawable", getPackageName());
284
- int customLargeIconResID = 0;
285
- if(icon != null){
286
- customLargeIconResID = getResources().getIdentifier(icon+"_large", "drawable", getPackageName());
287
- }
288
-
289
- int largeIconResID;
290
- if (customLargeIconResID != 0 || defaultLargeIconResID != 0) {
291
- if (customLargeIconResID != 0) {
292
- largeIconResID = customLargeIconResID;
293
- Log.d(TAG, "Large icon: custom="+icon);
294
- }else{
295
- Log.d(TAG, "Large icon: default="+defaultLargeIconName);
296
- largeIconResID = defaultLargeIconResID;
297
- }
298
- notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), largeIconResID));
299
- }
300
- }
301
-
302
- // Color
303
- if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
304
- int defaultColor = getResources().getColor(getResources().getIdentifier("accent", "color", getPackageName()), null);
305
- if(color != null){
306
- notificationBuilder.setColor(Color.parseColor(color));
307
- Log.d(TAG, "Color: custom="+color);
308
- }else{
309
- Log.d(TAG, "Color: default");
310
- notificationBuilder.setColor(defaultColor);
311
- }
312
- }
313
-
314
- // Visibility
315
- int iVisibility = NotificationCompat.VISIBILITY_PUBLIC;
316
- if(visibility != null){
317
- iVisibility = Integer.parseInt(visibility);
318
- }
319
- Log.d(TAG, "Visibility: " + iVisibility);
320
- notificationBuilder.setVisibility(iVisibility);
321
-
322
- // Priority
323
- int iPriority = NotificationCompat.PRIORITY_MAX;
324
- if(priority != null){
325
- iPriority = Integer.parseInt(priority);
326
- }
327
- Log.d(TAG, "Priority: " + iPriority);
328
- notificationBuilder.setPriority(iPriority);
329
-
330
-
331
- // Build notification
332
- Notification notification = notificationBuilder.build();
333
-
334
- // Display notification
335
- NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
336
- Log.d(TAG, "show notification: "+notification.toString());
337
- notificationManager.notify(id.hashCode(), notification);
338
- }
339
- // Send to plugin
340
- FirebasePlugin.sendMessage(bundle, this.getApplicationContext());
341
- }
342
-
343
- private void putKVInBundle(String k, String v, Bundle b){
344
- if(v != null && !b.containsKey(k)){
345
- b.putString(k, v);
346
- }
347
- }
348
- }
1
+ package org.apache.cordova.firebase;
2
+
3
+ import android.app.NotificationChannel;
4
+ import android.app.NotificationManager;
5
+ import android.app.PendingIntent;
6
+ import android.content.Context;
7
+ import android.content.Intent;
8
+ import android.graphics.BitmapFactory;
9
+ import android.media.RingtoneManager;
10
+ import android.net.Uri;
11
+ import android.os.Build;
12
+ import android.os.Bundle;
13
+ import androidx.core.app.NotificationCompat;
14
+ import android.util.Log;
15
+ import android.app.Notification;
16
+ import android.text.TextUtils;
17
+ import android.content.ContentResolver;
18
+ import android.graphics.Color;
19
+
20
+ import com.google.firebase.crashlytics.FirebaseCrashlytics;
21
+ import com.google.firebase.messaging.FirebaseMessagingService;
22
+ import com.google.firebase.messaging.RemoteMessage;
23
+
24
+ import java.util.Map;
25
+ import java.util.Random;
26
+
27
+ public class FirebasePluginMessagingService extends FirebaseMessagingService {
28
+
29
+ private static final String TAG = "FirebasePlugin";
30
+
31
+ static final String defaultSmallIconName = "notification_icon";
32
+ static final String defaultLargeIconName = "notification_icon_large";
33
+
34
+
35
+ /**
36
+ * Called if InstanceID token is updated. This may occur if the security of
37
+ * the previous token had been compromised. Note that this is called when the InstanceID token
38
+ * is initially generated so this is where you would retrieve the token.
39
+ */
40
+ @Override
41
+ public void onNewToken(String refreshedToken) {
42
+ try{
43
+ super.onNewToken(refreshedToken);
44
+ Log.d(TAG, "Refreshed token: " + refreshedToken);
45
+ FirebasePlugin.sendToken(refreshedToken);
46
+ }catch (Exception e){
47
+ FirebasePlugin.handleExceptionWithoutContext(e);
48
+ }
49
+ }
50
+
51
+
52
+ /**
53
+ * Called when message is received.
54
+ * Called IF message is a data message (i.e. NOT sent from Firebase console)
55
+ * OR if message is a notification message (e.g. sent from Firebase console) AND app is in foreground.
56
+ * Notification messages received while app is in background will not be processed by this method;
57
+ * they are handled internally by the OS.
58
+ *
59
+ * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
60
+ */
61
+ @Override
62
+ public void onMessageReceived(RemoteMessage remoteMessage) {
63
+ try{
64
+ // [START_EXCLUDE]
65
+ // There are two types of messages data messages and notification messages. Data messages are handled
66
+ // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
67
+ // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
68
+ // is in the foreground. When the app is in the background an automatically generated notification is displayed.
69
+ // When the user taps on the notification they are returned to the app. Messages containing both notification
70
+ // and data payloads are treated as notification messages. The Firebase console always sends notification
71
+ // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
72
+ // [END_EXCLUDE]
73
+
74
+ // Pass the message to the receiver manager so any registered receivers can decide to handle it
75
+ boolean wasHandled = FirebasePluginMessageReceiverManager.onMessageReceived(remoteMessage);
76
+ if (wasHandled) {
77
+ Log.d(TAG, "Message was handled by a registered receiver");
78
+
79
+ // Don't process the message in this method.
80
+ return;
81
+ }
82
+
83
+ if(FirebasePlugin.applicationContext == null){
84
+ FirebasePlugin.applicationContext = this.getApplicationContext();
85
+ }
86
+
87
+ // TODO(developer): Handle FCM messages here.
88
+ // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
89
+ String messageType;
90
+ String title = null;
91
+ String body = null;
92
+ String id = null;
93
+ String sound = null;
94
+ String vibrate = null;
95
+ String light = null;
96
+ String color = null;
97
+ String icon = null;
98
+ String channelId = null;
99
+ String visibility = null;
100
+ String priority = null;
101
+ boolean foregroundNotification = false;
102
+
103
+ Map<String, String> data = remoteMessage.getData();
104
+
105
+ if (remoteMessage.getNotification() != null) {
106
+ // Notification message payload
107
+ Log.i(TAG, "Received message: notification");
108
+ messageType = "notification";
109
+ id = remoteMessage.getMessageId();
110
+ RemoteMessage.Notification notification = remoteMessage.getNotification();
111
+ title = notification.getTitle();
112
+ body = notification.getBody();
113
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
114
+ channelId = notification.getChannelId();
115
+ }
116
+ sound = notification.getSound();
117
+ color = notification.getColor();
118
+ icon = notification.getIcon();
119
+ }else{
120
+ Log.i(TAG, "Received message: data");
121
+ messageType = "data";
122
+ }
123
+
124
+ if (data != null) {
125
+ // Data message payload
126
+ if(data.containsKey("notification_foreground")){
127
+ foregroundNotification = true;
128
+ }
129
+ if(data.containsKey("notification_title")) title = data.get("notification_title");
130
+ if(data.containsKey("notification_body")) body = data.get("notification_body");
131
+ if(data.containsKey("notification_android_channel_id")) channelId = data.get("notification_android_channel_id");
132
+ if(data.containsKey("notification_android_id")) id = data.get("notification_android_id");
133
+ if(data.containsKey("notification_android_sound")) sound = data.get("notification_android_sound");
134
+ if(data.containsKey("notification_android_vibrate")) vibrate = data.get("notification_android_vibrate");
135
+ if(data.containsKey("notification_android_light")) light = data.get("notification_android_light"); //String containing hex ARGB color, miliseconds on, miliseconds off, example: '#FFFF00FF,1000,3000'
136
+ if(data.containsKey("notification_android_color")) color = data.get("notification_android_color");
137
+ if(data.containsKey("notification_android_icon")) icon = data.get("notification_android_icon");
138
+ if(data.containsKey("notification_android_visibility")) visibility = data.get("notification_android_visibility");
139
+ if(data.containsKey("notification_android_priority")) priority = data.get("notification_android_priority");
140
+ }
141
+
142
+ if (TextUtils.isEmpty(id)) {
143
+ Random rand = new Random();
144
+ int n = rand.nextInt(50) + 1;
145
+ id = Integer.toString(n);
146
+ }
147
+
148
+ Log.d(TAG, "From: " + remoteMessage.getFrom());
149
+ Log.d(TAG, "Id: " + id);
150
+ Log.d(TAG, "Title: " + title);
151
+ Log.d(TAG, "Body: " + body);
152
+ Log.d(TAG, "Sound: " + sound);
153
+ Log.d(TAG, "Vibrate: " + vibrate);
154
+ Log.d(TAG, "Light: " + light);
155
+ Log.d(TAG, "Color: " + color);
156
+ Log.d(TAG, "Icon: " + icon);
157
+ Log.d(TAG, "Channel Id: " + channelId);
158
+ Log.d(TAG, "Visibility: " + visibility);
159
+ Log.d(TAG, "Priority: " + priority);
160
+
161
+
162
+ if (!TextUtils.isEmpty(body) || !TextUtils.isEmpty(title) || (data != null && !data.isEmpty())) {
163
+ boolean showNotification = (FirebasePlugin.inBackground() || !FirebasePlugin.hasNotificationsCallback() || foregroundNotification) && (!TextUtils.isEmpty(body) || !TextUtils.isEmpty(title));
164
+ sendMessage(remoteMessage, data, messageType, id, title, body, showNotification, sound, vibrate, light, color, icon, channelId, priority, visibility);
165
+ }
166
+ }catch (Exception e){
167
+ FirebasePlugin.handleExceptionWithoutContext(e);
168
+ }
169
+ }
170
+
171
+ private void sendMessage(RemoteMessage remoteMessage, Map<String, String> data, String messageType, String id, String title, String body, boolean showNotification, String sound, String vibrate, String light, String color, String icon, String channelId, String priority, String visibility) {
172
+ Log.d(TAG, "sendMessage(): messageType="+messageType+"; showNotification="+showNotification+"; id="+id+"; title="+title+"; body="+body+"; sound="+sound+"; vibrate="+vibrate+"; light="+light+"; color="+color+"; icon="+icon+"; channel="+channelId+"; data="+data.toString());
173
+ Bundle bundle = new Bundle();
174
+ for (String key : data.keySet()) {
175
+ bundle.putString(key, data.get(key));
176
+ }
177
+ bundle.putString("messageType", messageType);
178
+ this.putKVInBundle("id", id, bundle);
179
+ this.putKVInBundle("title", title, bundle);
180
+ this.putKVInBundle("body", body, bundle);
181
+ this.putKVInBundle("sound", sound, bundle);
182
+ this.putKVInBundle("vibrate", vibrate, bundle);
183
+ this.putKVInBundle("light", light, bundle);
184
+ this.putKVInBundle("color", color, bundle);
185
+ this.putKVInBundle("icon", icon, bundle);
186
+ this.putKVInBundle("channel_id", channelId, bundle);
187
+ this.putKVInBundle("priority", priority, bundle);
188
+ this.putKVInBundle("visibility", visibility, bundle);
189
+ this.putKVInBundle("show_notification", String.valueOf(showNotification), bundle);
190
+ this.putKVInBundle("from", remoteMessage.getFrom(), bundle);
191
+ this.putKVInBundle("collapse_key", remoteMessage.getCollapseKey(), bundle);
192
+ this.putKVInBundle("sent_time", String.valueOf(remoteMessage.getSentTime()), bundle);
193
+ this.putKVInBundle("ttl", String.valueOf(remoteMessage.getTtl()), bundle);
194
+
195
+ if (showNotification) {
196
+ Intent intent = new Intent(this, OnNotificationOpenReceiver.class);
197
+ intent.putExtras(bundle);
198
+ PendingIntent pendingIntent = PendingIntent.getBroadcast(this, id.hashCode(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
199
+
200
+ // Channel
201
+ if(channelId == null || !FirebasePlugin.channelExists(channelId)){
202
+ channelId = FirebasePlugin.defaultChannelId;
203
+ }
204
+ if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
205
+ Log.d(TAG, "Channel ID: "+channelId);
206
+ }
207
+
208
+ NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
209
+ notificationBuilder
210
+ .setContentTitle(title)
211
+ .setContentText(body)
212
+ .setStyle(new NotificationCompat.BigTextStyle().bigText(body))
213
+ .setAutoCancel(true)
214
+ .setContentIntent(pendingIntent);
215
+
216
+ // On Android O+ the sound/lights/vibration are determined by the channel ID
217
+ if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O){
218
+ // Sound
219
+ if (sound == null) {
220
+ Log.d(TAG, "Sound: none");
221
+ }else if (sound.equals("default")) {
222
+ notificationBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
223
+ Log.d(TAG, "Sound: default");
224
+ }else{
225
+ Uri soundPath = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/raw/" + sound);
226
+ Log.d(TAG, "Sound: custom=" + sound+"; path="+soundPath.toString());
227
+ notificationBuilder.setSound(soundPath);
228
+ }
229
+
230
+ // Light
231
+ if (light != null) {
232
+ try {
233
+ String[] lightsComponents = color.replaceAll("\\s", "").split(",");
234
+ if (lightsComponents.length == 3) {
235
+ int lightArgb = Color.parseColor(lightsComponents[0]);
236
+ int lightOnMs = Integer.parseInt(lightsComponents[1]);
237
+ int lightOffMs = Integer.parseInt(lightsComponents[2]);
238
+ notificationBuilder.setLights(lightArgb, lightOnMs, lightOffMs);
239
+ Log.d(TAG, "Lights: color="+lightsComponents[0]+"; on(ms)="+lightsComponents[2]+"; off(ms)="+lightsComponents[3]);
240
+ }
241
+
242
+ } catch (Exception e) {}
243
+ }
244
+
245
+ // Vibrate
246
+ if (vibrate != null){
247
+ try {
248
+ String[] sVibrations = vibrate.replaceAll("\\s", "").split(",");
249
+ long[] lVibrations = new long[sVibrations.length];
250
+ int i=0;
251
+ for(String sVibration: sVibrations){
252
+ lVibrations[i] = Integer.parseInt(sVibration.trim());
253
+ i++;
254
+ }
255
+ notificationBuilder.setVibrate(lVibrations);
256
+ Log.d(TAG, "Vibrate: "+vibrate);
257
+ } catch (Exception e) {
258
+ Log.e(TAG, e.getMessage());
259
+ }
260
+ }
261
+ }
262
+
263
+
264
+ // Icon
265
+ int defaultSmallIconResID = getResources().getIdentifier(defaultSmallIconName, "drawable", getPackageName());
266
+ int customSmallIconResID = 0;
267
+ if(icon != null){
268
+ customSmallIconResID = getResources().getIdentifier(icon, "drawable", getPackageName());
269
+ }
270
+
271
+ if (customSmallIconResID != 0) {
272
+ notificationBuilder.setSmallIcon(customSmallIconResID);
273
+ Log.d(TAG, "Small icon: custom="+icon);
274
+ }else if (defaultSmallIconResID != 0) {
275
+ Log.d(TAG, "Small icon: default="+defaultSmallIconName);
276
+ notificationBuilder.setSmallIcon(defaultSmallIconResID);
277
+ } else {
278
+ Log.d(TAG, "Small icon: application");
279
+ notificationBuilder.setSmallIcon(getApplicationInfo().icon);
280
+ }
281
+
282
+ if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
283
+ int defaultLargeIconResID = getResources().getIdentifier(defaultLargeIconName, "drawable", getPackageName());
284
+ int customLargeIconResID = 0;
285
+ if(icon != null){
286
+ customLargeIconResID = getResources().getIdentifier(icon+"_large", "drawable", getPackageName());
287
+ }
288
+
289
+ int largeIconResID;
290
+ if (customLargeIconResID != 0 || defaultLargeIconResID != 0) {
291
+ if (customLargeIconResID != 0) {
292
+ largeIconResID = customLargeIconResID;
293
+ Log.d(TAG, "Large icon: custom="+icon);
294
+ }else{
295
+ Log.d(TAG, "Large icon: default="+defaultLargeIconName);
296
+ largeIconResID = defaultLargeIconResID;
297
+ }
298
+ notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), largeIconResID));
299
+ }
300
+ }
301
+
302
+ // Color
303
+ if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
304
+ int defaultColor = getResources().getColor(getResources().getIdentifier("accent", "color", getPackageName()), null);
305
+ if(color != null){
306
+ notificationBuilder.setColor(Color.parseColor(color));
307
+ Log.d(TAG, "Color: custom="+color);
308
+ }else{
309
+ Log.d(TAG, "Color: default");
310
+ notificationBuilder.setColor(defaultColor);
311
+ }
312
+ }
313
+
314
+ // Visibility
315
+ int iVisibility = NotificationCompat.VISIBILITY_PUBLIC;
316
+ if(visibility != null){
317
+ iVisibility = Integer.parseInt(visibility);
318
+ }
319
+ Log.d(TAG, "Visibility: " + iVisibility);
320
+ notificationBuilder.setVisibility(iVisibility);
321
+
322
+ // Priority
323
+ int iPriority = NotificationCompat.PRIORITY_MAX;
324
+ if(priority != null){
325
+ iPriority = Integer.parseInt(priority);
326
+ }
327
+ Log.d(TAG, "Priority: " + iPriority);
328
+ notificationBuilder.setPriority(iPriority);
329
+
330
+
331
+ // Build notification
332
+ Notification notification = notificationBuilder.build();
333
+
334
+ // Display notification
335
+ NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
336
+ Log.d(TAG, "show notification: "+notification.toString());
337
+ notificationManager.notify(id.hashCode(), notification);
338
+ }
339
+ // Send to plugin
340
+ FirebasePlugin.sendMessage(bundle, this.getApplicationContext());
341
+ }
342
+
343
+ private void putKVInBundle(String k, String v, Bundle b){
344
+ if(v != null && !b.containsKey(k)){
345
+ b.putString(k, v);
346
+ }
347
+ }
348
+ }