Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new method getActiveNotifications and fix problems #19

Closed
wants to merge 2 commits into from
Closed
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
20 changes: 7 additions & 13 deletions android/src/ti/goosh/IntentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ private void parseNotification(Bundle bundle) {
if (data.has("force_show_in_foreground")) {
JsonPrimitive showInFore = data.getAsJsonPrimitive("force_show_in_foreground");
showNotification = ((showInFore.isBoolean() && showInFore.getAsBoolean() == true));
appInBackground = showNotification;
} else {
showNotification = false;
}
Expand All @@ -110,21 +111,14 @@ private void parseNotification(Bundle bundle) {

if (showNotification) {


// If the Application has a current activity, relaunch it
// otherwise, we need a LaunchIntent
Intent launcherIntent = null;
if (TiApplication.getAppRootOrCurrentActivity() == null) {
launcherIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
launcherIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_SINGLE_TOP);
launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);
} else {
launcherIntent = TiApplication.getAppRootOrCurrentActivity().getIntent();
}

// LaunchIntent
Intent launcherIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
launcherIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_SINGLE_TOP);
launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);
launcherIntent.setAction(Long.toString(System.currentTimeMillis()));
launcherIntent.putExtra("tigoosh.notification", jsonData);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, launcherIntent, PendingIntent.FLAG_ONE_SHOT);
PendingIntent contentIntent = PendingIntent.getActivity(this, (int) bundle.getLong("google.sent_time"), launcherIntent, PendingIntent.FLAG_ONE_SHOT);

// Start building notification

Expand Down
38 changes: 0 additions & 38 deletions android/src/ti/goosh/TiGooshActivityLifecycleCallbacks.java

This file was deleted.

83 changes: 75 additions & 8 deletions android/src/ti/goosh/TiGooshModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@


import java.lang.reflect.Type;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -37,6 +38,14 @@
import com.google.android.gms.iid.InstanceID;

import android.app.NotificationManager;
import android.service.notification.StatusBarNotification;
import android.app.PendingIntent;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONException;

@Kroll.module(name="TiGoosh", id="ti.goosh")
public class TiGooshModule extends KrollModule {
Expand All @@ -61,13 +70,6 @@ public static TiGooshModule getInstance() {
return instance;
}

@Kroll.onAppCreate
public static void onAppCreate(TiApplication app) {
// Register the events to ensure the Intent parsing on resume
TiApplication.getInstance().registerActivityLifecycleCallbacks(new TiGooshActivityLifecycleCallbacks());
Log.d(LCAT, "onAppCreate " + app + " (" + (instance != null) + ")");
}

public void parseIncomingNotificationIntent() {
try {
Activity root = TiApplication.getAppRootOrCurrentActivity();
Expand Down Expand Up @@ -106,6 +108,15 @@ private static NotificationManager getNotificationManager() {
return (NotificationManager) TiApplication.getInstance().getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
}

private static Intent getIntent(PendingIntent pendingIntent) {
try {
Method getIntent = PendingIntent.class.getDeclaredMethod("getIntent");
return (Intent) getIntent.invoke(pendingIntent);
} catch(Exception e) {
throw new RuntimeException(e);
}
}

@Kroll.method
public String getSenderId() {
return TiApplication.getInstance().getAppProperties().getString("gcm.senderid", "");
Expand Down Expand Up @@ -147,6 +158,16 @@ public void cancel(int id) {
getNotificationManager().cancel(-1 * id);
}

@Kroll.method
public Object getActiveNotifications() throws JSONException {
ArrayList<Object> list = new ArrayList<Object>();
for (StatusBarNotification sbn : getNotificationManager().getActiveNotifications()){
JSONObject jsonObject = new JSONObject(getIntent(sbn.getNotification().contentIntent).getStringExtra("tigoosh.notification"));
list.add(jsonToMap(jsonObject));
}
return list.toArray();
}

@Kroll.method
@Kroll.getProperty
public Boolean isRemoteNotificationsEnabled() {
Expand Down Expand Up @@ -214,7 +235,8 @@ public void sendMessage(String data, Boolean inBackground) {

try {
HashMap<String, Object> e = new HashMap<String, Object>();
e.put("data", data); // to parse on reverse on JS side
JSONObject jsonObject = new JSONObject(data);
e.put("data", jsonToMap(jsonObject));
e.put("inBackground", inBackground);

messageCallback.call(getKrollObject(), e);
Expand All @@ -224,5 +246,50 @@ public void sendMessage(String data, Boolean inBackground) {
}
}

public static Map<String, Object> jsonToMap(JSONObject json) throws JSONException {
Map<String, Object> retMap = new HashMap<String, Object>();

if(json != JSONObject.NULL) {
retMap = toMap(json);
}
return retMap;
}

public static Map<String, Object> toMap(JSONObject object) throws JSONException {
Map<String, Object> map = new HashMap<String, Object>();

Iterator<String> keysItr = object.keys();
while(keysItr.hasNext()) {
String key = keysItr.next();
Object value = object.get(key);

if(value instanceof JSONArray) {
value = toList((JSONArray) value);
}

else if(value instanceof JSONObject) {
value = toMap((JSONObject) value);
}
map.put(key, value);
}
return map;
}

public static List<Object> toList(JSONArray array) throws JSONException {
List<Object> list = new ArrayList<Object>();
for(int i = 0; i < array.length(); i++) {
Object value = array.get(i);
if(value instanceof JSONArray) {
value = toList((JSONArray) value);
}

else if(value instanceof JSONObject) {
value = toMap((JSONObject) value);
}
list.add(value);
}
return list;
}

}