From fead0aed6ddb293936298f557264927093e49862 Mon Sep 17 00:00:00 2001 From: Mehernosh Bhathena Date: Wed, 4 Mar 2015 07:27:08 +0530 Subject: [PATCH 1/4] changes to remove depricated sendJavascript function for android https://issues.apache.org/jira/browse/CB-6851 --- .../com/plugin/gcm/GCMIntentService.java | 10 ++++- src/android/com/plugin/gcm/PushPlugin.java | 40 ++++++++++++++----- www/PushNotification.js | 18 ++++++++- 3 files changed, 56 insertions(+), 12 deletions(-) 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/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 From 5d9d18c4d137bceeccd7b6dceae0e71f659dfd03 Mon Sep 17 00:00:00 2001 From: Mehernosh Bhathena Date: Mon, 23 Mar 2015 20:01:56 +0530 Subject: [PATCH 2/4] 1:Handled depricated functionality. 2:Also switched to the PluginResult way of calling javascript functions instead of stringByEvaluatingJavaScriptFromString --- src/ios/PushPlugin.h | 3 ++- src/ios/PushPlugin.m | 28 +++++++++++++++++++--------- 2 files changed, 21 insertions(+), 10 deletions(-) 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..81e0532b 100644 --- a/src/ios/PushPlugin.m +++ b/src/ios/PushPlugin.m @@ -35,6 +35,7 @@ @implementation PushPlugin @synthesize callback; + - (void)unregister:(CDVInvokedUrlCommand*)command; { self.callbackId = command.callbackId; @@ -46,7 +47,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 @@ -201,10 +203,10 @@ - (void)notificationReceived { if (notificationMessage && self.callback) { - NSMutableString *jsonStr = [NSMutableString stringWithString:@"{"]; + NSMutableString *jsonStr = [NSMutableString stringWithFormat:@"{\"method_name\":\"%@\",\"method_params\":",self.callback]; [self parseDictionary:notificationMessage intoJSON:jsonStr]; - + if (isInline) { [jsonStr appendFormat:@"foreground:\"%d\"", 1]; @@ -216,10 +218,17 @@ - (void)notificationReceived { [jsonStr appendString:@"}"]; NSLog(@"Msg: %@", jsonStr); - - NSString * jsCallBack = [NSString stringWithFormat:@"%@(%@);", self.callback, jsonStr]; - [self.webView stringByEvaluatingJavaScriptFromString:jsCallBack]; - + + //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 +261,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 +274,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 +283,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]; } From cdbe83e02fd3eeaa3dae64ba72da4feea11a7134 Mon Sep 17 00:00:00 2001 From: Mehernosh Bhathena Date: Mon, 23 Mar 2015 20:13:41 +0530 Subject: [PATCH 3/4] removed orphan lines --- src/ios/PushPlugin.m | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/src/ios/PushPlugin.m b/src/ios/PushPlugin.m index 81e0532b..d1f6111e 100644 --- a/src/ios/PushPlugin.m +++ b/src/ios/PushPlugin.m @@ -203,25 +203,6 @@ - (void)notificationReceived { if (notificationMessage && self.callback) { - NSMutableString *jsonStr = [NSMutableString stringWithFormat:@"{\"method_name\":\"%@\",\"method_params\":",self.callback]; - - [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 From c2852240bbd01f8358ddab6018354bee94aa71a1 Mon Sep 17 00:00:00 2001 From: Mehernosh Bhathena Date: Mon, 23 Mar 2015 21:03:29 +0530 Subject: [PATCH 4/4] enabledRemoteNotificationTypes is not available for ios 8.0 and up --- src/ios/PushPlugin.m | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/ios/PushPlugin.m b/src/ios/PushPlugin.m index d1f6111e..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 @@ -158,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";