From 58d51e5b41ff56c643e1b025c13b7752bc8a85a1 Mon Sep 17 00:00:00 2001 From: zmq Date: Tue, 1 Nov 2016 10:06:04 +0800 Subject: [PATCH 1/3] go to a specific window via the notification --- build.properties | 8 ++-- .../android/gcm/AppStateService.java | 31 ++++++++++++++ .../android/gcm/ForegroundCheckTask.java | 32 +++++++++++++++ .../android/gcm/GCMIntentService.java | 41 +++++++++++++++++-- src/nl/vanvianen/android/gcm/GCMModule.java | 11 ++++- 5 files changed, 115 insertions(+), 8 deletions(-) create mode 100644 src/nl/vanvianen/android/gcm/AppStateService.java create mode 100644 src/nl/vanvianen/android/gcm/ForegroundCheckTask.java mode change 100755 => 100644 src/nl/vanvianen/android/gcm/GCMIntentService.java diff --git a/build.properties b/build.properties index c196372..19c8e35 100644 --- a/build.properties +++ b/build.properties @@ -1,4 +1,4 @@ -titanium.platform=/Users/jeroen/Library/Application Support/Titanium/mobilesdk/osx/5.3.0.GA/android -android.platform=/Users/jeroen/Library/android-sdk-macosx/platforms/android-14 -google.apis=/Users/jeroen/Library/android-sdk-macosx/add-ons/addon-google_apis-google-15 -android.ndk=/Users/jeroen/Library/android-ndk-r12b +titanium.platform=/Users/zmq/Library/Application Support/Titanium/mobilesdk/osx/5.1.2.GA/android +android.platform=/Users/zmq/Library/Android/sdk/platforms/android-23 +google.apis=/Users/zmq/Library/Android/sdk/add-ons/addon-google_apis-google-23 +android.ndk=/Users/zmq/Library/Android/android-ndk-r11 \ No newline at end of file diff --git a/src/nl/vanvianen/android/gcm/AppStateService.java b/src/nl/vanvianen/android/gcm/AppStateService.java new file mode 100644 index 0000000..6da86d4 --- /dev/null +++ b/src/nl/vanvianen/android/gcm/AppStateService.java @@ -0,0 +1,31 @@ +package nl.vanvianen.android.gcm; + +import java.util.List; +import java.util.concurrent.ExecutionException; + +import org.appcelerator.titanium.TiApplication; + +import android.app.Activity; +import android.app.ActivityManager; +import android.app.ActivityManager.RunningAppProcessInfo; +import android.app.Application; +import android.content.Context; + +public class AppStateService { + + public static boolean initAppStateService(Application app) + { + try { + boolean foreground = new ForegroundCheckTask().execute(app.getApplicationContext()).get(); + return foreground; + } catch (InterruptedException e) { + e.printStackTrace(); + return false; + } catch (ExecutionException e) { + e.printStackTrace(); + return false; + } + } + + +} diff --git a/src/nl/vanvianen/android/gcm/ForegroundCheckTask.java b/src/nl/vanvianen/android/gcm/ForegroundCheckTask.java new file mode 100644 index 0000000..038ebc1 --- /dev/null +++ b/src/nl/vanvianen/android/gcm/ForegroundCheckTask.java @@ -0,0 +1,32 @@ +package nl.vanvianen.android.gcm; + + +import java.util.List; + +import android.app.ActivityManager; +import android.app.ActivityManager.RunningAppProcessInfo; +import android.content.Context; +import android.os.AsyncTask; + +class ForegroundCheckTask extends AsyncTask { + @Override + protected Boolean doInBackground(Context... params) { + final Context context = params[0]; + return isAppOnForeground(context); + } + + private boolean isAppOnForeground(Context context) { + ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); + List appProcesses = (List) activityManager.getRunningAppProcesses(); + if (appProcesses == null) { + return false; + } + final String packageName = context.getPackageName(); + for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) { + if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) { + return true; + } + } + return false; + } +} \ No newline at end of file diff --git a/src/nl/vanvianen/android/gcm/GCMIntentService.java b/src/nl/vanvianen/android/gcm/GCMIntentService.java old mode 100755 new mode 100644 index 87082ea..d30c241 --- a/src/nl/vanvianen/android/gcm/GCMIntentService.java +++ b/src/nl/vanvianen/android/gcm/GCMIntentService.java @@ -33,11 +33,16 @@ import org.json.JSONException; import org.json.JSONObject; +import java.lang.Math; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; +import android.app.ActivityManager; +import android.app.ActivityManager.RunningTaskInfo; +import java.util.ArrayList; + public class GCMIntentService extends GCMBaseIntentService { private static final String LCAT = "GCMIntentService"; @@ -54,6 +59,22 @@ public GCMIntentService() { super(""); } + public static boolean getAppIsRunning(Context cont) { + ActivityManager am = (ActivityManager) cont + .getSystemService(Context.ACTIVITY_SERVICE); + @SuppressWarnings("deprecation") + ArrayList list = (ArrayList) am + .getRunningTasks(100); + String MY_PKG_NAME = cont.getPackageName(); + for (RunningTaskInfo info : list) { + if (info.topActivity.getPackageName().equals(MY_PKG_NAME) + || info.baseActivity.getPackageName().equals(MY_PKG_NAME)) { + return true; + } + } + return false; + } + @Override public void onRegistered(Context context, String registrationId) { Log.d(LCAT, "Registered: " + registrationId); @@ -91,6 +112,9 @@ private int getResource(String type, String name) { protected void onMessage(Context context, Intent intent) { Log.d(LCAT, "Push notification received"); + if (AppStateService.initAppStateService(TiApplication.getInstance())) { + return; + } boolean isTopic = false; HashMap data = new HashMap(); @@ -140,7 +164,7 @@ protected void onMessage(Context context, Intent intent) { int priority = 0; boolean bigText = false; int notificationId = 1; - + Integer ledOn = null; Integer ledOff = null; @@ -332,6 +356,7 @@ protected void onMessage(Context context, Intent intent) { Intent launcherIntent = TiApplication.getInstance().getApplicationContext().getPackageManager().getLaunchIntentForPackage(pkg); launcherIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER); + launcherIntent.putExtra("datas",json.toString()); /* Grab notification content from data according to provided keys if not already set */ if (title == null && titleKey != null) { @@ -372,12 +397,23 @@ protected void onMessage(Context context, Intent intent) { if (bitmap == null) { Log.d(LCAT, "No large icon found"); } + notificationId = (int)(Math.random()*100); + PendingIntent pIntent; + if (getAppIsRunning(this)) { + Intent mIntent = new Intent("nl.vanvianen.android.DataReceiver"); + mIntent.putExtra("datas", json.toString()); + PendingIntent broadcastIntent = PendingIntent.getBroadcast(this, + notificationId, mIntent, PendingIntent.FLAG_UPDATE_CURRENT); + pIntent = broadcastIntent; + } else { + pIntent = PendingIntent.getActivity(this, notificationId, launcherIntent, PendingIntent.FLAG_UPDATE_CURRENT); + } NotificationCompat.Builder builder = new NotificationCompat.Builder(context) .setContentTitle(title) .setContentText(message) .setTicker(ticker) - .setContentIntent(PendingIntent.getActivity(this, 0, launcherIntent, PendingIntent.FLAG_ONE_SHOT)) + .setContentIntent(pIntent) .setSmallIcon(smallIcon) .setLargeIcon(bitmap); @@ -481,7 +517,6 @@ protected void onMessage(Context context, Intent intent) { } notification.flags |= Notification.FLAG_AUTO_CANCEL; - ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(notificationId, notification); } } diff --git a/src/nl/vanvianen/android/gcm/GCMModule.java b/src/nl/vanvianen/android/gcm/GCMModule.java index d5c24b9..3adb367 100644 --- a/src/nl/vanvianen/android/gcm/GCMModule.java +++ b/src/nl/vanvianen/android/gcm/GCMModule.java @@ -34,6 +34,7 @@ import java.util.HashMap; import java.util.Map; +import android.content.Intent; @Kroll.module(name = "Gcm", id = "nl.vanvianen.android.gcm") public class GCMModule extends KrollModule { @@ -64,13 +65,21 @@ public GCMModule() { appStateListener = new AppStateListener(); TiApplication.addActivityTransitionListener(appStateListener); } - + } public boolean isInForeground() { return AppStateListener.oneActivityIsResumed; } + @Kroll.method + public void startApp() { + Intent launchIntent = TiApplication.getInstance() + .getApplicationContext().getPackageManager() + .getLaunchIntentForPackage("us.askers"); + TiApplication.getInstance().startActivity(launchIntent); + } + @Kroll.method @SuppressWarnings("unchecked") public void registerPush(HashMap options) { From 7f8c94067776c4ea33278f45c335e1e5813d8c11 Mon Sep 17 00:00:00 2001 From: zmq Date: Tue, 1 Nov 2016 10:34:24 +0800 Subject: [PATCH 2/3] add some sample code to the README --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index 21bea4e..383e33f 100644 --- a/README.md +++ b/README.md @@ -22,4 +22,19 @@ $ ant clean $ ant ``` +Add the code below to your code to extract data from notification. + +``` +if (Ti.UI.Android) { + var bc = Ti.Android.createBroadcastReceiver({ + onReceived: function(e) { + var gcm = require("nl.vanvianen.android.gcm"); + gcm.startApp(); // this line of code get your app to foreground + var d = JSON.parse(e.intent.getStringExtra("datas")); // now you get the data, do whatever you want + } + }); + Ti.Android.registerBroadcastReceiver(bc, ["nl.vanvianen.android.DataReceiver"]); +} +``` + A zip file will be created in the `dist` folder. From f3439f153271281eef697c9deb0f22a8a663799d Mon Sep 17 00:00:00 2001 From: zmq Date: Tue, 1 Nov 2016 10:37:08 +0800 Subject: [PATCH 3/3] add some sample code to the README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 383e33f..a77112a 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ $ ant clean $ ant ``` -Add the code below to your code to extract data from notification. +Add the code below to your code to extract data from notification and just forget the callback function that used to get the gcm's lastData. ``` if (Ti.UI.Android) {