Skip to content

Commit d7b3648

Browse files
authored
Add support for newer fcm methods (#475)
1 parent 30595d7 commit d7b3648

File tree

2 files changed

+102
-32
lines changed

2 files changed

+102
-32
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2021, 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.text.TextUtils
25+
import com.google.firebase.iid.FirebaseInstanceId
26+
import com.google.firebase.messaging.FirebaseMessaging
27+
import com.leanplum.internal.Log
28+
29+
internal fun updateRegistrationId(provider: LeanplumCloudMessagingProvider) {
30+
try {
31+
Present.updateRegistrationId(provider)
32+
} catch (e: NoSuchMethodError) {
33+
Log.d("Using legacy firebase methods.")
34+
Legacy.updateRegistrationId(provider)
35+
}
36+
}
37+
38+
internal fun unregister() {
39+
try {
40+
Present.unregister()
41+
} catch (e: NoSuchMethodError) {
42+
Log.d("Using legacy firebase methods.")
43+
Legacy.unregister()
44+
}
45+
}
46+
47+
/**
48+
* Present Firebase interface was added in version 20.3.0.
49+
*/
50+
private object Present {
51+
fun updateRegistrationId(provider: LeanplumCloudMessagingProvider) {
52+
FirebaseMessaging.getInstance().token.addOnCompleteListener {
53+
if (it.isSuccessful) {
54+
val token = it.result.toString()
55+
if (!TextUtils.isEmpty(token)) {
56+
provider.registrationId = token
57+
}
58+
} else {
59+
Log.e("getToken failed:\n" + Log.getStackTraceString(it.exception))
60+
}
61+
}
62+
}
63+
64+
fun unregister() {
65+
try {
66+
FirebaseMessaging.getInstance().deleteToken()
67+
Log.i("Application was unregistered from FirebaseMessaging.")
68+
} catch (e: Exception) {
69+
Log.e("Failed to unregister from FirebaseMessaging.")
70+
}
71+
}
72+
}
73+
74+
/**
75+
* Legacy Firebase interface was removed in version 22.0.0.
76+
*/
77+
private object Legacy {
78+
fun updateRegistrationId(provider: LeanplumCloudMessagingProvider) {
79+
FirebaseInstanceId.getInstance().instanceId.addOnCompleteListener {
80+
if (it.isSuccessful) {
81+
val tokenId = it.result?.token
82+
if (!TextUtils.isEmpty(tokenId)) {
83+
provider.registrationId = tokenId
84+
}
85+
} else {
86+
Log.e("getInstanceId failed:\n" + Log.getStackTraceString(it.exception))
87+
}
88+
}
89+
}
90+
91+
fun unregister() {
92+
try {
93+
FirebaseInstanceId.getInstance().deleteInstanceId()
94+
Log.i("Application was unregistered from FCM.")
95+
} catch (e: Exception) {
96+
Log.e("Failed to unregister from FCM.")
97+
}
98+
}
99+
}

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

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020, Leanplum, Inc. All rights reserved.
2+
* Copyright 2021, 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,16 +21,7 @@
2121

2222
package com.leanplum;
2323

24-
import android.text.TextUtils;
25-
26-
import com.google.android.gms.tasks.OnCompleteListener;
27-
import com.google.android.gms.tasks.Task;
28-
import com.google.firebase.iid.FirebaseInstanceId;
29-
import com.google.firebase.iid.InstanceIdResult;
3024
import com.leanplum.internal.Constants;
31-
import com.leanplum.internal.Log;
32-
33-
import androidx.annotation.NonNull;
3425

3526
/**
3627
* Leanplum provider for work with Firebase.
@@ -58,31 +49,11 @@ public PushProviderType getType() {
5849

5950
@Override
6051
public void updateRegistrationId() {
61-
FirebaseInstanceId.getInstance().getInstanceId()
62-
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
63-
@Override
64-
public void onComplete(@NonNull Task<InstanceIdResult> task) {
65-
if (!task.isSuccessful()) {
66-
Exception exc = task.getException();
67-
Log.e("getInstanceId failed:\n" + Log.getStackTraceString(exc));
68-
return;
69-
}
70-
// Get new Instance ID token
71-
String tokenId = task.getResult().getToken();
72-
if (!TextUtils.isEmpty(tokenId)) {
73-
setRegistrationId(tokenId);
74-
}
75-
}
76-
});
52+
FirebaseUtilKt.updateRegistrationId(this);
7753
}
7854

7955
@Override
8056
public void unregister() {
81-
try {
82-
FirebaseInstanceId.getInstance().deleteInstanceId();
83-
Log.i("Application was unregistered from FCM.");
84-
} catch (Exception e) {
85-
Log.e("Failed to unregister from FCM.");
86-
}
57+
FirebaseUtilKt.unregister();
8758
}
8859
}

0 commit comments

Comments
 (0)