Skip to content

Commit

Permalink
Merge pull request arnesson#222 from Taracque/master
Browse files Browse the repository at this point in the history
Relaunch Cordova app if not running
  • Loading branch information
soumak77 authored May 2, 2018
2 parents 4a2584f + 7d3e7df commit 6552f41
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
32 changes: 27 additions & 5 deletions src/android/FirebasePlugin.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.apache.cordova.firebase;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
Expand All @@ -25,10 +26,10 @@
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.apache.cordova.CordovaWebView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
Expand All @@ -49,6 +50,7 @@
public class FirebasePlugin extends CordovaPlugin {

private FirebaseAnalytics mFirebaseAnalytics;
private static CordovaWebView appView;
private final String TAG = "FirebasePlugin";
protected static final String KEY = "badge";

Expand Down Expand Up @@ -198,11 +200,21 @@ public void onReset() {
FirebasePlugin.tokenRefreshCallbackContext = null;
}

@Override
public void onDestroy() {
super.onDestroy();
System.exit(0);

if (this.appView != null) {
appView.handleDestroy();
}
}

private void onNotificationOpen(final CallbackContext callbackContext) {
FirebasePlugin.notificationCallbackContext = callbackContext;
if (FirebasePlugin.notificationStack != null) {
for (Bundle bundle : FirebasePlugin.notificationStack) {
FirebasePlugin.sendNotification(bundle);
FirebasePlugin.sendNotification(bundle,this.cordova.getActivity().getApplicationContext());
}
FirebasePlugin.notificationStack.clear();
}
Expand All @@ -226,12 +238,22 @@ public void run() {
});
}

public static void sendNotification(Bundle bundle) {
if (!FirebasePlugin.hasNotificationsCallback()) {
public static void sendNotification(Bundle bundle, Context context) {
if(!FirebasePlugin.hasNotificationsCallback()) {
String packageName = context.getPackageName();
if (FirebasePlugin.notificationStack == null) {
FirebasePlugin.notificationStack = new ArrayList<Bundle>();
}
notificationStack.add(bundle);

/* start the main activity, if not running */
Intent intent = new Intent("android.intent.action.MAIN");
intent.setComponent(new ComponentName(packageName, packageName + ".MainActivity"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("cdvStartInBackground", true);

context.startActivity(intent);

return;
}
final CallbackContext callbackContext = FirebasePlugin.notificationCallbackContext;
Expand Down Expand Up @@ -279,7 +301,7 @@ public void onNewIntent(Intent intent) {
final Bundle data = intent.getExtras();
if (data != null && data.containsKey("google.message_id")) {
data.putBoolean("tap", true);
FirebasePlugin.sendNotification(data);
FirebasePlugin.sendNotification(data, this.cordova.getActivity().getApplicationContext());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/android/FirebasePluginMessagingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private void sendNotification(String id, String title, String messageBody, Map<S
bundle.putBoolean("tap", false);
bundle.putString("title", title);
bundle.putString("body", messageBody);
FirebasePlugin.sendNotification(bundle);
FirebasePlugin.sendNotification(bundle, this.getApplicationContext());
}
}
}
2 changes: 1 addition & 1 deletion src/android/OnNotificationOpenReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void onReceive(Context context, Intent intent) {
launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle data = intent.getExtras();
data.putBoolean("tap", true);
FirebasePlugin.sendNotification(data);
FirebasePlugin.sendNotification(data, context);
launchIntent.putExtras(data);
context.startActivity(launchIntent);
}
Expand Down

0 comments on commit 6552f41

Please sign in to comment.