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

标签 iphone ios background notifications uilocalnotification

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

相关文章:

ios - CGContextDrawPDFPage 内存泄漏 - 应用程序崩溃

ios - 将砌体 x/y 约束设置为 View 宽度/高度的百分比

android - 如何在待机模式下不让线程自行停止?

ios - 使用自定义消息 Swift 4 打开消息

iphone - MPMoviePlayerController 仅在横向模式下播放电影

iphone - 从 iphone 应用程序向用户发送短信

iphone - 如何选择UIScreen的显示方式覆盖整个电视屏幕?

ios - 处理同一分段按钮上的点击?

ios - 24 小时后 Firebase 授权出现奇怪状态

css - 圆圈图案背景