diff --git a/src/android/com/plugin/gcm/GCMIntentService.java b/src/android/com/plugin/gcm/GCMIntentService.java index caee145e..c9304025 100644 --- a/src/android/com/plugin/gcm/GCMIntentService.java +++ b/src/android/com/plugin/gcm/GCMIntentService.java @@ -15,6 +15,10 @@ import com.google.android.gcm.GCMBaseIntentService; +import android.net.Uri; +import android.media.Ringtone; +import android.media.RingtoneManager; + @SuppressLint("NewApi") public class GCMIntentService extends GCMBaseIntentService { @@ -70,7 +74,11 @@ protected void onMessage(Context context, Intent intent) { } else { extras.putBoolean("foreground", false); - + + Uri notificationUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); + Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notificationUri); + r.play(); + // Send a notification if there is a message if (extras.getString("message") != null && extras.getString("message").length() != 0) { createNotification(context, extras); diff --git a/src/android/com/plugin/gcm/PushPlugin.java b/src/android/com/plugin/gcm/PushPlugin.java index 3e390856..0e8191e2 100644 --- a/src/android/com/plugin/gcm/PushPlugin.java +++ b/src/android/com/plugin/gcm/PushPlugin.java @@ -12,6 +12,7 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import org.apache.cordova.PluginResult; import java.util.Iterator; @@ -31,7 +32,7 @@ public class PushPlugin extends CordovaPlugin { private static String gSenderID; private static Bundle gCachedExtras = null; private static boolean gForeground = false; - + private static CallbackContext gcmPushCallbackContext; /** * Gets the application context from cordova's main activity. * @return the application context @@ -47,6 +48,7 @@ public boolean execute(String action, JSONArray data, CallbackContext callbackCo Log.v(TAG, "execute: action=" + action); + gcmPushCallbackContext = callbackContext; if (REGISTER.equals(action)) { Log.v(TAG, "execute: data=" + data.toString()); @@ -63,8 +65,13 @@ public boolean execute(String action, JSONArray data, CallbackContext callbackCo Log.v(TAG, "execute: ECB=" + gECB + " senderID=" + gSenderID); GCMRegistrar.register(getApplicationContext(), gSenderID); - result = true; - callbackContext.success(); + + result = true; + + PluginResult pluginResult = new PluginResult(PluginResult.Status.OK,result); + pluginResult.setKeepCallback(true); + callbackContext.sendPluginResult(pluginResult); + //callbackContext.success(); } catch (JSONException e) { Log.e(TAG, "execute: Got JSON Exception " + e.getMessage()); result = false; @@ -97,13 +104,25 @@ public boolean execute(String action, JSONArray data, CallbackContext callbackCo * Sends a json object to the client as parameter to a method which is defined in gECB. */ public static void sendJavascript(JSONObject _json) { - String _d = "javascript:" + gECB + "(" + _json.toString() + ")"; - Log.v(TAG, "sendJavascript: " + _d); - - if (gECB != null && gWebView != null) { - gWebView.sendJavascript(_d); - } - } + //String _d = "javascript:" + gECB + "(" + _json.toString() + ")"; + //Log.v(TAG, "sendJavascript: " + _d); + + //if (gECB != null && gWebView != null) { + // gWebView.sendJavascript(_d); + //} + if (gcmPushCallbackContext != null) { + try{ + JSONObject jsMessage = new JSONObject().put("method_name",gECB); + jsMessage.put("method_params",_json); + PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, jsMessage); + pluginResult.setKeepCallback(true); + gcmPushCallbackContext.sendPluginResult(pluginResult); + }catch( JSONException e){ + Log.e(TAG, "extrasToJSON: JSON exception"); + } + } + gWebView.postMessage("pushnotification", 1); + } /* * Sends the pushbundle extras to the client application. @@ -125,6 +144,7 @@ public static void sendExtras(Bundle extras) public void initialize(CordovaInterface cordova, CordovaWebView webView) { super.initialize(cordova, webView); gForeground = true; + gcmPushCallbackContext = null; } @Override diff --git a/src/ios/PushPlugin.h b/src/ios/PushPlugin.h index 7e7ba4bc..1a762408 100644 --- a/src/ios/PushPlugin.h +++ b/src/ios/PushPlugin.h @@ -38,7 +38,8 @@ } @property (nonatomic, copy) NSString *callbackId; -@property (nonatomic, copy) NSString *notificationCallbackId; +@property (retain, nonatomic) NSString *notificationCallbackId; +//@property (nonatomic, copy) NSString *notificationCallbackId; @property (nonatomic, copy) NSString *callback; @property (nonatomic, strong) NSDictionary *notificationMessage; diff --git a/src/ios/PushPlugin.m b/src/ios/PushPlugin.m index 7686ae9d..01538a5e 100644 --- a/src/ios/PushPlugin.m +++ b/src/ios/PushPlugin.m @@ -24,6 +24,7 @@ */ #import "PushPlugin.h" +#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending) @implementation PushPlugin @@ -35,6 +36,7 @@ @implementation PushPlugin @synthesize callback; + - (void)unregister:(CDVInvokedUrlCommand*)command; { self.callbackId = command.callbackId; @@ -46,7 +48,8 @@ - (void)unregister:(CDVInvokedUrlCommand*)command; - (void)register:(CDVInvokedUrlCommand*)command; { self.callbackId = command.callbackId; - + self.notificationCallbackId = command.callbackId; + NSMutableDictionary* options = [command.arguments objectAtIndex:0]; #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 @@ -156,8 +159,12 @@ - (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { [results setValue:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] forKey:@"appVersion"]; // Check what Notifications the user has turned on. We registered for all three, but they may have manually disabled some or all of them. - NSUInteger rntypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; - + NSUInteger rntypes; + if (!SYSTEM_VERSION_LESS_THAN(@"8.0")) { + rntypes = [[[UIApplication sharedApplication] currentUserNotificationSettings] types]; + }else{ + rntypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; + } // Set the defaults to disabled unless we find otherwise... NSString *pushBadge = @"disabled"; NSString *pushAlert = @"disabled"; @@ -201,25 +208,13 @@ - (void)notificationReceived { if (notificationMessage && self.callback) { - NSMutableString *jsonStr = [NSMutableString stringWithString:@"{"]; - - [self parseDictionary:notificationMessage intoJSON:jsonStr]; - - if (isInline) - { - [jsonStr appendFormat:@"foreground:\"%d\"", 1]; - isInline = NO; - } - else - [jsonStr appendFormat:@"foreground:\"%d\"", 0]; - - [jsonStr appendString:@"}"]; - - NSLog(@"Msg: %@", jsonStr); - - NSString * jsCallBack = [NSString stringWithFormat:@"%@(%@);", self.callback, jsonStr]; - [self.webView stringByEvaluatingJavaScriptFromString:jsCallBack]; - + NSDictionary *jDict = @{ + @"method_name":self.callback, + @"method_params":self.notificationMessage + }; + CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:jDict]; + [commandResult setKeepCallbackAsBool:YES]; + [self.commandDelegate sendPluginResult:commandResult callbackId:self.callbackId]; self.notificationMessage = nil; } } @@ -252,7 +247,7 @@ -(void)parseDictionary:(NSDictionary *)inDictionary intoJSON:(NSMutableString *) - (void)setApplicationIconBadgeNumber:(CDVInvokedUrlCommand *)command { self.callbackId = command.callbackId; - + self.notificationCallbackId = command.callbackId; NSMutableDictionary* options = [command.arguments objectAtIndex:0]; int badge = [[options objectForKey:@"badge"] intValue] ?: 0; @@ -265,6 +260,7 @@ -(void)successWithMessage:(NSString *)message if (self.callbackId != nil) { CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:message]; + [commandResult setKeepCallbackAsBool:YES]; [self.commandDelegate sendPluginResult:commandResult callbackId:self.callbackId]; } } @@ -273,7 +269,7 @@ -(void)failWithMessage:(NSString *)message withError:(NSError *)error { NSString *errorMessage = (error) ? [NSString stringWithFormat:@"%@ - %@", message, [error localizedDescription]] : message; CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:errorMessage]; - + [commandResult setKeepCallbackAsBool:YES]; [self.commandDelegate sendPluginResult:commandResult callbackId:self.callbackId]; } diff --git a/www/PushNotification.js b/www/PushNotification.js index 6127cf56..f7f755ce 100644 --- a/www/PushNotification.js +++ b/www/PushNotification.js @@ -16,7 +16,23 @@ PushNotification.prototype.register = function(successCallback, errorCallback, o return } - cordova.exec(successCallback, errorCallback, "PushPlugin", "register", [options]); + cordova.exec(function(m){ + console.log(m); + if(m.hasOwnProperty('method_name')){ + var namespaces = m['method_name'].split("."); + var func = namespaces.pop(); + var context = window; + for(var i = 0; i < namespaces.length; i++) { + context = context[namespaces[i]]; + } + var args = undefined; + if(m.hasOwnProperty('method_params')){ + args = m['method_params']; + } + return context[func].call(this,args); + } + else successCallback(m); + }, errorCallback, "PushPlugin", "register", [options]); }; // Call this to unregister for push notifications