I'm working on a Phonegap application. For implementing push notification, I used a plugin for iOS (AppDelegate+Notification & PushNotification files from github) and was successful able to register device on APNs and able to send push notification using my MAC machine's terminal(using .php file).
When my application is in active mode, I'm able to receive notification inside "didReceiveRemoteNotification" block. And also this method get executed while I tap on any notification from notification center.
The problem I'm facing is :
- How can I get the Notification.js function call from "didReceiveRemoteNotification" while my app activates(its in background state) from the notification click?
Because the app is able to call the function :
1. AppDelegate+Notification.m
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"didReceiveRemoteNotification AppDelegate+Notification.h :%@", userInfo);
PushNotification* pushHandler = [self.viewController getCommandInstance:@"PushNotification"];
NSMutableDictionary* mutableUserInfo = [userInfo mutableCopy];
// Get application state for iOS4.x+ devices, otherwise assume active
UIApplicationState appState = UIApplicationStateActive;
if([application respondsToSelector:@selector(applicationState)]) {
appState = application.applicationState;
}
if(appState == UIApplicationStateActive) //Active state
{
NSLog(@"UIApplicationStateActive");
[mutableUserInfo setValue:@"1" forKey:@"applicationStateActive"];
[pushHandler didReceiveRemoteNotification:mutableUserInfo];
}
else // BG state
{
NSLog(@"UIApplicationStateBackground");
[mutableUserInfo setValue:@"0" forKey:@"applicationStateActive"];
[mutableUserInfo setValue:[NSNumber numberWithDouble: [[NSDate date] timeIntervalSince1970]] forKey:@"timestamp"];
[pushHandler.pendingNotifications addObject:mutableUserInfo];
}
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}
2. PushNotification.m
- (void)didReceiveRemoteNotification:(NSDictionary*)userInfo {
NSLog(@"didReceiveRemoteNotification : %@", userInfo);
// Converting Dict to NSString in JSON format using NSJSONSerialization as per Cordova 2.4.0
NSError* error = nil;
NSString *jsStatement = nil;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:userInfo options:0 error: &error];
if (error != nil)
{
jsStatement = [NSString stringWithFormat:@"window.plugins.pushNotification.notificationCallback({error: %@});",[error localizedDescription]];
}
else
{
jsStatement = [NSString stringWithFormat:@"window.plugins.pushNotification.notificationCallback(%@);", [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]];
}
[self writeJavascript:jsStatement];
}
3. PushNotification.js
PushNotification.prototype.notificationCallback = function(notification) {
alert ('JS notificationCallback');
}
when it is already in active state and notification comes. The last step's alert get fired (JS notificationCallback)
Or in simple way, how can I call java script function from my ios plugin?
It looks to me like you're using this plugin: https://github.com/mgcrea/cordova-push-notification (If this is incorrect, please provide us with the link to the correct plugin)
If the plugin I've referenced above is correct, I think you missed the step where you update the AppDelegate.m:
I seem to recall that, last year, I had problems with the documentation for the Urban Airship version of plugin... I don't remember if just the first two lines need to be added, or if that whole block was suppose to be added.
At any rate, I believe the problem is with the didFinishLaunchingWithOptions method inside of AppDelegate.m.