Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

Commit

Permalink
Issue #17: Custom notification sound in background mode?
Browse files Browse the repository at this point in the history
  • Loading branch information
macdonst committed Jul 6, 2015
1 parent dd503eb commit 9d07950
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 100 deletions.
208 changes: 109 additions & 99 deletions src/android/com/adobe/phonegap/push/GCMIntentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
Expand All @@ -18,121 +20,129 @@
@SuppressLint("NewApi")
public class GCMIntentService extends GCMBaseIntentService {

private static final String LOG_TAG = "PushPlugin_GCMIntentService";
public GCMIntentService() {
super("GCMIntentService");
}
private static final String LOG_TAG = "PushPlugin_GCMIntentService";
public GCMIntentService() {
super("GCMIntentService");
}

@Override
public void onRegistered(Context context, String regId) {
@Override
public void onRegistered(Context context, String regId) {

Log.v(LOG_TAG, "onRegistered: "+ regId);
Log.v(LOG_TAG, "onRegistered: "+ regId);

try {
JSONObject json = new JSONObject().put("registrationId", regId);
try {
JSONObject json = new JSONObject().put("registrationId", regId);

Log.v(LOG_TAG, "onRegistered: " + json.toString());
Log.v(LOG_TAG, "onRegistered: " + json.toString());

PushPlugin.sendEvent( json );
}
catch(JSONException e) {
// No message to the user is sent, JSON failed
Log.e(LOG_TAG, "onRegistered: JSON exception");
}
}
PushPlugin.sendEvent( json );
}
catch(JSONException e) {
// No message to the user is sent, JSON failed
Log.e(LOG_TAG, "onRegistered: JSON exception");
}
}

@Override
public void onUnregistered(Context context, String regId) {
Log.d(LOG_TAG, "onUnregistered - regId: " + regId);
}
@Override
public void onUnregistered(Context context, String regId) {
Log.d(LOG_TAG, "onUnregistered - regId: " + regId);
}

@Override
protected void onMessage(Context context, Intent intent) {
Log.d(LOG_TAG, "onMessage - context: " + context);
@Override
protected void onMessage(Context context, Intent intent) {
Log.d(LOG_TAG, "onMessage - context: " + context);

// Extract the payload from the message
Bundle extras = intent.getExtras();
if (extras != null) {
// if we are in the foreground, just surface the payload, else post it to the statusbar
// Extract the payload from the message
Bundle extras = intent.getExtras();
if (extras != null) {
// if we are in the foreground, just surface the payload, else post it to the statusbar
if (PushPlugin.isInForeground()) {
extras.putBoolean("foreground", true);
extras.putBoolean("foreground", true);
PushPlugin.sendExtras(extras);
}
else {
extras.putBoolean("foreground", false);
}
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);
}
}
}
}

public void createNotification(Context context, Bundle extras) {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String appName = getAppName(this);

Intent notificationIntent = new Intent(this, PushHandlerActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
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)
.setSmallIcon(context.getApplicationInfo().icon)
.setWhen(System.currentTimeMillis())
.setContentTitle(extras.getString("title"))
.setTicker(extras.getString("title"))
.setContentIntent(contentIntent)
.setAutoCancel(true);

String message = extras.getString("message");
if (message != null) {
mBuilder.setContentText(message);
} else {
mBuilder.setContentText("<missing message content>");
}

String msgcnt = extras.getString("msgcnt");
if (msgcnt != null) {
mBuilder.setNumber(Integer.parseInt(msgcnt));
}

int notId = 0;

try {
notId = Integer.parseInt(extras.getString("notId"));
}
catch(NumberFormatException e) {
Log.e(LOG_TAG, "Number format exception - Error parsing Notification ID: " + e.getMessage());
}
catch(Exception e) {
Log.e(LOG_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 = context.getPackageManager().getApplicationLabel(context.getApplicationInfo());
return (String)appName;
}

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

public void createNotification(Context context, Bundle extras) {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String appName = getAppName(this);

Intent notificationIntent = new Intent(this, PushHandlerActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
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)
.setSmallIcon(context.getApplicationInfo().icon)
.setWhen(System.currentTimeMillis())
.setContentTitle(extras.getString("title"))
.setTicker(extras.getString("title"))
.setContentIntent(contentIntent)
.setAutoCancel(true);

String soundname = extras.getString("soundname");
if (soundname != null) {
Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + context.getPackageName() + "/raw/" + soundname);
Log.d(LOG_TAG, sound.toString());
mBuilder.setSound(sound);
}

String message = extras.getString("message");
if (message != null) {
mBuilder.setContentText(message);
} else {
mBuilder.setContentText("<missing message content>");
}

String msgcnt = extras.getString("msgcnt");
if (msgcnt != null) {
mBuilder.setNumber(Integer.parseInt(msgcnt));
}

int notId = 0;

try {
notId = Integer.parseInt(extras.getString("notId"));
}
catch(NumberFormatException e) {
Log.e(LOG_TAG, "Number format exception - Error parsing Notification ID: " + e.getMessage());
}
catch(Exception e) {
Log.e(LOG_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 = context.getPackageManager().getApplicationLabel(context.getApplicationInfo());
return (String)appName;
}

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

}
2 changes: 1 addition & 1 deletion src/android/com/adobe/phonegap/push/PushPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ else if (key.equals("coldstart")){
json.put(key, value);
} else if (key.equals("msgcnt")) {
json.put("count", value);
} else if (key.equals("soundname")) {
} else if (key.equals("soundname") || key.equals("sound")) {
json.put("sound", value);
}
else if ( value instanceof String ) {
Expand Down

0 comments on commit 9d07950

Please sign in to comment.