我的问题是,如果应用在后台并且通知到达,我从图标打开了应用;应用程序恢复它状态,但我想在这种情况下更新屏幕数据。有通知到达时有没有办法在后台更新数据?
以下是我用于处理此案例的代码:
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);
}
答案 0 :(得分:3)
应用程序未运行
当应用未运行时,用户会通过以下方式查看通知,具体取决于通知设置:
显示提醒或横幅
标记应用图标
播放声音
点击通知的操作按钮,用户将启动该应用。在这种情况下,应用程序:didFinishLaunchingWithOptions:调用应用程序委托的方法。
- (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正在前台运行
如果应用程序在通知发送时正在运行,则屏幕上不会显示任何警报。应用程序自动调用其委托的应用程序: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;
}
应用程序正在后台运行
此应用已推出,但用户切换到另一个应用。通知被触发后,用户通常会在屏幕顶部看到警报横幅。当点击它时,应用程序将被带到前台。与应用程序在前台运行的情况类似,应用程序自动调用其委托的应用程序:didReceiveLocalNotification:method。
答案 1 :(得分:-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
}