iOS: If app is in background and local notificatio

2019-09-05 20:08发布

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);
}

2条回答
戒情不戒烟
2楼-- · 2019-09-05 20:18
 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
 }
查看更多
虎瘦雄心在
3楼-- · 2019-09-05 20:41

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.

查看更多
登录 后发表回答