0

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 :

  1. 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?

Mobiletainment
  • 22,201
  • 9
  • 82
  • 98
Lalit Paliwal
  • 723
  • 2
  • 10
  • 19

2 Answers2

0

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:

In order to support launch notifications (app starting from a remote notification), you have to add the following block inside - (BOOL) application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions, just before the return YES;

[self.window addSubview:self.viewController.view];
[self.window makeKeyAndVisible];

/* START BLOCK */

// PushNotification - Handle launch from a push notification
NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if(userInfo) {
    PushNotification *pushHandler = [self.viewController getCommandInstance:@"PushNotification"];
    NSMutableDictionary* mutableUserInfo = [userInfo mutableCopy];
    [mutableUserInfo setValue:@"1" forKey:@"applicationLaunchNotification"];
    [mutableUserInfo setValue:@"0" forKey:@"applicationStateActive"];
    [pushHandler.pendingNotifications addObject:mutableUserInfo];
}

/* STOP BLOCK */

return YES;

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.

Ed Meacham
  • 543
  • 1
  • 5
  • 19
  • Sorry, I forgot to mention that I already written this code into didFinishLaunchingWithOptions and its going to fired only when app is not running in the background(I removed app from active background apps list). But my case is app is in background apps list and a notification comes and app activates from notification click. How to call java script function then? – Lalit Paliwal Sep 11 '13 at 04:43
  • Are you trying figure out how to execute PushNotification.prototype.notificationCallback when the app activates when you "touch" a push notification? – Ed Meacham Sep 11 '13 at 15:32
  • Yes I want it to be execute in response to touch a push notification. Or I want to call any java script function from native function "didReceiveRemoteNotification" which is in AppDelegate+Notification.m. – Lalit Paliwal Sep 12 '13 at 10:07
  • You should be able to interact with your javascript plugins from the objective-c side by doing something similar to what's being done in your didReceiveRemoteNotification method in PushNotification.m: ` jsStatement = [NSString stringWithFormat:@"window.plugins.pushNotification.notificationCallback({error: %@});",[error localizedDescription]]; [self writeJavascript:jsStatement];` – Ed Meacham Sep 13 '13 at 21:19
  • its not getting called. Could you please provide me whole working code? – Lalit Paliwal Sep 17 '13 at 13:57
0

I think what you're looking for is [webView stringByEvaluatingJavaScriptFromString].

You can call your javascript callback like this:

NSString *jsCallback = [NSString stringWithFormat:@"%@(%@);", @"window.plugins.pushNotification.notificationCallback", @"your notification message"];
[self.webView stringByEvaluatingJavaScriptFromString:jsCallback];
Mobiletainment
  • 22,201
  • 9
  • 82
  • 98
  • i get an error with the `webview` saying `not found on object of type "appdelegate"`. Also the plugin i'm using is `org.apache.cordova.pushplugin`, any experience with that one? I want to open a specific page in index.html based on the notification. – allwynmasc Oct 13 '14 at 05:08