Browse Source

Callback function will be called in foreground

Sebastián Katzer 12 years ago
parent
commit
78a0e5818e
1 changed files with 33 additions and 1 deletions
  1. 33 1
      src/android/LocalNotificationReceiver.java

+ 33 - 1
src/android/LocalNotificationReceiver.java

@@ -90,9 +90,12 @@ public class LocalNotificationReceiver extends BroadcastReceiver {
 		.setSmallIcon(options.getIcon());
 
 		try {
-			if (!context.getPackageName().equalsIgnoreCase(((ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE)).getRunningTasks(1).get(0).topActivity.getPackageName())) {
+			if (isInBackground(context)) {
 				// app is in background
 				notification.setDefaults(Notification.DEFAULT_SOUND);
+			} else {
+				// exec foreground callback
+				invokeForegroundCallback(options);
 			}
 		} catch (Exception e) {
 			// missing GET_TASKS permission
@@ -107,4 +110,33 @@ public class LocalNotificationReceiver extends BroadcastReceiver {
 		 */
 		notificationMgr.notify(id, notification.build());
 	}
+
+	/**
+	 * Gibt an, ob die App im Hintergrund läuft.
+	 */
+	private boolean isInBackground (Context context) {
+		return !context.getPackageName().equalsIgnoreCase(((ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE)).getRunningTasks(1).get(0).topActivity.getPackageName());
+	}
+
+	/**
+	 * Ruft die `foreground` Callback Funktion auf.
+	 */
+	private void invokeForegroundCallback (LocalNotificationOptions options) {
+		String function = options.getForeground();
+
+		if (function != null) {
+			LocalNotification.webView.sendJavascript(function + "(" + options.getId() + ")");
+		}
+	}
+
+	/**
+	 * Ruft die `background` Callback Funktion auf.
+	 */
+	private void invokeBackgroundCallback (LocalNotificationOptions options) {
+		String function = options.getBackground();
+
+		if (function != null) {
+			LocalNotification.webView.sendJavascript(function + "(" + options.getId() + ")");
+		}
+	}
 }