I have created a Phonegap 3.0 app with Push Notification. I primarily followed the blogs here. The Android push is working perfectly. But I am having problems when receiving push notifications on iOS. When the app is running in the background, the alert comes up and the user taps it and the app opens and shoots off an ajax request to go retrive the message.
However, when the app is not running, neither in foreground or background, the alert comes up, and when it is clicked, the app opens but immediately freezes.
There is a similar question here which I thought would resolve my problem. However, the code in didFinishLaunchingWithOptions() in my Phonegap 3.0 project looks nothing like the code in the question. My code in AppDelegate.m is as follows:
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
#if __has_feature(objc_arc)
self.window = [[UIWindow alloc] initWithFrame:screenBounds];
#else
self.window = [[[UIWindow alloc] initWithFrame:screenBounds] autorelease];
#endif
self.window.autoresizesSubviews = YES;
#if __has_feature(objc_arc)
self.viewController = [[MainViewController alloc] init];
#else
self.viewController = [[[MainViewController alloc] init] autorelease];
#endif
// Set your app's start page by setting the <content src='foo.html' /> tag in config.xml.
// If necessary, uncomment the line below to override it.
// self.viewController.startPage = @"index.html";
// NOTE: To customize the view's frame size (which defaults to full screen), override
// [self.viewController viewWillAppear:] in your view controller.
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
I thought perhaps the reason for the different code for didFinishLaunchingWithOptions was because Im using Phonegap 3.0. But I created a project in 2.9 and the code is the same as 3.0. The code in that other questionis as follows:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//commented out because it makes the app crash at startup with local notification...
/*NSArray *keyArray = [launchOptions allKeys];
if ([launchOptions objectForKey:[keyArray objectAtIndex:0]]!=nil)
{
NSURL *url = [launchOptions objectForKey:[keyArray objectAtIndex:0]];
self.invokeString = [url absoluteString];
NSLog(@"Mosa_fr_en-busi launchOptions = %@",url);
}*/
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
The javascript to deal with the notifications is like so:
onDeviceReady: function() {
var pushNotification = window.plugins.pushNotification;
if (window.device.platform == 'android' || window.device.platform == 'Android') {
pushNotification.register(app.successHandler, app.errorHandler,{"senderID":"475226855592","ecb":"app.onNotificationGCM"});
}
else{
//so its apple
pushNotification.register(app.tokenHandler,app.errorHandler,{"badge":"true","sound":"true","alert":"true","ecb":"app.onNotificationAPN"});
}
},
and:
onNotificationAPN: function(event) {
console.log(event.article_id);
if ( event.article_id )
{
console.log('in event.article_id');
window.location.hash = "article/"+event.article_id;
//localStorage.payload =// event.payload
}
var pushNotification = window.plugins.pushNotification;
if (event.alert) {
console.log('in event.alert');
navigator.notification.alert(event.alert);
}
if (event.badge) {
console.log('in event.badge');
console.log("Set badge on " + pushNotification);
pushNotification.setApplicationIconBadgeNumber(this.successHandler, event.badge);
}
if (event.sound) {
console.log('in event.sound');
var snd = new Media(event.sound);
snd.play();
}
},
Is it actually possible to get Phonegap ios push notifications working fully?