Handling Push Notifications when App is NOT running (or Totally Killed)
I'm posting this solution as it worked for me.
Go to your AppDelegate.m file.
Step 1:
Write this code inside this function:
-(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (localNotif) {
NSString *cancelTitle = @"Close";
NSString *showTitle = @"OK";
NSString *message = [[localNotif valueForKey:@"aps"] valueForKey:@"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received"
message:message
delegate:self
cancelButtonTitle:cancelTitle
otherButtonTitles:showTitle, nil];
[alertView show];
}
}
Step 2:
Insert This full code:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"%s..userInfo=%@",__FUNCTION__,userInfo);
/**
* Dump your code here according to your requirement after receiving push
*/
if (application.applicationState == UIApplicationStateActive) {
NSString *cancelTitle = @"Close";
NSString *showTitle = @"OK";
NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received"
message:message
delegate:self
cancelButtonTitle:cancelTitle
otherButtonTitles:showTitle, nil];
[alertView show];
}
else if(application.applicationState == UIApplicationStateBackground){
//app is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here
NSString *cancelTitle = @"Close";
NSString *showTitle = @"OK";
NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received"
message:message
delegate:self
cancelButtonTitle:cancelTitle
otherButtonTitles:showTitle, nil];
[alertView show];
}
else if(application.applicationState == UIApplicationStateInactive){
//app is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here
NSString *cancelTitle = @"Close";
NSString *showTitle = @"OK";
NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received"
message:message
delegate:self
cancelButtonTitle:cancelTitle
otherButtonTitles:showTitle, nil];
[alertView show];
}
}
This whole code will work whether app is Active, InActive or Totally Killed. It will give you AlertView for push messages.