gpt4 book ai didi

iOS:如果应用程序在后台并且本地通知到达;哪个方法会自动调用?

转载 作者:行者123 更新时间:2023-11-28 21:21:24 25 4
gpt4 key购买 nike

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

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

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

最佳答案

应用程序未运行

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

  • 显示警告或横幅

  • 标记应用程序图标

  • 播放声音

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

应用程序在前台运行

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

应用程序在后台运行

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:如果应用程序在后台并且本地通知到达;哪个方法会自动调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39701986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com