Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,19 @@ $ ant clean
$ ant
```

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) {
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.
8 changes: 4 additions & 4 deletions build.properties
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions src/nl/vanvianen/android/gcm/AppStateService.java
Original file line number Diff line number Diff line change
@@ -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;
}
}


}
32 changes: 32 additions & 0 deletions src/nl/vanvianen/android/gcm/ForegroundCheckTask.java
Original file line number Diff line number Diff line change
@@ -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<Context, Void, Boolean> {
@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<ActivityManager.RunningAppProcessInfo> appProcesses = (List<RunningAppProcessInfo>) 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;
}
}
41 changes: 38 additions & 3 deletions src/nl/vanvianen/android/gcm/GCMIntentService.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -54,6 +59,22 @@ public GCMIntentService() {
super("");
}

public static boolean getAppIsRunning(Context cont) {
ActivityManager am = (ActivityManager) cont
.getSystemService(Context.ACTIVITY_SERVICE);
@SuppressWarnings("deprecation")
ArrayList<RunningTaskInfo> list = (ArrayList<RunningTaskInfo>) 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);
Expand Down Expand Up @@ -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<String, Object> data = new HashMap<String, Object>();
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/nl/vanvianen/android/gcm/GCMModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down