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 24, 2015
1 parent dc15f50 commit ccc125a
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 129 deletions.
14 changes: 11 additions & 3 deletions src/android/com/adobe/phonegap/push/GCMIntentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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.content.SharedPreferences;
Expand All @@ -20,6 +21,7 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
Expand Down Expand Up @@ -103,13 +105,12 @@ public void createNotification(Context context, Bundle extras) {

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setDefaults(defaults)
//.setDefaults(defaults)
.setWhen(System.currentTimeMillis())
.setContentTitle(extras.getString("title"))
.setTicker(extras.getString("title"))
.setContentIntent(contentIntent)
.setAutoCancel(true);


SharedPreferences prefs = context.getSharedPreferences("com.adobe.phonegap.push", Context.MODE_PRIVATE);
String localIcon = prefs.getString("icon", null);
Expand Down Expand Up @@ -198,7 +199,14 @@ public void createNotification(Context context, Bundle extras) {
}
}


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);
Expand Down
252 changes: 126 additions & 126 deletions src/android/com/adobe/phonegap/push/PushPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,56 +20,56 @@
import java.util.Iterator;

public class PushPlugin extends CordovaPlugin {
public static final String LOG_TAG = "PushPlugin";
public static final String LOG_TAG = "PushPlugin";

public static final String INITIALIZE = "init";
public static final String UNREGISTER = "unregister";
public static final String EXIT = "exit";
public static final String INITIALIZE = "init";
public static final String UNREGISTER = "unregister";
public static final String EXIT = "exit";

private static CallbackContext pushContext;
private static CordovaWebView gWebView;
private static String gSenderID;
private static Bundle gCachedExtras = null;
private static CallbackContext pushContext;
private static CordovaWebView gWebView;
private static String gSenderID;
private static Bundle gCachedExtras = null;
private static boolean gForeground = false;

/**
* Gets the application context from cordova's main activity.
* @return the application context
*/
private Context getApplicationContext() {
return this.cordova.getActivity().getApplicationContext();
}
/**
* Gets the application context from cordova's main activity.
* @return the application context
*/
private Context getApplicationContext() {
return this.cordova.getActivity().getApplicationContext();
}

@Override
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) {
@Override
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) {

boolean result = false;
boolean result = false;

Log.v(LOG_TAG, "execute: action=" + action);
Log.v(LOG_TAG, "execute: action=" + action);

if (INITIALIZE.equals(action)) {
pushContext = callbackContext;
if (INITIALIZE.equals(action)) {
pushContext = callbackContext;
JSONObject jo = null;

Log.v(LOG_TAG, "execute: data=" + data.toString());
Log.v(LOG_TAG, "execute: data=" + data.toString());

try {
try {
jo = data.getJSONObject(0).getJSONObject("android");

gWebView = this.webView;
Log.v(LOG_TAG, "execute: jo=" + jo.toString());
gWebView = this.webView;
Log.v(LOG_TAG, "execute: jo=" + jo.toString());

gSenderID = jo.getString("senderID");
gSenderID = jo.getString("senderID");

Log.v(LOG_TAG, "execute: senderID=" + gSenderID);
Log.v(LOG_TAG, "execute: senderID=" + gSenderID);

GCMRegistrar.register(getApplicationContext(), gSenderID);
result = true;
} catch (JSONException e) {
Log.e(LOG_TAG, "execute: Got JSON Exception " + e.getMessage());
result = false;
callbackContext.error(e.getMessage());
}
GCMRegistrar.register(getApplicationContext(), gSenderID);
result = true;
} catch (JSONException e) {
Log.e(LOG_TAG, "execute: Got JSON Exception " + e.getMessage());
result = false;
callbackContext.error(e.getMessage());
}

if (jo != null) {
SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("com.adobe.phonegap.push", Context.MODE_PRIVATE);
Expand All @@ -87,56 +87,56 @@ public boolean execute(String action, JSONArray data, CallbackContext callbackCo
editor.commit();
}

if ( gCachedExtras != null) {
Log.v(LOG_TAG, "sending cached extras");
sendExtras(gCachedExtras);
gCachedExtras = null;
}
if ( gCachedExtras != null) {
Log.v(LOG_TAG, "sending cached extras");
sendExtras(gCachedExtras);
gCachedExtras = null;
}

} else if (UNREGISTER.equals(action)) {
} else if (UNREGISTER.equals(action)) {

GCMRegistrar.unregister(getApplicationContext());
GCMRegistrar.unregister(getApplicationContext());

Log.v(LOG_TAG, "UNREGISTER");
result = true;
callbackContext.success();
} else {
result = false;
Log.e(LOG_TAG, "Invalid action : " + action);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.INVALID_ACTION));
}
Log.v(LOG_TAG, "UNREGISTER");
result = true;
callbackContext.success();
} else {
result = false;
Log.e(LOG_TAG, "Invalid action : " + action);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.INVALID_ACTION));
}

