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

dealt with deprications in core iOS and cordova-android + Bug Fixes #518

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
10 changes: 9 additions & 1 deletion src/android/com/plugin/gcm/GCMIntentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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);
Expand Down
40 changes: 30 additions & 10 deletions src/android/com/plugin/gcm/PushPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -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());
Expand All @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/ios/PushPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
44 changes: 20 additions & 24 deletions src/ios/PushPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/

#import "PushPlugin.h"
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)

@implementation PushPlugin

Expand All @@ -35,6 +36,7 @@ @implementation PushPlugin
@synthesize callback;



- (void)unregister:(CDVInvokedUrlCommand*)command;
{
self.callbackId = command.callbackId;
Expand All @@ -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
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;

Expand All @@ -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];
}
}
Expand All @@ -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];
}

Expand Down
18 changes: 17 additions & 1 deletion www/PushNotification.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down