gpt4 book ai didi

iphone - 如何调用didReceiveLocalNotification :(UILocalNotification *)notification in iPhone?

转载 作者:行者123 更新时间:2023-11-29 03:52:40 26 4
gpt4 key购买 nike

我需要后台进程(用于调用网络服务)在应用程序启动状态后调用 didReceiveLocalNotification:(UILocalNotification *)notification,如何做到这一点,请帮助我。

提前致谢

我试过这个:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];

UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
}

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
{
if (app.applicationState == UIApplicationStateInactive )
{
NSLog(@"app not running");
}
else if(app.applicationState == UIApplicationStateActive )
{
NSLog(@"app running");
}
}

最佳答案

这就是我创建本地通知的方法,该通知安排在该代码运行当天的 17:00。一旦触发,方法 -(void)application:didReceiveLocalNotification: 将被调用。

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setTimeZone:[NSTimeZone localTimeZone]];

NSDateComponents *dateComponents = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[NSDate date]];
[dateComponents setHour:17];
[dateComponents setMinute:00];
[dateComponents setSecond:00];

NSDate *notificationDate = [calendar dateFromComponents:dateComponents];

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = notificationDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];

localNotif.alertBody = @"blah blah blah";
localNotif.alertAction = @"Ok";

localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

关于iphone - 如何调用didReceiveLocalNotification :(UILocalNotification *)notification in iPhone?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16917925/

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