Skip to content

Commit 69a4b0d

Browse files
#2 Add 'isRegistered' function
1 parent e08226a commit 69a4b0d

File tree

4 files changed

+52
-9
lines changed

4 files changed

+52
-9
lines changed

src/android/com/plugin/gcm/PushPlugin.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class PushPlugin extends CordovaPlugin {
2424

2525
public static final String REGISTER = "register";
2626
public static final String UNREGISTER = "unregister";
27+
public static final String IS_REGISTERED = "isRegistered";
2728
public static final String EXIT = "exit";
2829

2930
private static CordovaWebView gWebView;
@@ -77,7 +78,15 @@ public boolean execute(String action, JSONArray data, CallbackContext callbackCo
7778
gCachedExtras = null;
7879
}
7980

80-
} else if (UNREGISTER.equals(action)) {
81+
} else if (IS_REGISTERED.equals(action)) {
82+
83+
Log.v(TAG, "IS_REGISTERED");
84+
final boolean registered = GCMRegistrar.isRegistered(getApplicationContext());
85+
Log.d(TAG, "isRegistered? " + registered);
86+
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, registered));
87+
result = true;
88+
89+
} else if (UNREGISTER.equals(action)) {
8190

8291
GCMRegistrar.unregister(getApplicationContext());
8392

src/ios/PushPlugin.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646

4747
- (void)register:(CDVInvokedUrlCommand*)command;
4848

49+
- (void)isRegistered:(CDVInvokedUrlCommand*)command;
50+
4951
- (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;
5052
- (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error;
5153

src/ios/PushPlugin.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ - (void)unregister:(CDVInvokedUrlCommand*)command;
4343
[self successWithMessage:@"unregistered"];
4444
}
4545

46+
- (void)isRegistered:(CDVInvokedUrlCommand*)command;
47+
{
48+
self.callbackId = command.callbackId;
49+
BOOL registered;
50+
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
51+
registered = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
52+
#else
53+
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
54+
registered = types != UIRemoteNotificationTypeNone;
55+
#endif
56+
CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:registered];
57+
[self.commandDelegate sendPluginResult:commandResult callbackId:self.callbackId];
58+
}
59+
4660
- (void)register:(CDVInvokedUrlCommand*)command;
4761
{
4862
self.callbackId = command.callbackId;

www/PushNotification.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,35 @@ PushNotification.prototype.unregister = function(successCallback, errorCallback,
3636
cordova.exec(successCallback, errorCallback, "PushPlugin", "unregister", [options]);
3737
};
3838

39-
// Call this if you want to show toast notification on WP8
40-
PushNotification.prototype.showToastNotification = function (successCallback, errorCallback, options) {
41-
if (errorCallback == null) { errorCallback = function () { } }
39+
// Check to see if we've already registered
40+
PushNotification.prototype.isRegistered = function(successCallback, errorCallback) {
41+
if (errorCallback == null) { errorCallback = function() {}}
42+
43+
if (typeof errorCallback != "function") {
44+
console.log("PushNotification.isRegistered failure: failure parameter not a function");
45+
return
46+
}
47+
48+
if (typeof successCallback != "function") {
49+
console.log("PushNotification.isRegistered failure: success callback parameter must be a function");
50+
return
51+
}
52+
53+
cordova.exec(successCallback, errorCallback, "PushPlugin", "isRegistered", []);
54+
};
4255

43-
if (typeof errorCallback != "function") {
44-
console.log("PushNotification.register failure: failure parameter not a function");
45-
return
46-
}
56+
// Call this if you want to show toast notification on WP8
57+
PushNotification.prototype.showToastNotification = function (successCallback, errorCallback, options) {
58+
if (errorCallback == null) { errorCallback = function () { } }
4759

48-
cordova.exec(successCallback, errorCallback, "PushPlugin", "showToastNotification", [options]);
60+
if (typeof errorCallback != "function") {
61+
console.log("PushNotification.register failure: failure parameter not a function");
62+
return
4963
}
64+
65+
cordova.exec(successCallback, errorCallback, "PushPlugin", "showToastNotification", [options]);
66+
};
67+
5068
// Call this to set the application icon badge
5169
PushNotification.prototype.setApplicationIconBadgeNumber = function(successCallback, errorCallback, badge) {
5270
if (errorCallback == null) { errorCallback = function() {}}

0 commit comments

Comments
 (0)