我正在开发一个 Phonegap 应用程序。为了实现推送通知,我使用了一个 iOS 插件(来自 github 的 AppDelegate+Notification 和 PushNotification 文件),并且成功地在 APNs 上注册了设备并能够使用我的 MAC 机器的终端发送推送通知(使用 .php 文件)。
当我的应用程序处于活动模式时,我可以在“didReceiveRemoteNotification”块内接收通知。当我点击通知中心的任何通知时,也会执行此方法。
我面临的问题是:
- 当我的应用通过通知点击激活(处于后台状态)时,如何从“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 脚本函数?