gpt4 book ai didi

ios - 输入 applicationDidBecomeActive 时本地通知不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:06:02 29 4
gpt4 key购买 nike

我正在使用重复本地通知向用户显示警报。为此,我使用了下面的代码

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

我的问题是当本地通知开始时,我按下主页按钮然后等待显示。时间段后显示通知消息。当我点击应用程序图标时,进程停止。之后本地通知不起作用。但是当我点击通知消息时它会正常工作。我如何解决这个问题。任何人都可以帮助我做到这一点。提前致谢..

最佳答案

didReceiveLocalNotification 仅在您点击通知消息时调用。如果您想在 applicationDidBecomeActive 中执行此操作,则必须手动执行。

您可以检查角标(Badge)数量(如果通知包含角标(Badge)),例如:

-(void) applicationDidBecomeActive:(UIApplication *)application
{

if(application.applicationIconBadgeNumber >= 1)
{
//Do you stuff here
application.applicationIconBadgeNumber = 0;

}
}

关于ios - 输入 applicationDidBecomeActive 时本地通知不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23333156/

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