Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restoring Firebase initialization on startup #830

Merged
merged 1 commit into from
Sep 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 8 additions & 50 deletions src/android/FirebasePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public class FirebasePlugin extends CordovaPlugin {
private final String ERRORINITPERFORMANCE = "Performance isn't initialised";
protected static final String KEY = "badge";

private static boolean firebaseInit = false;
private static boolean crashlyticsInit = false;
private static boolean analyticsInit = false;
private static boolean performanceInit = false;
Expand All @@ -77,10 +76,12 @@ public class FirebasePlugin extends CordovaPlugin {

@Override
protected void pluginInitialize() {
final Context context = this.cordova.getActivity().getApplicationContext();
final Bundle extras = this.cordova.getActivity().getIntent().getExtras();
this.cordova.getThreadPool().execute(new Runnable() {
public void run() {
Log.d(TAG, "Starting Firebase plugin");
FirebaseApp.initializeApp(context);
if (extras != null && extras.size() > 1) {
if (FirebasePlugin.notificationStack == null) {
FirebasePlugin.notificationStack = new ArrayList<Bundle>();
Expand All @@ -96,10 +97,7 @@ public void run() {

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals("initFirebase")) {
this.initFirebase(callbackContext);
return true;
} else if (action.equals("initCrashlytics")) {
if (action.equals("initCrashlytics")) {
this.initCrashlytics(callbackContext);
return true;
} else if (action.equals("initAnalytics")) {
Expand Down Expand Up @@ -244,22 +242,6 @@ public void onDestroy() {
}
}

private void initFirebase(final CallbackContext callbackContext) {
final Context context = this.cordova.getActivity().getApplicationContext();

Log.d(TAG, "Initialising Firebase");
try {
FirebaseApp.initializeApp(context);
FirebasePlugin.firebaseInit = true;
callbackContext.success();
} catch(Exception e) {
if(FirebasePlugin.crashlyticsInit()){
Crashlytics.logException(e);
}
callbackContext.error(ERRORINITFIREBASE);
}
}

private void initCrashlytics(final CallbackContext callbackContext) {
final Context context = this.cordova.getActivity().getApplicationContext();

Expand Down Expand Up @@ -322,13 +304,9 @@ private void onTokenRefresh(final CallbackContext callbackContext) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
try {
if(FirebasePlugin.firebaseInit()){
String currentToken = FirebaseInstanceId.getInstance().getToken();
if (currentToken != null) {
FirebasePlugin.sendToken(currentToken);
}
} else {
callbackContext.error(ERRORINITFIREBASE);
String currentToken = FirebaseInstanceId.getInstance().getToken();
if (currentToken != null) {
FirebasePlugin.sendToken(currentToken);
}
} catch (Exception e) {
if(FirebasePlugin.crashlyticsInit()){
Expand Down Expand Up @@ -389,10 +367,6 @@ public static boolean inBackground() {
return FirebasePlugin.inBackground;
}

public static boolean firebaseInit() {
return FirebasePlugin.firebaseInit;
}

public static boolean crashlyticsInit() {
return FirebasePlugin.crashlyticsInit;
}
Expand Down Expand Up @@ -424,12 +398,8 @@ private void getInstanceId(final CallbackContext callbackContext) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
try {
if(FirebasePlugin.firebaseInit()){
String token = FirebaseInstanceId.getInstance().getToken();
callbackContext.success(token);
} else {
callbackContext.error(ERRORINITFIREBASE);
}
String token = FirebaseInstanceId.getInstance().getToken();
callbackContext.success(token);
} catch (Exception e) {
callbackContext.error(e.getMessage());
}
Expand All @@ -441,12 +411,8 @@ private void getId(final CallbackContext callbackContext) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
try {
if(FirebasePlugin.firebaseInit()){
String id = FirebaseInstanceId.getInstance().getId();
callbackContext.success(id);
} else {
callbackContext.error(ERRORINITFIREBASE);
}
} catch (Exception e) {
if(FirebasePlugin.crashlyticsInit()){
Crashlytics.logException(e);
Expand All @@ -461,12 +427,8 @@ private void getToken(final CallbackContext callbackContext) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
try {
if(FirebasePlugin.firebaseInit()){
String token = FirebaseInstanceId.getInstance().getToken();
callbackContext.success(token);
} else {
callbackContext.error(ERRORINITFIREBASE);
}
} catch (Exception e) {
if(FirebasePlugin.crashlyticsInit()){
Crashlytics.logException(e);
Expand Down Expand Up @@ -571,12 +533,8 @@ private void unregister(final CallbackContext callbackContext) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
try {
if(FirebasePlugin.firebaseInit()){
FirebaseInstanceId.getInstance().deleteInstanceId();
callbackContext.success();
} else {
callbackContext.error(ERRORINITFIREBASE);
}
} catch (Exception e) {
if(FirebasePlugin.crashlyticsInit()){
Crashlytics.logException(e);
Expand Down
1 change: 0 additions & 1 deletion src/ios/FirebasePlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
@property (nonatomic, copy) NSString *tokenRefreshCallbackId;
@property (nonatomic, retain) NSMutableArray *notificationStack;
@property (nonatomic, readwrite) NSMutableDictionary* traces;
@property (atomic, assign) BOOL firebaseInit;
@property (atomic, assign) BOOL crashlyticsInit;
@property (atomic, assign) BOOL analyticsInit;
@property (atomic, assign) BOOL remoteconfigInit;
Expand Down
66 changes: 14 additions & 52 deletions src/ios/FirebasePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ @implementation FirebasePlugin
@synthesize notificationStack;
@synthesize traces;

@synthesize firebaseInit;
@synthesize crashlyticsInit;
@synthesize analyticsInit;
@synthesize performanceInit;
Expand All @@ -45,28 +44,11 @@ + (FirebasePlugin *) firebasePlugin {
- (void)pluginInitialize {
NSLog(@"Starting Firebase plugin");
firebasePlugin = self;
self.firebaseInit = NO;
self.crashlyticsInit = NO;
self.analyticsInit = NO;
self.performanceInit = NO;
}

- (void)initFirebase:(CDVInvokedUrlCommand *)command {
__block CDVPluginResult *pluginResult;
if ([FIRApp defaultApp] == nil) {
[FIRApp configure];
}

if ([FIRApp defaultApp] == nil) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
} else {
self.firebaseInit = YES;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
}

[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void)initCrashlytics:(CDVInvokedUrlCommand *)command {
__block CDVPluginResult *pluginResult;
[Fabric with:@[[Crashlytics class]]];
Expand Down Expand Up @@ -133,22 +115,12 @@ - (void)getId:(CDVInvokedUrlCommand *)command {

// DEPRECATED - alias of getToken
- (void)getInstanceId:(CDVInvokedUrlCommand *)command {
CDVPluginResult *pluginResult;
if(self.firebaseInit){
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[[FIRInstanceID instanceID] token]];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:ERRORINITFIREBASE];
}
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[[FIRInstanceID instanceID] token]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void)getToken:(CDVInvokedUrlCommand *)command {
CDVPluginResult *pluginResult;
if(self.firebaseInit){
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[[FIRInstanceID instanceID] token]];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:ERRORINITFIREBASE];
}
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[[FIRInstanceID instanceID] token]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

Expand Down Expand Up @@ -294,19 +266,14 @@ - (void)unsubscribe:(CDVInvokedUrlCommand *)command {
}

- (void)unregister:(CDVInvokedUrlCommand *)command {
__block CDVPluginResult *pluginResult;
if(self.firebaseInit){
[[FIRInstanceID instanceID] deleteIDWithHandler:^void(NSError *_Nullable error) {
if (error) {
NSLog(@"Unable to delete instance");
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
}
}];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:ERRORINITFIREBASE];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
[[FIRInstanceID instanceID] deleteIDWithHandler:^void(NSError *_Nullable error) {
if (error) {
NSLog(@"Unable to delete instance");
} else {
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
}];
}

- (void)onNotificationOpen:(CDVInvokedUrlCommand *)command {
Expand All @@ -321,16 +288,11 @@ - (void)onNotificationOpen:(CDVInvokedUrlCommand *)command {
}

- (void)onTokenRefresh:(CDVInvokedUrlCommand *)command {
if(self.firebaseInit){
self.tokenRefreshCallbackId = command.callbackId;
NSString* currentToken = [[FIRInstanceID instanceID] token];
self.tokenRefreshCallbackId = command.callbackId;
NSString* currentToken = [[FIRInstanceID instanceID] token];

if (currentToken != nil) {
[self sendToken:currentToken];
}
} else {
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:ERRORINITFIREBASE];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
if (currentToken != nil) {
[self sendToken:currentToken];
}
}

Expand Down
4 changes: 0 additions & 4 deletions www/firebase.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
var exec = require('cordova/exec');

exports.initFirebase = function (success, error) {
exec(success, error, "FirebasePlugin", "initFirebase", []);
};

exports.initCrashlytics = function (success, error) {
exec(success, error, "FirebasePlugin", "initCrashlytics", []);
};
Expand Down