2

I'm able to send push notifications to my IOS 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.

  • The code you have is not related to push notification.The code you have just shows an alert view when the app is in active state. What are you trying to do ? – Teja Nandamuri Jul 21 '16 at 19:26
  • I'm receiving push notifications on my IOS device. I just want when i click on that push message than it should open my app and display that message to user thats it. Would be happy if you guide in detailed manner with proper code. @Teja Nandamuri – Prabhjas Singh Bajwa Jul 21 '16 at 19:30

5 Answers5

3

When application is totally killed get notification code

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (launchOptions != nil)
{
     //opened from a push notification when the app is closed
    NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (userInfo != nil)
    {
         NSLog(@"userInfo->%@",[userInfo objectForKey:@"aps"]);
         //write you push handle code here

    }

}
}

For more go through this link: Handling Push Notifications when App is Terminated

Community
  • 1
  • 1
Chandan
  • 697
  • 6
  • 15
1

From the apple documentation

https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/Introduction.html

"When your app must be launched to receive a notification, UIKit includes the UIApplicationLaunchOptionsLocalNotificationKey or UIApplicationLaunchOptionsRemoteNotificationKey key in the launch options dictionary passed to your app delegate’s application:willFinishLaunchingWithOptions: and application:didFinishLaunchingWithOptions: methods. The presence of those keys lets you know that there is notification data waiting to be handled and gives you a chance to configure your app’s interface appropriately. You do not need to handle the notification in these methods, though. After your app is running, UIKit calls other methods of your app delegate, such as the application:didReceiveLocalNotification: method, to give you an opportunity to process the notification data. Which methods are called depends on which methods you implemented and whether the user interacted with the system UI for the message."

So check if your app has been launched due to a notification and if so display the dialog.

Gruntcakes
  • 36,166
  • 41
  • 170
  • 346
1

I think there is one easy solution.

  1. You can store some flag in the app once when you receive notification you can check how to do it here
  2. After that on the you have to detect when you come from background maybe like here

If you want to show the alert only when you open the application from the notification then maybe you this is your solution

Community
  • 1
  • 1
m1sh0
  • 1,996
  • 1
  • 12
  • 17
  • 50% I got a solution. Used this code: else if(application.applicationState == UIApplicationStateInactive){ //app is transitioning from background to foreground (user taps notification), do what you need when user taps here } But if i'm using for UiapplicationStateBackground it doesn't works for me. @m1sh0 – Prabhjas Singh Bajwa Jul 22 '16 at 04:12
1

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.

Hasya
  • 8,834
  • 4
  • 28
  • 44
-2

The best way to handle things like this is to use deep linking within your APN. That will let you embed data that can then be handled within your app and direct the user to a specific event.

Otherwise, you are limited to using the ApplicationDidEnterForeground method from your app delegate. Just put your alertView code in there and anytime your application is brought into the foreground that will run.

TheValyreanGroup
  • 3,407
  • 2
  • 10
  • 30
  • Could you please provide a solution in detailed manner with code @TheValyreanGroup – Prabhjas Singh Bajwa Jul 21 '16 at 19:20
  • Here is a very good write up on how to do it. You need 3 things... 1-Custom URL Scheme 2-Deep link within APN 3-Code to handle the deep link from the APN https://blog.mixpanel.com/2015/04/21/guide-setting-up-deep-linking-in-ios-and-android/ – TheValyreanGroup Jul 21 '16 at 19:40
  • Appreciate your help but that tutorial gives knowledge how to open specific screen for push notifications using deep linking. But I want to show my push notification on home screen only. @TheValyreanGroup – Prabhjas Singh Bajwa Jul 21 '16 at 19:50
  • 1) deep linking is totally unnecessary 2)You can't even deep link from a push anyway. 3) The payload within the APN is directly available when the app is launched so whats the point of attempting deep linking? 4) Its not true that he's limited to ApplicationDidEnterForeground, and what has that even got to do with push notifications and the question anyway? – Gruntcakes Jul 21 '16 at 20:07