Quellcode durchsuchen

Get the `background` callback on Android to work!

Sebastián Katzer vor 12 Jahren
Ursprung
Commit
bd95db8f6f
3 geänderte Dateien mit 75 neuen und 16 gelöschten Zeilen
  1. 8 0
      plugin.xml
  2. 4 16
      src/android/Receiver.java
  3. 63 0
      src/android/ReceiverActivity.java

+ 8 - 0
plugin.xml

@@ -63,6 +63,13 @@
                     <action android:name="android.intent.action.BOOT_COMPLETED" />
                 </intent-filter>
             </receiver>
+
+            <!--
+             * The receiver activity is triggered when a notification is clicked by a user.
+             * The activity calls the background callback and brings the launch inten
+             * up to foreground.
+            -->
+            <activity android:name="de.appplant.cordova.plugin.localnotification.ReceiverActivity"></activity>
         </config-file>
 
         <config-file target="AndroidManifest.xml" parent="/manifest">
@@ -74,6 +81,7 @@
         <source-file src="src/android/Receiver.java"          target-dir="src/de/appplant/cordova/plugin/localnotification" />
         <source-file src="src/android/Options.java"           target-dir="src/de/appplant/cordova/plugin/localnotification" />
         <source-file src="src/android/Restore.java"           target-dir="src/de/appplant/cordova/plugin/localnotification" />
+        <source-file src="src/android/ReceiverActivity.java"  target-dir="src/de/appplant/cordova/plugin/localnotification" />
     </platform>
 
     <!-- wp8 -->

+ 4 - 16
src/android/Receiver.java

@@ -115,9 +115,10 @@ public class Receiver extends BroadcastReceiver {
      * Fügt der Notification einen onclick Handler hinzu.
      */
     private Builder setClickEvent (Builder notification) {
-        String packageName          = context.getPackageName();
-        Intent launchIntent         = context.getPackageManager().getLaunchIntentForPackage(packageName);
-        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
+        Intent intent = new Intent(context, ReceiverActivity.class)
+            .putExtra(OPTIONS, options.getJSONObject().toString());
+
+        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
 
         return notification.setContentIntent(contentIntent);
     }
@@ -163,17 +164,4 @@ public class Receiver extends BroadcastReceiver {
                     LocalNotification.webView.sendJavascript(function + "(" + options.getId() + ")");
             }
     }
-
-    /**
-     * Ruft die `background` Callback Funktion auf.
-     */
-    private void invokeBackgroundCallback () {
-            String function = options.getBackground();
-
-            // after reboot, LocalNotification.webView is always null
-            // may be call background callback later
-            if (function != null && LocalNotification.webView != null) {
-                    LocalNotification.webView.sendJavascript(function + "(" + options.getId() + ")");
-            }
-    }
 }

+ 63 - 0
src/android/ReceiverActivity.java

@@ -0,0 +1,63 @@
+/**
+ *  Cordova LocalNotification Plugin
+ *
+ *  Created by Sebastian Katzer (github.com/katzer).
+ *  Copyright 2013 Sebastian Katzer. All rights reserved.
+ *  LGPL v2.1 licensed
+ */
+
+package de.appplant.cordova.plugin.localnotification;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import android.app.Activity;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+
+public class ReceiverActivity extends Activity {
+
+    /** Called when the activity is first created. */
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        Intent intent = this.getIntent();
+        Bundle bundle = intent.getExtras();
+
+        try {
+            JSONObject args = new JSONObject(bundle.getString(Receiver.OPTIONS));
+            Options options = new Options(getApplicationContext()).parse(args);
+
+            invokeBackgroundCallback(options);
+            launchMainIntent();
+        } catch (JSONException e) {}
+    }
+
+    /**
+     * Launch main intent for package.
+     */
+    private void launchMainIntent () {
+        Context context     = getApplicationContext();
+        String packageName  = context.getPackageName();
+        Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
+
+        launchIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
+
+        context.startActivity(launchIntent);
+    }
+
+    /**
+     * Ruft die `background` Callback Funktion auf.
+     */
+    private void invokeBackgroundCallback (Options options) {
+        String function = options.getBackground();
+
+        // after reboot, LocalNotification.webView is always null
+        // may be call background callback later
+        if (function != null && LocalNotification.webView != null) {
+            LocalNotification.webView.sendJavascript(function + "(" + options.getId() + ")");
+        }
+    }
+}