Timo Ernst
Timo Ernst

Reputation: 16003

Push Messages for PhoneGap

I created an app for iPhone and Android using Phonegap.

Now I wanted to add push functionality which already works pretty good.

When a push message arrives on my iPhone I get a message on the homescreen. If I swipe it, iOS will open my application. - So far so good.

Now, within my PhoneGap app I need to check what that message actually says in order to open the correct view within my app via JavaScript.

I know there are quite some posts about this but I couldn't find some clear answers to these questions:

Upvotes: 2

Views: 280

Answers (2)

Timo Ernst
Timo Ernst

Reputation: 16003

I found a really easy solution without using a framework.

I simply added the following code to the very end of the method didFinishLaunchingWithOptions (Right before the return statement):

if (launchOptions != nil)
{
    NSDictionary* dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (dictionary != nil)
    {
        NSLog(@"Launched from push notification: %@", dictionary);
        [self addMessageFromRemoteNotification:dictionary updateUI:NO];
    }
}

Also, I added this new method to my AppDelegate.m which gets the payload from the push message:

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
    NSLog(@"Received notification: %@", userInfo);
    [self addMessageFromRemoteNotification:userInfo updateUI:YES];
}

(The NSLog calls above will show you what's inside the push message)

As a last step, I added this listener to do what ever I want with the payload:

- (void)addMessageFromRemoteNotification:(NSDictionary*)userInfo updateUI:(BOOL)updateUI
{
 // do what ever you want with userInfo
}

If you want to do an additional JavaScript call with this info, I recommend using:

[self.viewController.webView stringByEvaluatingJavaScriptFromString:@"callSomeJSFunction()"];

Of course you can also pass arguments as strings into that JS function if you want.

Upvotes: 0

Noah Borg
Noah Borg

Reputation: 762

By Pushwoosh ID you most probably mean Pushwoosh App Id or Application Code. It's an ID of your application in Pushwoosh Control Panel (current format is XXXXX-XXXXX). You will see it as soon as you add a new app in Pushwoosh.

There was quite an extensive blog post made by Holly Schinsky on easy PhoneGap integration with Pushwoosh

http://devgirl.org/2012/12/04/easy-phonegap-push-notifications-with-pushwoosh/

It should be very helpful for all PhoneGap developers aiming to integrate push notifications into their apps.

Upvotes: 3

Related Questions