iOS:如果应用程序在后台并且本地通知到达;哪个方法会自动调用? [英] iOS: If app is in background and local notification is arrived; which method will called automatically?

查看:71
本文介绍了iOS:如果应用程序在后台并且本地通知到达;哪个方法会自动调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,如果应用程序在后台并且通知到达并且我从图标打开了应用程序;应用程序恢复它的状态,但我想在这种情况下更新屏幕数据.当通知到达时,有没有办法在后台更新数据?

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?

这是我用来处理这种情况的代码:

ViewController.m 文件代码:

 - (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 文件代码:

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

推荐答案

应用程序未运行

当应用未运行时,用户会通过以下方式看到通知,具体取决于通知设置:

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

  • 显示警报或横幅

  • Displaying an alert or banner

标记应用图标

播放声音

通过点击通知的操作按钮,用户将启动应用程序.在这种情况下,应用程序委托的 application:didFinishLaunchingWithOptions: 方法被调用.

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

应用程序在前台运行

如果在传递通知时应用程序正在运行,则屏幕上不会显示警报.应用程序自动调用其委托的 application:didReceiveLocalNotification: 方法.

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:didReceiveLocalNotification: 方法.

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.

这篇关于iOS:如果应用程序在后台并且本地通知到达;哪个方法会自动调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