Skip to content

Commit c7e9175

Browse files
authored
Allow usage of custom FirebaseMessagingService (#432)
* Simplify fcm custom service and remove manifest check * Update deprecated fcm code * Fix unit tests * Revert "Update deprecated fcm code" Changes are reverted because getToken method is added in newer version than the one in AndroidSDKFcm module dependency. Will be updated in future. This reverts commit aeea89b.
1 parent 8697d76 commit c7e9175

File tree

9 files changed

+99
-268
lines changed

9 files changed

+99
-268
lines changed

AndroidSDKFcm/src/main/java/com/leanplum/LeanplumFcmProvider.java

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016, Leanplum, Inc. All rights reserved.
2+
* Copyright 2020, Leanplum, Inc. All rights reserved.
33
*
44
* Licensed to the Apache Software Foundation (ASF) under one
55
* or more contributor license agreements. See the NOTICE file
@@ -21,19 +21,13 @@
2121

2222
package com.leanplum;
2323

24-
import android.content.Context;
25-
import android.nfc.Tag;
2624
import android.text.TextUtils;
2725

2826
import com.google.android.gms.tasks.OnCompleteListener;
2927
import com.google.android.gms.tasks.Task;
3028
import com.google.firebase.iid.FirebaseInstanceId;
3129
import com.google.firebase.iid.InstanceIdResult;
32-
import com.leanplum.internal.LeanplumManifestHelper;
3330
import com.leanplum.internal.Log;
34-
import com.leanplum.internal.Util;
35-
36-
import java.util.Collections;
3731

3832
import androidx.annotation.NonNull;
3933

@@ -74,41 +68,6 @@ public boolean isInitialized() {
7468
return true;
7569
}
7670

77-
@Override
78-
public boolean isManifestSetup() {
79-
Context context = Leanplum.getContext();
80-
if (context == null) {
81-
return false;
82-
}
83-
84-
try {
85-
boolean hasPushReceiver = LeanplumManifestHelper.checkComponent(LeanplumManifestHelper.ApplicationComponent.RECEIVER,
86-
LeanplumManifestHelper.LP_PUSH_RECEIVER, false, null,
87-
Collections.singletonList(LeanplumManifestHelper.LP_PUSH_FCM_MESSAGING_SERVICE), context.getPackageName());
88-
89-
boolean hasPushFirebaseMessagingService = LeanplumManifestHelper.checkComponent(
90-
LeanplumManifestHelper.ApplicationComponent.SERVICE,
91-
LeanplumManifestHelper.LP_PUSH_FCM_MESSAGING_SERVICE, false, null,
92-
Collections.singletonList(LeanplumManifestHelper.FCM_MESSAGING_EVENT), context.getPackageName());
93-
94-
boolean hasRegistrationService = LeanplumManifestHelper.checkComponent(
95-
LeanplumManifestHelper.ApplicationComponent.SERVICE,
96-
LeanplumPushRegistrationService.class.getName(), false, null, null, context.getPackageName());
97-
98-
boolean hasServices = hasPushFirebaseMessagingService &&
99-
hasRegistrationService;
100-
101-
if (hasPushReceiver && hasServices) {
102-
Log.i("Firebase Messaging is setup correctly.");
103-
return true;
104-
}
105-
} catch (Throwable t) {
106-
Log.exception(t);
107-
}
108-
Log.e("Failed to setup Firebase Messaging, check your manifest configuration.");
109-
return false;
110-
}
111-
11271
@Override
11372
public void unregister() {
11473
try {
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2020, Leanplum, Inc. All rights reserved.
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
package com.leanplum;
23+
24+
import android.content.Context;
25+
import android.os.Bundle;
26+
import com.google.firebase.messaging.FirebaseMessagingService;
27+
import com.google.firebase.messaging.RemoteMessage;
28+
import com.leanplum.internal.Constants;
29+
import com.leanplum.internal.Log;
30+
import java.util.Map;
31+
32+
/**
33+
* This class encapsulates functionality for handling data messages from FCM. Needs to be called
34+
* from your instance of {@link FirebaseMessagingService}.
35+
*/
36+
public final class LeanplumFirebaseServiceHandler {
37+
38+
/**
39+
* Call from your implementation of {@link FirebaseMessagingService#onCreate()}
40+
*/
41+
public void onCreate(Context context) {
42+
Leanplum.setApplicationContext(context);
43+
}
44+
45+
/**
46+
* Call from your implementation of {@link FirebaseMessagingService#onNewToken(String)}
47+
*/
48+
public void onNewToken(String token, Context context) {
49+
LeanplumPushService.setCloudMessagingProvider(new LeanplumFcmProvider());
50+
LeanplumPushService.getCloudMessagingProvider().storePreferences(context, token);
51+
52+
//send the new token to backend
53+
LeanplumPushService.getCloudMessagingProvider().onRegistrationIdReceived(context, token);
54+
}
55+
56+
/**
57+
* Call from your implementation of
58+
* {@link FirebaseMessagingService#onMessageReceived(RemoteMessage)}
59+
*/
60+
public void onMessageReceived(RemoteMessage remoteMessage, Context context) {
61+
try {
62+
Map<String, String> messageMap = remoteMessage.getData();
63+
if (messageMap.containsKey(Constants.Keys.PUSH_MESSAGE_TEXT)) {
64+
LeanplumPushService.handleNotification(context, getBundle(messageMap));
65+
}
66+
Log.d("Received push notification message: %s", messageMap.toString());
67+
} catch (Throwable t) {
68+
Log.exception(t);
69+
}
70+
}
71+
72+
/**
73+
* @param messageMap {@link RemoteMessage}'s data map.
74+
*/
75+
private Bundle getBundle(Map<String, String> messageMap) {
76+
Bundle bundle = new Bundle();
77+
if (messageMap != null) {
78+
for (Map.Entry<String, String> entry : messageMap.entrySet()) {
79+
bundle.putString(entry.getKey(), entry.getValue());
80+
}
81+
}
82+
return bundle;
83+
}
84+
}
Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016, Leanplum, Inc. All rights reserved.
2+
* Copyright 2020, Leanplum, Inc. All rights reserved.
33
*
44
* Licensed to the Apache Software Foundation (ASF) under one
55
* or more contributor license agreements. See the NOTICE file
@@ -21,41 +21,29 @@
2121

2222
package com.leanplum;
2323

24-
import android.annotation.SuppressLint;
25-
import android.os.Build;
26-
import android.os.Bundle;
27-
24+
import androidx.annotation.NonNull;
2825
import com.google.firebase.messaging.FirebaseMessagingService;
2926
import com.google.firebase.messaging.RemoteMessage;
30-
import com.leanplum.internal.Constants;
31-
import com.leanplum.internal.Log;
32-
import com.leanplum.internal.Util;
33-
34-
import java.util.Map;
3527

3628
/**
3729
* FCM listener service, which enables handling messages on the app's behalf.
3830
*
3931
* @author Anna Orlova
4032
*/
41-
@SuppressLint("Registered")
4233
public class LeanplumPushFirebaseMessagingService extends FirebaseMessagingService {
4334

35+
private final LeanplumFirebaseServiceHandler handler = new LeanplumFirebaseServiceHandler();
36+
4437
@Override
4538
public void onCreate() {
4639
super.onCreate();
47-
Leanplum.setApplicationContext(getApplicationContext());
40+
handler.onCreate(getApplicationContext());
4841
}
4942

5043
@Override
51-
public void onNewToken(String token) {
44+
public void onNewToken(@NonNull String token) {
5245
super.onNewToken(token);
53-
54-
LeanplumPushService.setCloudMessagingProvider(new LeanplumFcmProvider());
55-
LeanplumPushService.getCloudMessagingProvider().storePreferences(getApplicationContext(), token);
56-
57-
//send the new token to backend
58-
LeanplumPushService.getCloudMessagingProvider().onRegistrationIdReceived(getApplicationContext(), token);
46+
handler.onNewToken(token, getApplicationContext());
5947
}
6048

6149
/**
@@ -65,28 +53,7 @@ public void onNewToken(String token) {
6553
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
6654
*/
6755
@Override
68-
public void onMessageReceived(RemoteMessage remoteMessage) {
69-
try {
70-
Map<String, String> messageMap = remoteMessage.getData();
71-
if (messageMap.containsKey(Constants.Keys.PUSH_MESSAGE_TEXT)) {
72-
LeanplumPushService.handleNotification(getApplicationContext(), getBundle(messageMap));
73-
}
74-
Log.d("Received push notification message: %s", messageMap.toString());
75-
} catch (Throwable t) {
76-
Log.exception(t);
77-
}
78-
}
79-
80-
/**
81-
* @param messageMap {@link RemoteMessage}'s data map.
82-
*/
83-
private Bundle getBundle(Map<String, String> messageMap) {
84-
Bundle bundle = new Bundle();
85-
if (messageMap != null) {
86-
for (Map.Entry<String, String> entry : messageMap.entrySet()) {
87-
bundle.putString(entry.getKey(), entry.getValue());
88-
}
89-
}
90-
return bundle;
56+
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
57+
handler.onMessageReceived(remoteMessage, getApplicationContext());
9158
}
9259
}

AndroidSDKPush/src/main/java/com/leanplum/LeanplumCloudMessagingProvider.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016, Leanplum, Inc. All rights reserved.
2+
* Copyright 2020, Leanplum, Inc. All rights reserved.
33
*
44
* Licensed to the Apache Software Foundation (ASF) under one
55
* or more contributor license agreements. See the NOTICE file
@@ -73,13 +73,6 @@ private static void sendRegistrationIdToBackend(String registrationId) {
7373
*/
7474
public abstract boolean isInitialized();
7575

76-
/**
77-
* Whether app manifest is setup correctly.
78-
*
79-
* @return True if manifest is setup, false otherwise.
80-
*/
81-
public abstract boolean isManifestSetup();
82-
8376
/**
8477
* Unregister from cloud messaging.
8578
*/

AndroidSDKPush/src/main/java/com/leanplum/LeanplumManualProvider.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@ public boolean isInitialized() {
4848
return true;
4949
}
5050

51-
@Override
52-
public boolean isManifestSetup() {
53-
return true;
54-
}
55-
5651
@Override
5752
public void unregister() {
5853
}

AndroidSDKPush/src/main/java/com/leanplum/LeanplumPushService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014, Leanplum, Inc. All rights reserved.
2+
* Copyright 2020, Leanplum, Inc. All rights reserved.
33
*
44
* Licensed to the Apache Software Foundation (ASF) under one
55
* or more contributor license agreements. See the NOTICE file
@@ -684,7 +684,7 @@ static void onStart() {
684684
* Initialize push service.
685685
*/
686686
static void initPushService() {
687-
if (!provider.isInitialized() || !provider.isManifestSetup()) {
687+
if (!provider.isInitialized()) {
688688
return;
689689
}
690690
if (hasAppIDChanged(APIConfig.getInstance().appId())) {

0 commit comments

Comments
 (0)