phonegap 2.0推送通知插件iOS设置(一个错误接着另一个)

时间:2012-09-11 11:02:58

标签: cordova push-notification ios6 xcode4.5

过去几周一直尝试使用Phonegap 2基础应用设置PushNotification。

这是我所做的步骤。

  1. 将PushNotification文件夹拖到plugins文件夹:

    http://joelchu.com/wp-content/uploads/2012/09/1.png

  2. 按照以下说明进行操作

    http://joelchu.com/wp-content/uploads/2012/09/2.png

  3. 将PushNotifcation / PushNotification添加到Cordova.plist插件部分

  4. 修改AppDelegate.m

    - (void) dealloc
    {
        [super dealloc];
    }
    
    /* START PUSH MODIFCATION */
    
    #pragma PushNotification delegation
    
    - (void)application:(UIApplication*)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
    {
        PushNotification* pushHandler = [self.viewController getCommandInstance:@"PushNotification"];
        [pushHandler didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
    }
    
    - (void)application:(UIApplication*)app didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
    {
        PushNotification* pushHandler = [self.viewController getCommandInstance:@"PushNotification"];
        [pushHandler didFailToRegisterForRemoteNotificationsWithError:error];
    }
    
    - (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)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;
        }
    
        [mutableUserInfo setValue:@"0" forKey:@"applicationLaunchNotification"];
        if (appState == UIApplicationStateActive) {
            [mutableUserInfo setValue:@"1" forKey:@"applicationStateActive"];
            [pushHandler didReceiveRemoteNotification:mutableUserInfo];
        } else {
            [mutableUserInfo setValue:@"0" forKey:@"applicationStateActive"];
            [mutableUserInfo setValue:[NSNumber numberWithDouble: [[NSDate date] timeIntervalSince1970]] forKey:@"timestamp"];
            [pushHandler.pendingNotifications addObject:mutableUserInfo];
        }
    }
    
    /* END PUSH MODIFICATION */
    
    @end
    
  5. FIRST ERROR

      

    未知类型名称PushNotification

    所以我在最后一次导入后添加了这一行:

    #import "PushNotification.h"
    

    解决了这个问题。

    下一个问题

      

    找不到CDVPlugin.h文件

    所以我将以下内容添加到用户标题搜索路径 - 所有构建设置:

    $(CORDOVALIB)/Classes (recursive)
    

    下一个问题

    然后它会引发超过20个错误!

    http://joelchu.com/wp-content/uploads/2012/09/3.png

    我做错了什么?

    到处搜寻。令我惊讶的是,似乎没有人有问题。

    有关我的设置的更多信息:

    • Phonegap 2.0
    • XCode 4.5(预览4)
    • iOS 6基础(iOS 5及更高版本的应用程序版本)

    希望有人在那里得到答案。谢谢。

    (P.S。如果Phonegap PushNotification插件坏了 - 他们已经4个月没有更新了。有没有免费替代品?再次感谢)

    我在my blog上描述了同样的问题。

    已修复(现在)

    固定

    /* START BLOCK */
    
    #pragma PushNotification delegation
    
    - (void) application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
    {
        PushNotification* pushHandler = [self.viewController getCommandInstance:@"PushNotification"];
        [pushHandler didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
    }
    
    - (void) application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
    {
        PushNotification* pushHandler = [self.viewController getCommandInstance:@"PushNotification"];
        [pushHandler didFailToRegisterForRemoteNotificationsWithError:error];
    }
    
    - (void) application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)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;
        }
    
        [mutableUserInfo setValue:@"0" forKey:@"applicationLaunchNotification"];
        if (appState == UIApplicationStateActive) {
            [mutableUserInfo setValue:@"1" forKey:@"applicationStateActive"];
            [pushHandler didReceiveRemoteNotification:mutableUserInfo];
        } else {
            [mutableUserInfo setValue:@"0" forKey:@"applicationStateActive"];
            [mutableUserInfo setValue:[NSNumber numberWithDouble: [[NSDate date] timeIntervalSince1970]] forKey:@"timestamp"];
            [pushHandler.pendingNotifications addObject:mutableUserInfo];
        }
    }
    
    /* STOP BLOCK */
    

    我做了什么 - 前两个

    - (void)application:(UIApplication*)app

    更改为

    - (void) application:(UIApplication*)application

    我移动了

    #import "PushNotification.h"

    到块的头部(而不是在修改代码之前 - 导致@end不在上下文问题中)。

    我还没有运行javascript界面​​。但所有错误都消失了。

    感谢。

1 个答案:

答案 0 :(得分:2)

请尝试将更改还原为导入,然后替换此块

#ifdef CORDOVA_FRAMEWORK
    #import <Cordova/CDVPlugin.h>
#else
    #import "CDVPlugin.h"
#endif

通过这一行

#import <Cordova/CDVPlugin.h>

如果仍然无效,您可能没有PhoneGap 2.0项目,或者您安装了错误或多个版本的框架。

您可以在“PushNotification.m”文件中执行相同操作:只需将第二次导入替换为#import <Cordova/JSONKit.h>

请注意,我还没有使用过这个插件,这只是我从移植到2.0的经验(我是标签/导航栏插件的作者)。