Created
April 22, 2016 21:31
-
-
Save evantahler/bf5b2430544d37ad5aa0a0d3fdc12974 to your computer and use it in GitHub Desktop.
PhoneGap and Push
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { | |
for (id key in userInfo) { | |
NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]); | |
if ([key isEqualToString:@"aps"]){ | |
self.LastPushMessageMessage = [[userInfo objectForKey:key] objectForKey:@"alert"]; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// set me at the top of the file before entering a method | |
@property (retain, nonatomic) NSString* token; | |
and extend the **didFinishLaunchingWithOptions** method in **AppDelagte.m** | |
// Let the device know we want to receive push notifications | |
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; | |
and add 2 new methods, also to **AppDelegate.m** | |
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { | |
self.token = [[[[deviceToken description] | |
stringByReplacingOccurrencesOfString: @" stringByReplacingOccurrencesOfString: @" " withString: @""]; | |
NSLog(@"My token is: %@", self.token); | |
} | |
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { | |
NSLog(@"PUSH FAIL!!!"); | |
NSLog(@"error: %@",error); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { | |
self.token = [[[[deviceToken description] | |
stringByReplacingOccurrencesOfString: @" stringByReplacingOccurrencesOfString: @" " withString: @""]; | |
NSLog(@"My token is: %@", self.token); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getDeviceToken = function(){ | |
if(typeof device != "undefined" && typeof Cordova == "object"){ | |
var getToken = function(types, success, fail){ | |
Cordova.exec(success, fail, "PushToken", "getToken", types); | |
} | |
getToken(["getToken"], function(token){ | |
device.token = token; | |
return token; | |
}, function(e){ | |
console.log("cannot get device token: "+e); | |
return false; | |
}); | |
}else{ | |
// console.log("device not ready, or not a native app"); | |
return false; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.getLastPushMessage = function(){ | |
clearTimeout(app.timers["iosMessageTimer"]); | |
if(typeof device != "undefined" && typeof Cordova == "object"){ | |
var getMessageFromIos = function(types, success, fail){ | |
Cordova.exec(success, fail, "LastPushMessage", "getLastPushMessage", types); | |
} | |
getMessageFromIos(["getLastPushMessage"], function(message){ | |
message = unescape(message); | |
if(message != null && message.length > 0 && message != app.lastPushMessage){ | |
app.showMessage(message); | |
} | |
app.lastPushMessage = message; | |
return message; | |
}, function(e){ | |
console.log("cannot get last message: "+e); | |
return false; | |
}); | |
app.timers["iosMessageTimer"] = setTimeout(app.getLastPushMessage, 1000); | |
}else{ | |
// console.log("device not ready, or not a native app"); | |
return false; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import | |
#import | |
@interface LastPushMessage : CDVPlugin{ | |
NSString* LastPushMessageCallbackID; | |
} | |
@property (nonatomic, copy) NSString* LastPushMessageCallbackID; | |
- (void) getLastPushMessage:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "LastPushMessage.h" | |
#import "AppDelegate.h" | |
@implementation LastPushMessage | |
@synthesize LastPushMessageCallbackID; | |
-(void)getLastPushMessage:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options { | |
self.LastPushMessageCallbackID = [arguments pop]; | |
NSString *LastPushMessageMessage = ((AppDelegate *)[[UIApplication sharedApplication] delegate]).LastPushMessageMessage; | |
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[LastPushMessageMessage stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; | |
[self writeJavascript: [pluginResult toSuccessCallbackString:self.LastPushMessageCallbackID]]; | |
} | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import | |
#import | |
@interface PushToken : CDVPlugin{ | |
NSString* callbackID; | |
} | |
@property (nonatomic, copy) NSString* callbackID; | |
- (void) getToken:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "PushToken.h" | |
#import "AppDelegate.h" | |
@implementation PushToken | |
@synthesize callbackID; | |
-(void)getToken:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options { | |
self.callbackID = [arguments pop]; | |
NSString *token = ((AppDelegate *)[[UIApplication sharedApplication] delegate]).token; | |
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[token stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; | |
if(token.length != 0) | |
{ | |
[self writeJavascript: [pluginResult toSuccessCallbackString:self.callbackID]]; | |
}else { | |
[self writeJavascript: [pluginResult toErrorCallbackString:self.callbackID]]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment