How to handle push notifications if the application is already running?

How do we handle push notifications if the application is already running ? I want to show an alert if the application is running (instead of a push notification alert). If the application is not running, then show a push notification alert.

Also, if i send a payload to APNs, how do I cancel the payload?

-------------Problems Reply------------

You can implement application:didReceiveRemoteNotification:

Here is a possible sample code:

- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSString *message = nil;
id alert = [userInfo objectForKey:@"alert"];
if ([alert isKindOfClass:[NSString class]]) {
message = alert;
} else if ([alert isKindOfClass:[NSDictionary class]]) {
message = [alert objectForKey:@"body"];
}
if (alert) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title"
message:@"AThe message." delegate:self
cancelButtonTitle:@"button 1"
otherButtonTitles:@"button", nil];
[alertView show];
[alertView release];
}

You can check for the state of the UIApplication. Just do a check like this

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{

UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive)
{

UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"xxx" message:yourMessage delegate:self cancelButtonTitle:@"Done" otherButtonTitles: @"Anzeigen", nil] autorelease];
[alert setTag: 2];
[alert show];
}
else {
// Push Notification received in the background
}
}

"alert" key will not be there directly under the userInfo dictionary, you need to get another dictionary with name "aps" and then get the "alert" or "body" from "aps" dictionary.

Iterating the 3 levels of payload

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

for (id key in userInfo) {
NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
NSString *message = nil;

NSDictionary *aps = [NSDictionary dictionaryWithDictionary:(NSDictionary *) [userInfo objectForKey:key] ];
for (id key1 in aps){
NSLog(@"key1: %@", key1);
id alert = [aps objectForKey:key1];
if ([alert isKindOfClass:[NSDictionary class]]) {
message = [alert objectForKey:@"body"];
NSLog(@"body: %@, value: %@", key1, message);
message = [alert objectForKey:@"loc-args"];
NSLog(@"loc-args: %@, value: %@", key1, message);
NSArray *args = (NSArray *) [alert objectForKey:@"loc-args"] ;
for (id key2 in args){
NSLog(@"key2: %@, value: ", key2);
}
message = [alert objectForKey:@"action-loc-key"];
NSLog(@"action-loc-key: %@, value: %@", key1, message);

}
else if ([alert isKindOfClass:[NSArray class]]) {
for (id key2 in key1){
NSLog(@"key2: %@, value: %@", key2, [key1 objectForKey:key2]);
}
}
else if([key1 isKindOfClass:[NSString class]]) {
message = [aps objectForKey:key1];
NSLog(@"key1: %@, value: %@", key1, message);
}

}
}

}

The result is:

2012-01-27 20:38:09.599 SPush[4181:707] key: aps, value: {
alert = {
"action-loc-key" = Open;
body = test;
"loc-args" = (
1000,
2000
);
};
badge = 0;
"content-available" = 10;
sound = default;
}
2012-01-27 20:38:13.133 SPush[4181:707] key1: alert
2012-01-27 20:38:13.134 SPush[4181:707] body: alert, value: test
2012-01-27 20:38:13.137 SPush[4181:707] loc-args: alert, value: (
1000,
2000
)
2012-01-27 20:38:13.138 SPush[4181:707] key2: 1000, value:
2012-01-27 20:38:13.139 SPush[4181:707] key2: 2000, value:
2012-01-27 20:38:13.140 SPush[4181:707] action-loc-key: alert, value: Open
2012-01-27 20:38:13.141 SPush[4181:707] key1: sound
2012-01-27 20:38:13.143 SPush[4181:707] key1: sound, value: default
2012-01-27 20:38:13.144 SPush[4181:707] key1: badge
2012-01-27 20:38:13.145 SPush[4181:707] key1: badge, value: 0
2012-01-27 20:38:13.146 SPush[4181:707] key1: content-available
2012-01-27 20:38:13.147 SPush[4181:707] key1: content-available, value: 10

Category:iphone Views:0 Time:2009-10-12

Related post

  • how can I handle push notification when my app is not running 2011-01-24

    how can I handle push notification when my app is not running i am developing for ios3 .please help --------------Solutions------------- A short example: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)l

  • location request by iOS push notification (when the application is NOT running on backgorund) 2012-01-23

    is it possible to send iOS push notification to get location when the application is not running in background? exactly as Find My iPhone do it.. --------------Solutions------------- UIRemoteNotification is your friend. 1) Register your app for Remot

  • Handling Push notification 2011-02-08

    I am working with push notification, when the application is opened/or in backgroung process then it is no problem for me to receive the push notification, the problem occurs when i quit my application, it is not calling my delegate. Why? -----------

  • Push Notifications in your application 2010-01-06

    I am wonder how to enable push notifications in my application.Actually i have some messages that server sends me to display. Whenever there is a new message i want push notification to be sent to me. Kindly guide that how i can enable push notificat

  • alert for allow or dont allow Apple push notification in iphone application 2011-03-02

    I am using Apple Push Notification in my application. when my application is installed on device i want application to ask user if they want allow application to use notification or not, how can i implement so? something like this application. Thanks

  • Windows Phone 8 Why am I receiving Update Push Notifications to Update applications I have previously uninstalled? 2013-11-12

    Windows Phone 8.0.10501.127 - HTC 8X Why am I receiving Update Push Notifications to Update applications I have previously uninstalled? For kicks, I let it update... and it indeed downloaded an update and installed it, however the uninstalled applica

  • Handling Push Notification While App is Open 2010-11-13

    I have my Push Notification running. It works. I receive a notification and use application:didReceiveRemoteNotification: to get the incoming data and then send the user to the necessary screen. Problem is, if you are using the App and a notification

  • Push notification and launch application for Iphone4 2010-12-06

    I have an APNS enabled application. In Iphone4 when user press the HOME button application remains in cache. Now in this condition when a push notification received after clicking on the VIEW button the application starts running from the cache!!!. I

  • Handling push notification effectively 2011-03-01

    I have a query regarding push notification. Whenever a push comes and user is working on some XYZ controller and push requires another controller to be initialized and shown. Now should I show the push controller on top of the current active controll

  • how can i do push notification in iphone application? 2009-06-16

    How can i use push notification in my iphone application.can u please help me by given some source or instruction to include my application. --------------Solutions------------- http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual

  • iOS - Handling pushed notifications with APNS 2010-07-02

    Considering that I receive a pushed notification on my iPhone. What happens: If the application is started: is there a way to get the payload? Do I see the notification on my screen? If the application is not started, is there a way to get the payloa

  • "Push notification" - feedback, uninstall application 2011-02-24

    Apple push notification - Feedback Service How do you know when the user uninstalls your application? so that you can remove their devicetoken from the push server. --------------Solutions------------- You simply don't know. You can grab device ident

  • Push notification for Android applications 2011-04-07

    Do we have any equivalent service of Push Notification (in iPhone) for Android applications? --------------Solutions------------- Yes, here is a good tutorial with all code you need: http://blog.boxedice.com/2010/10/07/android-push-notifications-tuto

  • Push notification on iphone applications 2011-06-07

    I want to send advertisements to the application users using Push notification but i don't know is it possible or not.Can anyone help me to solve this issue. --------------Solutions------------- If you want to send advertisements to your users, I wou

  • how to handle push notification by the kind of notification 2011-07-06

    i want to handle the notification, in my app i have 2 tabs(friends,jobs) with navigation controller,and a barbutton on each navigation controller named 'notification'(on click it displays tableview). On notification, if its job related it shud open t

  • Handle push notification when device is idle 2011-09-12

    I have implemented Apple Push notification service. I have a requirement that I need to execute code in the device when a notification is received. When I followed the documentation, I found that there are two ways to do this application:didReceiveRe

  • WP7 push notifications received in application 2011-11-21

    I'm wondering if it's possible to receive toast push-notifications (or any push-notification for that matter) on wp7 in my code when the notification was sent when my application was closed. For example. Right now I have a demo-application that incre

  • Blackberry: Push Notification in Blackberry application 2011-12-06

    Has anybody implemented push notifications for a BlackBerry application? --------------Solutions------------- Yes, Blackberry provides an example application with the SDK and you can use the material on the blackberry website to see how to implement

  • Android: push notification for my application which is not in android market 2012-02-01

    I developed an application in android and i want to do push notification. I learned that this can be done by c2dm. Is there any way that i can test c2dm in my local system path of the app itself. can any body help. --------------Solutions------------

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.067 (s). 11 q(s)