输入 applicationDidBecomeActive 时本地通知不起作用
Posted
技术标签:
【中文标题】输入 applicationDidBecomeActive 时本地通知不起作用【英文标题】:Local Notifications not working When enter applicationDidBecomeActive 【发布时间】:2014-04-28 05:31:57 【问题描述】:我正在使用重复本地通知向用户显示警报。为此,我使用了以下代码
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [[NSDate date] dateByAddingTimeInterval:60];
localNotification.alertBody = @"sss";
localNotification.alertAction = @"Show me the item";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
使用 NSNotification Center 我将每 60 秒调用一次本地通知。它工作正常。
我也在 Appdelegate 中使用以下代码:-
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
NSLog(@"Received");
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive)
[[NSNotificationCenter defaultCenter] postNotificationName:@"RestartProcess" object:self];
application.applicationIconBadgeNumber = 0;
还有
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
UILocalNotification *locationNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (locationNotification)
// Set icon badge number to zero
NSLog(@"Yes Notid=fications is predsent");
application.applicationIconBadgeNumber = 0;
return YES;
我的问题是当本地通知开始时,我按下主页按钮然后等待显示。时间段后显示通知消息。 当我点击应用程序图标时,进程停止。之后本地通知不起作用。但是当我点击通知消息时,它会正常工作。我该如何解决这个问题。任何人都可以帮我做到这一点。提前谢谢..
【问题讨论】:
【参考方案1】:didReceiveLocalNotification 仅在您点击通知消息时调用。如果您想在 applicationDidBecomeActive 中执行此操作,则必须手动执行。
您可以检查徽章数量(如果通知包含徽章),例如:
-(void) applicationDidBecomeActive:(UIApplication *)application
if(application.applicationIconBadgeNumber >= 1)
//Do you stuff here
application.applicationIconBadgeNumber = 0;
【讨论】:
【参考方案2】:当你按下主页按钮时:
UIApplicationState state = [application applicationState];
// at this time, state == UIApplicationStateInactive
当你点击你的应用程序时,我认为你应该在这个方法中实现:
- (void)applicationDidBecomeActive:(UIApplication *)application- (void)applicationWillEnterForeground:(UIApplication *)application
UIApplicationState state = [application applicationState];
application.applicationIconBadgeNumber = 0;
//state at this time is UIApplicationStateInactive
if (state == UIApplicationStateInactive)
[[NSNotificationCenter defaultCenter] postNotificationName:@"RestartProcess" object:self];
希望对你有帮助
【讨论】:
以上是关于输入 applicationDidBecomeActive 时本地通知不起作用的主要内容,如果未能解决你的问题,请参考以下文章