我正在开发一个Phonegap应用程序。为了实现推送通知,我使用了iOS插件(来自github的AppDelegate + Notification& PushNotification文件),并且成功地能够在APN上注册设备并能够使用我的MAC机器终端发送推送通知(使用.php文件)。 / p>
当我的应用程序处于活动模式时,我能够在“didReceiveRemoteNotification”块中接收通知。当我点击通知中心的任何通知时,也会执行此方法。
我面临的问题是:
因为应用程序可以调用该函数:
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脚本函数?
答案 0 :(得分:0)
我认为您使用此插件:https://github.com/mgcrea/cordova-push-notification(如果这不正确,请向我们提供指向正确插件的链接)
如果上面引用的插件是正确的,我想你错过了更新AppDelegate.m的步骤:
为了支持启动通知(应用程序从远程通知开始),您必须在 - (BOOL)应用程序中添加以下块:(UIApplication *)应用程序didFinishLaunchingWithOptions:(NSDictionary *)launchOptions,就在返回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;
我似乎记得,去年,我对Urban Airship版插件的文档有问题...我不记得是否只需要添加前两行,或者整个块是否是假设要加入。
无论如何,我认为问题在于AppDelegate.m中的didFinishLaunchingWithOptions方法。
答案 1 :(得分:0)
我认为你要找的是[webView stringByEvaluatingJavaScriptFromString]
。
你可以像这样调用你的javascript回调:
NSString *jsCallback = [NSString stringWithFormat:@"%@(%@);", @"window.plugins.pushNotification.notificationCallback", @"your notification message"];
[self.webView stringByEvaluatingJavaScriptFromString:jsCallback];