0

My problem is, if app is in background and notification arrives and I opened the app from icon; app restores it states but I want to update the screen data in this case. Is there any way to update the data in background when notification arrives?

Here is the code which I'm using for tackling this case:

ViewController.m file code:

 - (void)viewDidLoad {
        [super viewDidLoad];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(appIsComingFromBackground:)
                                                     name:UIApplicationDidBecomeActiveNotification
                                                   object:nil];
        // Do any additional setup after loading the view, typically from a nib.
    }
    - (void) appIsComingFromBackground:(NSNotification *) note {
        // code
        NSString *hasMessage = [[NSUserDefaults standardUserDefaults] objectForKey:@"alertmsg"];
        if([hasMessage length]!=0)
        {
            _labelText.text = hasMessage;
             [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"alertmsg"];
        }
        else{
             _labelText.text = @"";
        }
    } 

AppDelegate.m file code:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{

    if (application.applicationState == UIApplicationStateActive) {

    }
    else if(application.applicationState == UIApplicationStateBackground || application.applicationState == UIApplicationStateInactive)
    {
        [[NSUserDefaults standardUserDefaults] setObject:notification.alertTitle forKey:@"alertmsg"];
    }
    NSLog(@"Alert Message: %@", notification.alertTitle);
    NSLog(@"Alert Body: %@", notification.alertBody);
}
Ekta Padaliya
  • 5,743
  • 3
  • 39
  • 51
Muhammad Umair
  • 583
  • 11
  • 32
  • 1
    If the app is suspended then no app delegate method is called when a local notification is shown. – Paulw11 Sep 26 '16 at 11:53
  • @Paulw11 so there is no solution for this problem? – Muhammad Umair Sep 26 '16 at 11:58
  • Is there any way we can update data if user didn't open app from notification but it open from icon? – Muhammad Umair Sep 26 '16 at 11:59
  • The appropriate action is for your app to check for new data or take whatever action is appropriate no matter how it was launched. Tapping a notification is usually to take the user to a specific place in your app, such as a particular message or event – Paulw11 Sep 26 '16 at 12:03
  • @Paulw11 Problem is when i opened app from icon; `didReceiveLocalNotification` isn't calling. if this method called then my problem will be solved. Because u have to update the data from local notification Payload. – Muhammad Umair Sep 26 '16 at 12:11
  • You need to manage things so that you don't need the information from the notification, since if the user doesn't tap the notification then the data is never delivered to your app. – Paulw11 Sep 26 '16 at 12:16
  • You mean in this case there is no way; we can get data from notification? :( – Muhammad Umair Sep 26 '16 at 12:18
  • Correct. From https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW4 "If the app icon is clicked on a computer running macOS, the app calls the delegate’s applicationDidFinishLaunching: method in which the delegate can obtain the remote-notification payload. If the app icon is tapped on a device running iOS, the app calls the same method, but furnishes no information about the notification." – Paulw11 Sep 26 '16 at 12:19
  • Thanks for your precious time. :) – Muhammad Umair Sep 26 '16 at 12:20

2 Answers2

3

Application is NOT Running

When the app is not running, users see notifications in the following ways, depending on the notification settings:

  • Displaying an alert or banner

  • Badging the app icon

  • Playing a sound

By tapping on action button of the notification, users will launch the app. In this case, the application:didFinishLaunchingWithOptions: method of the application delegate is called.

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.

// Handle launching from a notification
UILocalNotification *locationNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (locationNotification) {
    // Set icon badge number to zero
    application.applicationIconBadgeNumber = 0;
}

return YES;
}

Applicaton is Running in Foreground

If the app is running while the notification is delivered, there is no alert displayed on screen. The application automatically calls its delegate’s application:didReceiveLocalNotification: method.

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
    
}

// Request to reload table view data
[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self];

// Set icon badge number to zero
application.applicationIconBadgeNumber = 0;
 }

Application is Running in Background

The app has been launched before but users switch to another app. When the notification is fired, users normally see an alert banner at the top of the screen. When it’s tapped, the app will be brought to the foreground. Similar to the case that the app is running in foreground, the application automatically calls its delegate’s application:didReceiveLocalNotification: method.

Community
  • 1
  • 1
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • Yes you are right but problem is if user opens app from icon than `application:didReceiveLocalNotification` isn't called and data is also not updated in this case. `application:didReceiveLocalNotification` is only called when user open app from banner or notification. – Muhammad Umair Sep 26 '16 at 11:57
  • see this https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html – Anbu.Karthik Sep 26 '16 at 11:59
  • Thank you so much @Anbu.Karthik. but my problem is still there. Is there any way we can update data if user didn't open app from notification but it opens app from icon? – Muhammad Umair Sep 26 '16 at 12:01
  • @MuhammadUmair - if you tapping the icon then didFinishLaunchingWithOptions will called , check once with break point – Anbu.Karthik Sep 26 '16 at 12:03
  • I have checked that.. if app is in background and i opened app from icon `didFinishLaunchingWithOptions` isn't calling. App just restores its previous state. – Muhammad Umair Sep 26 '16 at 12:04
  • If the app is suspended rather than terminated then `applicationWillEnterForeground` is called – Paulw11 Sep 26 '16 at 12:06
  • If app is in background `didFinishLaunchingWithOptions' isn't calling i have checked it again. – Muhammad Umair Sep 26 '16 at 12:06
  • @MuhammadUmair - if your app is background `applicationWillEnterForeground` is called or didbecomeactive is called – Anbu.Karthik Sep 26 '16 at 12:07
  • @Paulw11 if app is not terminating and i opened app from icon; app delegate methods aren't calling. **App just restores its previous state.** – Muhammad Umair Sep 26 '16 at 12:07
  • See my comment, willEnterForeground is called. https://developer.apple.com/reference/uikit/uiapplicationdelegate – Paulw11 Sep 26 '16 at 12:10
  • Anbu is correct here. let me just make it simple @MuhammadUmair If app not terminated and you just kept your app in background mode. then you can keep some bool flag and keep your local notification object as global. now when you tap on icon applicationDidBecomeActive will get call and through bool flag and global local notification object you can do further things. – Hasya Sep 27 '16 at 06:20
-1
 In Appdelegate

  - (void)application:(UIApplication *)application didReceiveLocalNotification:   (UILocalNotification *)notification
{

if (application.applicationState == UIApplicationStateActive) {

}
else if(application.applicationState == UIApplicationStateBackground || application.applicationState == UIApplicationStateInactive)
{
  //  [[NSUserDefaults standardUserDefaults] setObject:notification.alertTitle forKey:@"alertmsg"];

[[NSNotificationCenter defaultCenter]postNotificationName:@"PushNotificationReceived" object:nil userInfo:userInfo];

}
NSLog(@"Alert Message: %@", notification.alertTitle);
NSLog(@"Alert Body: %@", notification.alertBody);

 }


 In view controller:

 -(void)viewDidLoad
{
     [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(doChangesWhatdidYouWant:) name:@"PushNotificationReceived" object:nil];
 }

 -(void)doChangesWhatdidYouWant:(NSNotification *)notification{
 //Todo
 }
Sommm
  • 527
  • 4
  • 22