return result;
}
return result;
}

public static void sendEvent(JSONObject _json) {
public static void sendEvent(JSONObject _json) {
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, _json);
pluginResult.setKeepCallback(true);
pushContext.sendPluginResult(pluginResult);
}

/*
* Sends the pushbundle extras to the client application.
* If the client application isn't currently active, it is cached for later processing.
*/
public static void sendExtras(Bundle extras) {
if (extras != null) {
if (gWebView != null) {
sendEvent(convertBundleToJson(extras));
} else {
Log.v(LOG_TAG, "sendExtras: caching extras to send at a later time.");
gCachedExtras = extras;
}
}
}
}

/*
* Sends the pushbundle extras to the client application.
* If the client application isn't currently active, it is cached for later processing.
*/
public static void sendExtras(Bundle extras) {
if (extras != null) {
if (gWebView != null) {
sendEvent(convertBundleToJson(extras));
} else {
Log.v(LOG_TAG, "sendExtras: caching extras to send at a later time.");
gCachedExtras = extras;
}
}
}

@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);
gForeground = true;
}

@Override
@Override
public void onPause(boolean multitasking) {
super.onPause(multitasking);
gForeground = false;
Expand All @@ -154,77 +154,77 @@ public void onResume(boolean multitasking) {
public void onDestroy() {
super.onDestroy();
gForeground = false;
gWebView = null;
gWebView = null;
}

/*
* serializes a bundle to JSON.
*/
private static JSONObject convertBundleToJson(Bundle extras) {
try {
JSONObject json = new JSONObject();
JSONObject additionalData = new JSONObject();
Iterator<String> it = extras.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
Object value = extras.get(key);
Log.d(LOG_TAG, "key = " + key);

// System data from Android
if (key.equals("from") || key.equals("collapse_key")) {
additionalData.put(key, value);
}
else if (key.equals("foreground")) {
additionalData.put(key, extras.getBoolean("foreground"));
}
else if (key.equals("coldstart")){
additionalData.put(key, extras.getBoolean("coldstart"));
} else if (key.equals("message") || key.equals("title")) {
json.put(key, value);
} else if (key.equals("msgcnt")) {
json.put("count", value);
} else if (key.equals("soundname")) {
json.put("sound", value);
} else if (key.equals("image")) {
json.put("image", value);
}
else if ( value instanceof String ) {
String strValue = (String)value;
try {
// Try to figure out if the value is another JSON object
if (strValue.startsWith("{")) {
additionalData.put(key, new JSONObject(strValue));
}
// Try to figure out if the value is another JSON array
else if (strValue.startsWith("[")) {
additionalData.put(key, new JSONArray(strValue));
}
else {
additionalData.put(key, value);
}
} catch (Exception e) {
try {
JSONObject json = new JSONObject();
JSONObject additionalData = new JSONObject();
Iterator<String> it = extras.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
Object value = extras.get(key);
Log.d(LOG_TAG, "key = " + key);

// System data from Android
if (key.equals("from") || key.equals("collapse_key")) {
additionalData.put(key, value);
}
else if (key.equals("foreground")) {
additionalData.put(key, extras.getBoolean("foreground"));
}
else if (key.equals("coldstart")){
additionalData.put(key, extras.getBoolean("coldstart"));
} else if (key.equals("message") || key.equals("title")) {
json.put(key, value);
} else if (key.equals("msgcnt")) {
json.put("count", value);
} else if (key.equals("soundname") || key.equals("sound")) {
json.put("sound", value);
} else if (key.equals("image")) {
json.put("image", value);
}
else if ( value instanceof String ) {
String strValue = (String)value;
try {
// Try to figure out if the value is another JSON object
if (strValue.startsWith("{")) {
additionalData.put(key, new JSONObject(strValue));
}
// Try to figure out if the value is another JSON array
else if (strValue.startsWith("[")) {
additionalData.put(key, new JSONArray(strValue));
}
else {
additionalData.put(key, value);
}
} catch (Exception e) {
additionalData.put(key, value);
}
}
} // while
json.put("additionalData", additionalData);
Log.v(LOG_TAG, "extrasToJSON: " + json.toString());

return json;
}
catch( JSONException e) {
Log.e(LOG_TAG, "extrasToJSON: JSON exception");
}
return null;
}
} // while
json.put("additionalData", additionalData);
Log.v(LOG_TAG, "extrasToJSON: " + json.toString());

return json;
}
catch( JSONException e) {
Log.e(LOG_TAG, "extrasToJSON: JSON exception");
}
return null;
}

public static boolean isInForeground() {
return gForeground;
}

public static boolean isActive() {
return gWebView != null;
return gWebView != null;
}
}

0 comments on commit ccc125a

Please sign in to comment.