1

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

这是我已经完成的步骤。

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


    (来源:joelchu.com

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


    (来源:joelchu.com

  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
    

第一个错误

未知类型名称 PushNotification

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

#import "PushNotification.h"

这样就解决了问题。

下一个问题

未找到 CDVPlugin.h 文件

因此,我将以下内容添加到 User Header Search Paths - 所有构建设置中:

$(CORDOVALIB)/Classes (recursive)

下一个问题

然后它抛出了20多个错误!


(来源:joelchu.com

我做错了什么?

一直在到处寻找。令我惊讶的是,似乎没有人有问题。

更多关于我的设置:

  • 电话间隙 2.0
  • XCode 4.5(预览版 4)
  • iOS 6 基础版(适用于 iOS 5 及更高版本的应用程序)

希望有大神解答一下。谢谢你。

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

我在我的博客上描述了同样的问题。

已修复(暂时)

固定的

/* 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界面​​。但是所有的错误都消失了。

谢谢。

4

1 回答 1

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 的经验(我是标签/导航栏插件的作者)。

于 2012-09-11T14:47:49.187 回答