11package org .apache .cordova .firebase ;
22
3+ import android .content .ComponentName ;
34import android .content .Context ;
45import android .content .Intent ;
56import android .content .SharedPreferences ;
2526import org .apache .cordova .CallbackContext ;
2627import org .apache .cordova .CordovaPlugin ;
2728import org .apache .cordova .PluginResult ;
29+ import org .apache .cordova .CordovaWebView ;
2830import org .json .JSONArray ;
2931import org .json .JSONException ;
3032import org .json .JSONObject ;
31-
3233import java .util .ArrayList ;
3334import java .util .HashMap ;
3435import java .util .Iterator ;
4950public 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
0 commit comments