Skip to content

Commit

Permalink
Fixing issue of activity PushHandler won't launch
Browse files Browse the repository at this point in the history
  • Loading branch information
Flavio De Stefano committed Oct 28, 2016
1 parent 4cad017 commit 948158e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
22 changes: 15 additions & 7 deletions android/src/ti/goosh/IntentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,15 @@ private Bitmap getBitmapFromURL(String src) throws Exception {
}

private void parseNotification(Bundle bundle) {
TiGooshModule module = TiGooshModule.getModule();

Context context = getApplicationContext();
Boolean appInBackground = !TiApplication.isCurrentActivityInForeground();

// Flag that determine if the message should be broadcasted to TiGooshModule and call the callback
Boolean sendMessage = !appInBackground;

// Flag to show the system alert
Boolean showNotification = true;

String jsonData = bundle.getString("data");
Expand All @@ -94,16 +100,18 @@ private void parseNotification(Bundle bundle) {
}

if (data != null && data.has("alert") == true) {

if (appInBackground) {
showNotification = true;
} else {
if (data.has("force_show_in_foreground")) {
JsonPrimitive showInFore = data.getAsJsonPrimitive("force_show_in_foreground");
showNotification = ((showInFore.isBoolean() && showInFore.getAsBoolean() == true));
JsonPrimitive forceShowInForeground = data.getAsJsonPrimitive("force_show_in_foreground");
showNotification = ((forceShowInForeground.isBoolean() && forceShowInForeground.getAsBoolean() == true));
} else {
showNotification = false;
}
}

} else {

Log.i(LCAT, "Not showing notification cause missing data.alert");
Expand All @@ -118,10 +126,14 @@ private void parseNotification(Bundle bundle) {

}

if (sendMessage) {
module.sendMessage(jsonData, appInBackground);
}

if (showNotification) {

Intent notificationIntent = new Intent(this, PushHandlerActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.putExtra(TiGooshModule.INTENT_EXTRA, jsonData);

PendingIntent contentIntent = PendingIntent.getActivity(this, new Random().nextInt(), notificationIntent, PendingIntent.FLAG_ONE_SHOT);
Expand Down Expand Up @@ -375,10 +387,6 @@ private void parseNotification(Bundle bundle) {
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(tag, id, builder.build());
}

if (!appInBackground && TiGooshModule.getInstance() != null) {
TiGooshModule.getInstance().sendMessage(jsonData, appInBackground);
}
}

}
13 changes: 10 additions & 3 deletions android/src/ti/goosh/PushHandlerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,31 @@ public void onCreate(Bundle savedInstanceState) {
Log.d(LCAT, "started");
super.onCreate(savedInstanceState);

TiGooshModule module = TiGooshModule.getModule();
Context context = getApplicationContext();
String notification = getIntent().getStringExtra(TiGooshModule.INTENT_EXTRA);
TiGooshModule instance = TiGooshModule.getInstance();

Intent launcherIntent;

if (instance == null) {
if (TiApplication.getAppRootOrCurrentActivity() == null) {
launcherIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
launcherIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
} else {
launcherIntent = TiApplication.getAppRootOrCurrentActivity().getIntent();
instance.sendMessage(notification, true);
if (module != null) {
TiGooshModule.getModule().sendMessage(notification, true);
}
}

launcherIntent.putExtra(TiGooshModule.INTENT_EXTRA, notification);
startActivity(launcherIntent);

finish();
}

@Override protected void onResume() {
Log.d(LCAT, "resumed");
super.onResume();
}

}
17 changes: 10 additions & 7 deletions android/src/ti/goosh/RegistrationIntentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,26 @@ public RegistrationIntentService() {

@Override
protected void onHandleIntent(Intent intent) {
TiGooshModule instance = TiGooshModule.getInstance();
if (instance == null) return;
TiGooshModule module = TiGooshModule.getModule();
if (module == null) {
Log.e(LCAT, "Intent handled but no TiGoosh instance module found");
return;
}

try {

String senderId = instance.getSenderId();
Log.i(LCAT, "Sender ID: " + senderId);

String senderId = module.getSenderId();
String token = InstanceID.getInstance(this).getToken(senderId, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

Log.i(LCAT, "Sender ID: " + senderId);
Log.i(LCAT, "Device Token: " + token);

instance.sendSuccess(token);
module.sendSuccess(token);

} catch (Exception ex) {

Log.e(LCAT, "Failed to get GCM Registration Token:" + ex.getMessage());
instance.sendError(ex);
module.sendError(ex);

}
}
Expand Down

0 comments on commit 948158e

Please sign in to comment.