Skip to content

Instantly share code, notes, and snippets.

@evantahler
Created April 22, 2016 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evantahler/bf5b2430544d37ad5aa0a0d3fdc12974 to your computer and use it in GitHub Desktop.
Save evantahler/bf5b2430544d37ad5aa0a0d3fdc12974 to your computer and use it in GitHub Desktop.
PhoneGap and Push
- (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"];
}
}
}
// 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);
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
self.token = [[[[deviceToken description]
stringByReplacingOccurrencesOfString: @" stringByReplacingOccurrencesOfString: @" " withString: @""];
NSLog(@"My token is: %@", self.token);
}
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;
}
}
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;
}
}
#import
#import
@interface LastPushMessage : CDVPlugin{
NSString* LastPushMessageCallbackID;
}
@property (nonatomic, copy) NSString* LastPushMessageCallbackID;
- (void) getLastPushMessage:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
@end
#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
#import
#import
@interface PushToken : CDVPlugin{
NSString* callbackID;
}
@property (nonatomic, copy) NSString* callbackID;
- (void) getToken:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
@end
#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