Skip to content

Commit 19fdc6f

Browse files
authored
Update FirebasePluginMessagingService.java
1 parent c8f137a commit 19fdc6f

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/android/FirebasePluginMessagingService.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.apache.cordova.firebase;
22

3+
import static android.text.TextUtils.isEmpty;
4+
35
import android.app.NotificationChannel;
46
import android.app.NotificationManager;
57
import android.app.PendingIntent;
@@ -47,6 +49,7 @@ public class FirebasePluginMessagingService extends FirebaseMessagingService {
4749

4850
static final String imageTypeCircle = "circle";
4951
static final String imageTypeBigPicture = "big_picture";
52+
private static String lastPostId= "";
5053

5154
/**
5255
* Called if InstanceID token is updated. This may occur if the security of
@@ -113,7 +116,7 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
113116
if(FirebasePlugin.applicationContext == null){
114117
FirebasePlugin.applicationContext = this.getApplicationContext();
115118
}
116-
119+
Log.d(TAG, "From: " + remoteMessage.getFrom());
117120
// TODO(developer): Handle FCM messages here.
118121
// Not getting messages here? See why this may be: https://goo.gl/39bRNJ
119122
String messageType;
@@ -160,11 +163,11 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
160163
if (notification.getImageUrl() != null) {
161164
image = notification.getImageUrl().toString();
162165
}
163-
if (!TextUtils.isEmpty(titleLocKey)) {
166+
if (!isEmpty(titleLocKey)) {
164167
int titleId = getResources().getIdentifier(titleLocKey, "string", getPackageName());
165168
title = String.format(getResources().getString(titleId), (Object[])titleLocArgs);
166169
}
167-
if (!TextUtils.isEmpty(bodyLocKey)) {
170+
if (!isEmpty(bodyLocKey)) {
168171
int bodyId = getResources().getIdentifier(bodyLocKey, "string", getPackageName());
169172
body = String.format(getResources().getString(bodyId), (Object[])bodyLocArgs);
170173
}
@@ -178,6 +181,10 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
178181
if(data.containsKey("notification_foreground")){
179182
foregroundNotification = true;
180183
}
184+
if(data.containsKey("post_id") && !isEmpty(data.get("post_id")) && lastPostId.equalsIgnoreCase(data.get("post_id"))){
185+
return;
186+
}
187+
if(data.containsKey("post_id")) lastPostId = data.get("post_id");
181188
if(data.containsKey("notification_title")) title = data.get("notification_title");
182189
if(data.containsKey("notification_body")) body = data.get("notification_body");
183190
if(data.containsKey("notification_android_body_html")) bodyHtml = data.get("notification_android_body_html");
@@ -194,7 +201,7 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
194201
if(data.containsKey("notification_android_image_type")) imageType = data.get("notification_android_image_type");
195202
}
196203

197-
if (TextUtils.isEmpty(id)) {
204+
if (isEmpty(id)) {
198205
Random rand = new Random();
199206
int n = rand.nextInt(50) + 1;
200207
id = Integer.toString(n);
@@ -216,8 +223,8 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
216223
Log.d(TAG, "image Type: " + imageType);
217224

218225

219-
if (!TextUtils.isEmpty(body) || !TextUtils.isEmpty(title) || (data != null && !data.isEmpty())) {
220-
boolean showNotification = (FirebasePlugin.inBackground() || !FirebasePlugin.hasNotificationsCallback() || foregroundNotification) && (!TextUtils.isEmpty(body) || !TextUtils.isEmpty(title));
226+
if (!isEmpty(body) || !isEmpty(title) || (data != null && !data.isEmpty())) {
227+
boolean showNotification = (FirebasePlugin.inBackground() || !FirebasePlugin.hasNotificationsCallback() || foregroundNotification) && (!isEmpty(body) || !isEmpty(title));
221228
sendMessage(remoteMessage, data, messageType, id, title, body, bodyHtml, showNotification, sound, vibrate, light, color, icon, channelId, priority, visibility, image, imageType);
222229
}
223230
}catch (Exception e){

0 commit comments

Comments
 (0)