diff --git a/mobile/android/base/GeckoApp.java b/mobile/android/base/GeckoApp.java index 96543e69a293..57590c957143 100644 --- a/mobile/android/base/GeckoApp.java +++ b/mobile/android/base/GeckoApp.java @@ -1749,6 +1749,11 @@ abstract public class GeckoApp alertCookie = ""; } handleNotification(ACTION_ALERT_CALLBACK, alertName, alertCookie); + + if (intent.hasExtra(NotificationHelper.NOTIFICATION_ID)) { + String id = intent.getStringExtra(NotificationHelper.NOTIFICATION_ID); + mNotificationHelper.hideNotification(id); + } } @Override @@ -1991,6 +1996,8 @@ abstract public class GeckoApp if (mTextSelection != null) mTextSelection.destroy(); SiteIdentityPopup.clearInstance(); + if (mNotificationHelper != null) + mNotificationHelper.destroy(); Tabs.getInstance().detachFromActivity(this); diff --git a/mobile/android/base/NotificationHelper.java b/mobile/android/base/NotificationHelper.java index 0705a66490e9..5d55b577e926 100644 --- a/mobile/android/base/NotificationHelper.java +++ b/mobile/android/base/NotificationHelper.java @@ -21,12 +21,18 @@ import android.net.Uri; import android.support.v4.app.NotificationCompat; import android.util.Log; +import java.util.Set; +import java.util.HashSet; + public class NotificationHelper implements GeckoEventListener { + public static final String NOTIFICATION_ID = "NotificationHelper_ID"; private static final String LOGTAG = "GeckoNotificationManager"; - private Context mContext; + private Context mContext; + private Set mShowing; public NotificationHelper(Context context) { mContext = context; + mShowing = new HashSet(); registerEventListener("Notification:Show"); registerEventListener("Notification:Hide"); } @@ -48,7 +54,7 @@ public class NotificationHelper implements GeckoEventListener { NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext); // These attributes are required - String id; + final String id; try { builder.setContentTitle(message.getString("title")); builder.setContentText(message.getString("text")); @@ -91,12 +97,20 @@ public class NotificationHelper implements GeckoEventListener { String app = mContext.getClass().getName(); notificationIntent.setClassName(AppConstants.ANDROID_PACKAGE_NAME, app); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + // if this isn't an ongoing notification, add the id to the intent so that we + // can remove the notification from our list of active notifications if its clicked + if (!ongoing) { + notificationIntent.putExtra(NOTIFICATION_ID, id); + } PendingIntent pi = PendingIntent.getActivity(mContext, 0, notificationIntent, 0); builder.setContentIntent(pi); NotificationManager manager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(id.hashCode(), builder.build()); + if (!mShowing.contains(id)) { + mShowing.add(id); + } } private void hideNotification(JSONObject message) { @@ -107,7 +121,23 @@ public class NotificationHelper implements GeckoEventListener { Log.i(LOGTAG, "Error parsing", ex); return; } + + hideNotification(id); + } + + public void hideNotification(String id) { NotificationManager manager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); manager.cancel(id.hashCode()); + mShowing.remove(id); + } + + private void clearAll() { + for (String id : mShowing) { + hideNotification(id); + } + } + + public void destroy() { + clearAll(); } }