Skip to content

Commit 6552f41

Browse files
authored
Merge pull request #222 from Taracque/master
Relaunch Cordova app if not running
2 parents 4a2584f + 7d3e7df commit 6552f41

3 files changed

Lines changed: 29 additions & 7 deletions

File tree

src/android/FirebasePlugin.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.apache.cordova.firebase;
22

3+
import android.content.ComponentName;
34
import android.content.Context;
45
import android.content.Intent;
56
import android.content.SharedPreferences;
@@ -25,10 +26,10 @@
2526
import org.apache.cordova.CallbackContext;
2627
import org.apache.cordova.CordovaPlugin;
2728
import org.apache.cordova.PluginResult;
29+
import org.apache.cordova.CordovaWebView;
2830
import org.json.JSONArray;
2931
import org.json.JSONException;
3032
import org.json.JSONObject;
31-
3233
import java.util.ArrayList;
3334
import java.util.HashMap;
3435
import java.util.Iterator;
@@ -49,6 +50,7 @@
4950
public class FirebasePlugin extends CordovaPlugin {
5051

5152
private FirebaseAnalytics mFirebaseAnalytics;
53+
private static CordovaWebView appView;
5254
private final String TAG = "FirebasePlugin";
5355
protected static final String KEY = "badge";
5456

@@ -198,11 +200,21 @@ public void onReset() {
198200
FirebasePlugin.tokenRefreshCallbackContext = null;
199201
}
200202

203+
@Override
204+
public void onDestroy() {
205+
super.onDestroy();
206+
System.exit(0);
207+
208+
if (this.appView != null) {
209+
appView.handleDestroy();
210+
}
211+
}
212+
201213
private void onNotificationOpen(final CallbackContext callbackContext) {
202214
FirebasePlugin.notificationCallbackContext = callbackContext;
203215
if (FirebasePlugin.notificationStack != null) {
204216
for (Bundle bundle : FirebasePlugin.notificationStack) {
205-
FirebasePlugin.sendNotification(bundle);
217+
FirebasePlugin.sendNotification(bundle,this.cordova.getActivity().getApplicationContext());
206218
}
207219
FirebasePlugin.notificationStack.clear();
208220
}
@@ -226,12 +238,22 @@ public void run() {
226238
});
227239
}
228240

229-
public static void sendNotification(Bundle bundle) {
230-
if (!FirebasePlugin.hasNotificationsCallback()) {
241+
public static void sendNotification(Bundle bundle, Context context) {
242+
if(!FirebasePlugin.hasNotificationsCallback()) {
243+
String packageName = context.getPackageName();
231244
if (FirebasePlugin.notificationStack == null) {
232245
FirebasePlugin.notificationStack = new ArrayList<Bundle>();
233246
}
234247
notificationStack.add(bundle);
248+
249+
/* start the main activity, if not running */
250+
Intent intent = new Intent("android.intent.action.MAIN");
251+
intent.setComponent(new ComponentName(packageName, packageName + ".MainActivity"));
252+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
253+
intent.putExtra("cdvStartInBackground", true);
254+
255+
context.startActivity(intent);
256+
235257
return;
236258
}
237259
final CallbackContext callbackContext = FirebasePlugin.notificationCallbackContext;
@@ -279,7 +301,7 @@ public void onNewIntent(Intent intent) {
279301
final Bundle data = intent.getExtras();
280302
if (data != null && data.containsKey("google.message_id")) {
281303
data.putBoolean("tap", true);
282-
FirebasePlugin.sendNotification(data);
304+
FirebasePlugin.sendNotification(data, this.cordova.getActivity().getApplicationContext());
283305
}
284306
}
285307

src/android/FirebasePluginMessagingService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ private void sendNotification(String id, String title, String messageBody, Map<S
184184
bundle.putBoolean("tap", false);
185185
bundle.putString("title", title);
186186
bundle.putString("body", messageBody);
187-
FirebasePlugin.sendNotification(bundle);
187+
FirebasePlugin.sendNotification(bundle, this.getApplicationContext());
188188
}
189189
}
190190
}

src/android/OnNotificationOpenReceiver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void onReceive(Context context, Intent intent) {
1717
launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
1818
Bundle data = intent.getExtras();
1919
data.putBoolean("tap", true);
20-
FirebasePlugin.sendNotification(data);
20+
FirebasePlugin.sendNotification(data, context);
2121
launchIntent.putExtras(data);
2222
context.startActivity(launchIntent);
2323
}

0 commit comments

Comments
 (0)