Skip to content

Commit

Permalink
(Android): Remove/replace previous deprecations which have been remov…
Browse files Browse the repository at this point in the history
…ed in latest Android Firebase SDK major versions
  • Loading branch information
dpa99c committed Jun 16, 2021
1 parent 21b0b65 commit 7aa1b92
Showing 1 changed file with 43 additions and 25 deletions.
68 changes: 43 additions & 25 deletions src/android/FirebasePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
import com.google.firebase.functions.FirebaseFunctions;
import com.google.firebase.functions.FirebaseFunctionsException;
import com.google.firebase.functions.HttpsCallableResult;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.installations.FirebaseInstallations;
import com.google.firebase.installations.InstallationTokenResult;
import com.google.firebase.messaging.FirebaseMessaging;
Expand Down Expand Up @@ -221,9 +220,9 @@ public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContex
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
try{
if (action.equals("getId")) {
this.getId(callbackContext);
this.getInstallationId(args, callbackContext);
} else if (action.equals("getToken")) {
this.getToken(callbackContext);
this.getToken(args, callbackContext);
} else if (action.equals("hasPermission")) {
this.hasPermission(callbackContext);
}else if (action.equals("subscribe")) {
Expand Down Expand Up @@ -494,10 +493,25 @@ private void onTokenRefresh(final CallbackContext callbackContext) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
try {
String currentToken = FirebaseInstanceId.getInstance().getToken();
if (currentToken != null) {
FirebasePlugin.sendToken(currentToken);
}
FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener<String>() {
@Override
public void onComplete(@NonNull Task<String> task) {
try {
if (task.isSuccessful() || task.getException() == null) {
String currentToken = task.getResult();
if (currentToken != null) {
FirebasePlugin.sendToken(currentToken);
}
}else if(task.getException() != null){
callbackContext.error(task.getException().getMessage());
}else{
callbackContext.error("Task failed for unknown reason");
}
} catch (Exception e) {
handleExceptionWithContext(e, callbackContext);
}
};
});
} catch (Exception e) {
handleExceptionWithContext(e, callbackContext);
}
Expand Down Expand Up @@ -574,25 +588,28 @@ public void onNewIntent(Intent intent) {
}


private void getId(final CallbackContext callbackContext) {
private void getToken(JSONArray args, final CallbackContext callbackContext) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
try {
String id = FirebaseInstanceId.getInstance().getId();
callbackContext.success(id);
} catch (Exception e) {
handleExceptionWithContext(e, callbackContext);
}
}
});
}
FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener<String>() {
@Override
public void onComplete(@NonNull Task<String> task) {
try {
if (task.isSuccessful() || task.getException() == null) {
String currentToken = task.getResult();
callbackContext.success(currentToken);
}else if(task.getException() != null){
callbackContext.error(task.getException().getMessage());
}else{
callbackContext.error("Task failed for unknown reason");
}
} catch (Exception e) {
handleExceptionWithContext(e, callbackContext);
}
};
});

private void getToken(final CallbackContext callbackContext) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
try {
String token = FirebaseInstanceId.getInstance().getToken();
callbackContext.success(token);
} catch (Exception e) {
handleExceptionWithContext(e, callbackContext);
}
Expand Down Expand Up @@ -643,8 +660,7 @@ private void unregister(final CallbackContext callbackContext) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
try {
FirebaseInstanceId.getInstance().deleteInstanceId();
callbackContext.success();
handleTaskOutcome(FirebaseMessaging.getInstance().deleteToken(), callbackContext);
} catch (Exception e) {
handleExceptionWithContext(e, callbackContext);
}
Expand Down Expand Up @@ -853,7 +869,9 @@ private void setScreenName(final CallbackContext callbackContext, final String n
cordovaActivity.runOnUiThread(new Runnable() {
public void run() {
try {
mFirebaseAnalytics.setCurrentScreen(cordovaActivity, name, null);
Bundle bundle = new Bundle();
bundle.putString(FirebaseAnalytics.Param.SCREEN_NAME, name);
mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SCREEN_VIEW, bundle);
callbackContext.success();
} catch (Exception e) {
handleExceptionWithContext(e, callbackContext);
Expand Down

0 comments on commit 7aa1b92

Please sign in to comment.