Skip to content

Commit

Permalink
Convert iid to gms tasks (#2150)
Browse files Browse the repository at this point in the history
[skip ci]
  • Loading branch information
Ehesp authored May 21, 2019
1 parent 35f6f8d commit b293373
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 36 deletions.
5 changes: 5 additions & 0 deletions packages/iid/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ android {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
sourceSets {
main {
java.srcDirs = ['src/main/java', 'src/reactnative/java']
}
}
}

repositories {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package io.invertase.firebase.iid;

/*
* Copyright (c) 2016-present Invertase Limited & Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this library except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

import android.content.Context;

import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.Tasks;
import com.google.firebase.FirebaseApp;
import com.google.firebase.iid.FirebaseInstanceId;

import io.invertase.firebase.common.UniversalFirebaseModule;

public class UniversalFirebaseIidModule extends UniversalFirebaseModule {

UniversalFirebaseIidModule(Context context, String serviceName) {
super(context, serviceName);
}

Task<String> get(String appName) {
return Tasks.call(() -> {
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
return FirebaseInstanceId.getInstance(firebaseApp).getId();
});
}

Task<String> getToken(String appName, String authorizedEntity, String scope) {
return Tasks.call(() -> {
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
return FirebaseInstanceId.getInstance(firebaseApp).getToken(authorizedEntity, scope);
});
}

Task<Void> delete(String appName) {
return Tasks.call(() -> {
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
FirebaseInstanceId.getInstance(firebaseApp).deleteInstanceId();
return null;
});
}

Task<Void> deleteToken(String appName, String authorizedEntity, String scope) {
return Tasks.call(() -> {
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
FirebaseInstanceId.getInstance(firebaseApp).deleteToken(authorizedEntity, scope);
return null;
});
}
}
2 changes: 2 additions & 0 deletions packages/iid/android/src/reactnative/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="io.invertase.firebase.iid" />
Original file line number Diff line number Diff line change
Expand Up @@ -20,63 +20,59 @@
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactMethod;
import com.google.firebase.FirebaseApp;
import com.google.firebase.iid.FirebaseInstanceId;

import io.invertase.firebase.common.ReactNativeFirebaseModule;

public class ReactNativeFirebaseIidModule extends ReactNativeFirebaseModule {
private static final String TAG = "Iid";
private static final String SERVICE_NAME = "Iid";
private final UniversalFirebaseIidModule module;

ReactNativeFirebaseIidModule(ReactApplicationContext reactContext) {
super(reactContext, TAG);
super(reactContext, SERVICE_NAME);
module = new UniversalFirebaseIidModule(reactContext, SERVICE_NAME);
}

@ReactMethod
public void get(String appName, Promise promise) {
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);

try {
String id = FirebaseInstanceId.getInstance(firebaseApp).getId();
promise.resolve(id);
} catch (Exception exception) {
rejectPromiseWithExceptionMap(promise, exception);
}
module.get(appName).addOnCompleteListener(task -> {
if (task.isSuccessful()) {
promise.resolve(task.getResult());
} else {
rejectPromiseWithExceptionMap(promise, task.getException());
}
});
}

@ReactMethod
public void getToken(String appName, String authorizedEntity, String scope, Promise promise) {
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);

try {
String token = FirebaseInstanceId.getInstance(firebaseApp).getToken(authorizedEntity, scope);
promise.resolve(token);
} catch (Exception exception) {
rejectPromiseWithExceptionMap(promise, exception);
}
module.getToken(appName, authorizedEntity, scope).addOnCompleteListener(task -> {
if (task.isSuccessful()) {
promise.resolve(task.getResult());
} else {
rejectPromiseWithExceptionMap(promise, task.getException());
}
});
}

@ReactMethod
public void delete(String appName, Promise promise) {
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);

try {
FirebaseInstanceId.getInstance(firebaseApp).deleteInstanceId();
promise.resolve(null);
} catch (Exception exception) {
rejectPromiseWithExceptionMap(promise, exception);
}
module.delete(appName).addOnCompleteListener(task -> {
if (task.isSuccessful()) {
promise.resolve(task.getResult());
} else {
rejectPromiseWithExceptionMap(promise, task.getException());
}
});
}

@ReactMethod
public void deleteToken(String appName, String authorizedEntity, String scope, Promise promise) {
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);

try {
FirebaseInstanceId.getInstance(firebaseApp).deleteToken(authorizedEntity, scope);
promise.resolve(null);
} catch (Exception exception) {
rejectPromiseWithExceptionMap(promise, exception);
}
module.deleteToken(appName, authorizedEntity, scope).addOnCompleteListener(task -> {
if (task.isSuccessful()) {
promise.resolve(task.getResult());
} else {
rejectPromiseWithExceptionMap(promise, task.getException());
}
});
}
}

0 comments on commit b293373

Please sign in to comment.