Skip to content

Commit 26d3fde

Browse files
authored
Merge pull request #196 from Iterable/feature/MOB-1187-add-isghostpush-method
[MOB-1187] Add isGhostPush method for apps to detect ghost/silent pushes
2 parents 0a9cdbf + 84c4a5a commit 26d3fde

File tree

4 files changed

+54
-5
lines changed

4 files changed

+54
-5
lines changed

iterableapi/src/main/java/com/iterable/iterableapi/IterableFirebaseMessagingService.java

+18-4
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,7 @@ public static boolean handleMessageReceived(Context context, RemoteMessage remot
4646
IterableLogger.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
4747
}
4848

49-
Bundle extras = new Bundle();
50-
for (Map.Entry<String, String> entry : messageData.entrySet()) {
51-
extras.putString(entry.getKey(), entry.getValue());
52-
}
49+
Bundle extras = IterableNotificationHelper.mapToBundle(messageData);
5350

5451
if (!IterableNotificationHelper.isIterablePush(extras)) {
5552
IterableLogger.d(TAG, "Not an Iterable push message");
@@ -92,6 +89,23 @@ public static void handleTokenRefresh() {
9289
IterableLogger.d(TAG, "New Firebase Token generated: " + registrationToken);
9390
IterableApi.getInstance().registerForPush();
9491
}
92+
93+
/**
94+
* Checks if the message is an Iterable ghost push or silent push message
95+
* @param remoteMessage Remote message received from Firebase in
96+
* {@link FirebaseMessagingService#onMessageReceived(RemoteMessage)}
97+
* @return Boolean indicating whether the message is an Iterable ghost push or silent push
98+
*/
99+
public static boolean isGhostPush(RemoteMessage remoteMessage) {
100+
Map<String,String> messageData = remoteMessage.getData();
101+
102+
if (messageData == null || messageData.isEmpty()) {
103+
return false;
104+
}
105+
106+
Bundle extras = IterableNotificationHelper.mapToBundle(messageData);
107+
return IterableNotificationHelper.isGhostPush(extras);
108+
}
95109
}
96110

97111

iterableapi/src/main/java/com/iterable/iterableapi/IterableNotificationHelper.java

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import org.json.JSONException;
1919
import org.json.JSONObject;
2020

21+
import java.util.Map;
22+
2123
class IterableNotificationHelper {
2224
private static final String DEFAULT_CHANNEL_NAME = "iterable channel";
2325

@@ -79,6 +81,14 @@ static boolean isEmptyBody(Bundle extras) {
7981
return instance.isEmptyBody(extras);
8082
}
8183

84+
static Bundle mapToBundle(Map<String,String> map) {
85+
Bundle bundle = new Bundle();
86+
for (Map.Entry<String, String> entry : map.entrySet()) {
87+
bundle.putString(entry.getKey(), entry.getValue());
88+
}
89+
return bundle;
90+
}
91+
8292
static class IterableNotificationHelperImpl {
8393

8494
public IterableNotificationBuilder createNotification(Context context, Bundle extras) {

iterableapi/src/test/java/com/iterable/iterableapi/IterableFirebaseMessagingServiceTest.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import android.content.Intent;
44
import android.os.Bundle;
55

6-
import com.google.firebase.iid.FirebaseInstanceId;
76
import com.google.firebase.messaging.RemoteMessage;
87

98
import org.junit.After;
@@ -21,6 +20,8 @@
2120

2221
import static com.iterable.iterableapi.IterableTestUtils.bundleToMap;
2322
import static junit.framework.Assert.assertEquals;
23+
import static junit.framework.TestCase.assertFalse;
24+
import static junit.framework.TestCase.assertTrue;
2425
import static org.mockito.ArgumentMatchers.any;
2526
import static org.mockito.ArgumentMatchers.eq;
2627
import static org.mockito.Mockito.doNothing;
@@ -112,4 +113,20 @@ public void testSilentPushInAppRemoved() throws Exception {
112113
verify(inAppManagerSpy).removeMessage("1234567890abcdef");
113114
}
114115

116+
@Test
117+
public void testIsGhostPushWithGhostPushMessage() throws Exception {
118+
RemoteMessage.Builder builder = new RemoteMessage.Builder("[email protected]");
119+
builder.setData(IterableTestUtils.getMapFromJsonResource("push_payload_ghost_push.json"));
120+
assertTrue(IterableFirebaseMessagingService.isGhostPush(builder.build()));
121+
verify(notificationHelperSpy).isGhostPush(any(Bundle.class));
122+
}
123+
124+
@Test
125+
public void testIsGhostPushWithNotificationMessage() throws Exception {
126+
RemoteMessage.Builder builder = new RemoteMessage.Builder("[email protected]");
127+
builder.setData(IterableTestUtils.getMapFromJsonResource("push_payload_legacy_deep_link.json"));
128+
assertFalse(IterableFirebaseMessagingService.isGhostPush(builder.build()));
129+
verify(notificationHelperSpy).isGhostPush(any(Bundle.class));
130+
}
131+
115132
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"itbl": {
3+
"campaignId": 1234,
4+
"templateId": 4321,
5+
"messageId": "123456789abcdef",
6+
"isGhostPush": true
7+
}
8+
}

0 commit comments

Comments
 (0)