Unable to show push message inside app...

0 votes

Hi Rajeev,

I'm able to send push notifications to my device. But when i click on that notification it just opens the app. No message is shown inside the app. 

Code used by me:

if (application.applicationState == UIApplicationStateActive) {

        NSString *cancelTitle = @"Close";

        NSString *showTitle = @"Show";

        NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Some title"

                                                            message:message

                                                           delegate:self

                                                  cancelButtonTitle:cancelTitle

                                                  otherButtonTitles:showTitle, nil];

        [alertView show];

[alertView release];

    } else {

        //Do stuff that you would do if the application was not active

       

    }

 

But unable to show my message with the help of above code. Above code only works when my app is open that is in foreground state than only this alert gets displayed else not.

Please help.

 

Thanks.

asked Jul 21, 2016 in iOS by prabhjassinghbajwa (25 points)

1 Answer

0 votes
 
Best answer
Hi Prabhjas,

It is happening because of your if-else structure in your code. When the app is in Foreground then your if block condition is true and hence you are getting the alert. But when the app is in Background and you click on the push alert then your else block executes and there is no code handle it so your are not able to see anything.

I hope it helps.

Regards,

Rajeev
answered Jul 22, 2016 by rajeev.etc (1,660 points) 1 flag
selected Jul 22, 2016 by prabhjassinghbajwa
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.
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...