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

"Parse" incoming data object when in background #617

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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