0

我正在开发一个 Phonegap 应用程序。为了实现推送通知,我使用了一个 iOS 插件(来自 github 的 AppDelegate+Notification 和 PushNotification 文件),并且成功地在 APNs 上注册了设备并能够使用我的 MAC 机器的终端发送推送通知(使用 .php 文件)。

当我的应用程序处于活动模式时,我可以在“didReceiveRemoteNotification”块内接收通知。当我点击通知中心的任何通知时,也会执行此方法。

我面临的问题是:

  1. 当我的应用通过通知点击激活(处于后台状态)时,如何从“didReceiveRemoteNotification”获取 Notification.js 函数调用?

因为应用程序能够调用该函数:

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');
}

当它已经处于活动状态并且通知到来时。最后一步的警报被触发(JS notificationCallback)

或者以简单的方式,如何从我的 ios 插件中调用 java 脚本函数?

4

2 回答 2

0

我想你要找的是[webView stringByEvaluatingJavaScriptFromString].

您可以像这样调用您的 javascript 回调:

NSString *jsCallback = [NSString stringWithFormat:@"%@(%@);", @"window.plugins.pushNotification.notificationCallback", @"your notification message"];
[self.webView stringByEvaluatingJavaScriptFromString:jsCallback];
于 2014-04-03T21:23:01.177 回答
0

在我看来您正在使用此插件:https ://github.com/mgcrea/cordova-push-notification (如果这不正确,请向我们提供正确插件的链接)

如果我上面引用的插件是正确的,我认为你错过了更新 AppDelegate.m 的步骤:

为了支持启动通知(应用程序从远程通知开始),您必须在返回 YES 之前添加以下块 - (BOOL) application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions;

[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;

我似乎记得,去年,我对 Urban Airship 版本的插件的文档有问题......我不记得是否只需要添加前两行,或者整个块是否应该是添加。

无论如何,我认为问题出在 AppDelegate.m 中的 didFinishLaunchingWithOptions 方法上。

于 2013-09-10T22:41:40.730 回答