Skip to content

Commit 037782c

Browse files
committed
Add support for a custom Firebase messaging service
1 parent c66e0e0 commit 037782c

File tree

4 files changed

+60
-15
lines changed

4 files changed

+60
-15
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,29 @@ compile 'com.iterable:iterableapi:3.0.5'
3434
compile 'com.google.firebase:firebase-messaging:X.X.X' // Min version 9.0.0
3535
```
3636

37-
See [Bintray](https://bintray.com/davidtruong/maven/Iterable-SDK) for the latest version of the Iterable Android SDK.
37+
See [Bintray](https://bintray.com/davidtruong/maven/Iterable-SDK) for the latest version of the Iterable Android SDK.
38+
39+
#### Handling Firebase push messages and tokens
40+
41+
The SDK adds a FirebaseMessagingService and FirebaseInstanceIdService to the app manifest automatically, so you don't have to do any extra setup to handle incoming push messages.
42+
If your application implements its own FirebaseMessagingService, make sure you forward `onMessageReceived` and `onNewToken` calls to `IterableFirebaseMessagingService.handleMessageReceived` and `IterableFirebaseInstanceIDService.handleTokenRefresh`, respectively:
43+
44+
```java
45+
public class MyFirebaseMessagingService extends FirebaseMessagingService {
46+
47+
@Override
48+
public void onMessageReceived(RemoteMessage remoteMessage) {
49+
IterableFirebaseMessagingService.handleMessageReceived(this, remoteMessage);
50+
}
51+
52+
@Override
53+
public void onNewToken(String s) {
54+
IterableFirebaseInstanceIDService.handleTokenRefresh();
55+
}
56+
}
57+
```
58+
59+
Note that `FirebaseInstanceIdService` is deprecated and replaced with `onNewToken` in recent versions of Firebase.
3860

3961
# Using the SDK
4062

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ public class IterableFirebaseInstanceIDService extends FirebaseInstanceIdService
1818

1919
@Override
2020
public void onTokenRefresh() {
21+
handleTokenRefresh();
22+
}
23+
24+
/**
25+
* Handles token refresh
26+
* Call this from a custom {@link FirebaseInstanceIdService} to register the new token with Iterable
27+
*/
28+
public static void handleTokenRefresh() {
2129
String registrationToken = FirebaseInstanceId.getInstance().getToken();
2230
IterableLogger.d(TAG, "New Firebase Token generated: " + registrationToken);
2331
IterableApi.getInstance().registerForPush();

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

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.iterable.iterableapi;
22

3-
import android.content.ComponentName;
43
import android.content.Context;
5-
import android.content.Intent;
6-
import android.content.pm.PackageManager;
74
import android.os.AsyncTask;
85
import android.os.Bundle;
96

@@ -18,40 +15,54 @@ public class IterableFirebaseMessagingService extends FirebaseMessagingService {
1815

1916
@Override
2017
public void onMessageReceived(RemoteMessage remoteMessage) {
21-
if (remoteMessage.getData().size() > 0) {
22-
Map<String,String> messageData = remoteMessage.getData();
23-
handlePushReceived(messageData);
24-
IterableLogger.d(TAG, "Message data payload: " + remoteMessage.getData());
18+
handleMessageReceived(this, remoteMessage);
19+
}
20+
21+
/**
22+
* Handles receiving an incoming push notification from the intent.
23+
*
24+
* Call this from a custom {@link FirebaseMessagingService} to pass Iterable push messages to
25+
* Iterable SDK for tracking and rendering
26+
* @param remoteMessage Remote message received from Firebase in
27+
* {@link FirebaseMessagingService#onMessageReceived(RemoteMessage)}
28+
* @return Boolean indicating whether it was an Iterable message or not
29+
*/
30+
public static boolean handleMessageReceived(Context context, RemoteMessage remoteMessage) {
31+
Map<String,String> messageData = remoteMessage.getData();
32+
33+
if (messageData == null || messageData.size() == 0) {
34+
return false;
2535
}
2636

37+
IterableLogger.d(TAG, "Message data payload: " + remoteMessage.getData());
2738
// Check if message contains a notification payload.
2839
if (remoteMessage.getNotification() != null) {
2940
IterableLogger.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
3041
}
31-
}
3242

33-
/**
34-
* Handles receiving an incoming push notification from the intent.
35-
* @param messageData
36-
*/
37-
private void handlePushReceived(Map<String,String> messageData) {
3843
Bundle extras = new Bundle();
3944
for (Map.Entry<String, String> entry : messageData.entrySet()) {
4045
extras.putString(entry.getKey(), entry.getValue());
4146
}
4247

48+
if (!IterableNotificationBuilder.isIterablePush(extras)) {
49+
IterableLogger.d(TAG, "Not an Iterable push message");
50+
return false;
51+
}
52+
4353
if (!IterableNotificationBuilder.isGhostPush(extras)) {
4454
if (!IterableNotificationBuilder.isEmptyBody(extras)) {
4555
IterableLogger.d(TAG, "Iterable push received " + messageData);
4656
IterableNotificationBuilder notificationBuilder = IterableNotificationBuilder.createNotification(
47-
getApplicationContext(), extras);
57+
context.getApplicationContext(), extras);
4858
new IterableNotificationManager().execute(notificationBuilder);
4959
} else {
5060
IterableLogger.d(TAG, "Iterable OS notification push received");
5161
}
5262
} else {
5363
IterableLogger.d(TAG, "Iterable ghost silent push received");
5464
}
65+
return true;
5566
}
5667
}
5768

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,10 @@ private static int getIconId(Context context) {
376376
return iconId;
377377
}
378378

379+
static boolean isIterablePush(Bundle extras) {
380+
return extras != null && extras.containsKey(IterableConstants.ITERABLE_DATA_KEY);
381+
}
382+
379383
/**
380384
* Returns if the given notification is a ghost/silent push notification
381385
*

0 commit comments

Comments
 (0)