Question

Been trying to setup PushNotification with Phonegap 2 base app for the last couple weeks.

Here the steps I have done.

  1. Drag the PushNotification folder to the plugins folder:


    (source: joelchu.com)

  2. Follow the following instruction per documentation


    (source: joelchu.com)

  3. Add PushNotifcation / PushNotification to the Cordova.plist plugins section

  4. Modify the 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
    

FIRST ERROR

Unknown type name PushNotification

So I add this line just after the the last import:

#import "PushNotification.h"

That solves the problem.

NEXT PROBLEM

CDVPlugin.h file not found

So I add the following to User Header Search Paths - to all the build Settings:

$(CORDOVALIB)/Classes (recursive)

NEXT PROBLEM

Then it throws up more than 20 errors!


(source: joelchu.com)

What have I done wrong?

Been searching up and down everywhere. To my surprise it seems nobody has a problem.

More about my setup:

  • Phonegap 2.0
  • XCode 4.5 (preview 4)
  • iOS 6 base (the app build for iOS 5 and up)

Hope someone out there got an answer. Thank you.

(P.S. if the Phonegap PushNotification plugin is broken - they haven't update for 4 months already. Is there any free alternative? Thanks again)

I described the same problem on my blog.

FIXED (FOR NOW)

FIXED

/* 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 */

What I did - the first two

- (void)application:(UIApplication*)app

changed to

- (void) application:(UIApplication*)application

and I move the

#import "PushNotification.h"

to the head of the block (instead of just before the modify code - that cause @end not in context problem).

I haven't run the javascript interface yet. But all the error are gone.

Thanks.

Was it helpful?

Solution

Please try to revert your changes to the imports and then replace this block

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

by this line

#import <Cordova/CDVPlugin.h>

If that still does not work, you probably do not have a PhoneGap 2.0 project or you installed the wrong or multiple versions of the framework.

You can do the same in the "PushNotification.m" file: just replace the second import by #import <Cordova/JSONKit.h>.

Note that I haven't used that plugin yet, this is just from my experience with porting to 2.0 (I'm the author of the tab/navigation bar plugins).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top