Skip to content

Commit

Permalink
"Parse" incoming data object when in background
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenstolk committed Aug 4, 2015
1 parent 0985299 commit bc3c94e
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions src/android/com/plugin/gcm/GCMIntentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class GCMIntentService extends GCMBaseIntentService {

private static final String TAG = "GCMIntentService";

public GCMIntentService() {
super("GCMIntentService");
}
Expand Down Expand Up @@ -64,19 +64,30 @@ protected void onMessage(Context context, Intent intent) {
if (extras != null)
{
// if we are in the foreground, just surface the payload, else post it to the statusbar
if (PushPlugin.isInForeground()) {
if (PushPlugin.isInForeground()) {
extras.putBoolean("foreground", true);
PushPlugin.sendExtras(extras);
PushPlugin.sendExtras(extras);
}
else {
extras.putBoolean("foreground", false);

// Send a notification if there is a message
if (extras.getString("message") != null && extras.getString("message").length() != 0) {
createNotification(context, extras);
}
}
}
String message = extras.getString("message");

if (message == null) {
// Providers like Parse always send a 'data' as root object, so "Parse" that
try {
JSONObject object_example = new JSONObject(extras.getString("data"));
message = object_example.getString("alert");
extras.putString("message", message);
}
catch (JSONException e) {}
}

// Send a notification if there is a message
if (message != null && message.length() != 0) {
createNotification(context, extras);
}
}
}
}

public void createNotification(Context context, Bundle extras)
Expand All @@ -89,15 +100,15 @@ public void createNotification(Context context, Bundle extras)
notificationIntent.putExtra("pushBundle", extras);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

int defaults = Notification.DEFAULT_ALL;

if (extras.getString("defaults") != null) {
try {
defaults = Integer.parseInt(extras.getString("defaults"));
} catch (NumberFormatException e) {}
}

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setDefaults(defaults)
Expand All @@ -119,9 +130,9 @@ public void createNotification(Context context, Bundle extras)
if (msgcnt != null) {
mBuilder.setNumber(Integer.parseInt(msgcnt));
}

int notId = 0;

try {
notId = Integer.parseInt(extras.getString("notId"));
}
Expand All @@ -131,20 +142,20 @@ public void createNotification(Context context, Bundle extras)
catch(Exception e) {
Log.e(TAG, "Number format exception - Error parsing Notification ID" + e.getMessage());
}

mNotificationManager.notify((String) appName, notId, mBuilder.build());
}

private static String getAppName(Context context)
{
CharSequence appName =
CharSequence appName =
context
.getPackageManager()
.getApplicationLabel(context.getApplicationInfo());

return (String)appName;
}

@Override
public void onError(Context context, String errorId) {
Log.e(TAG, "onError - errorId: " + errorId);
Expand Down

0 comments on commit bc3c94e

Please sign in to comment.