我需要后台进程(用于调用网络服务)在应用程序启动状态后调用 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/