本地通知不工作 iOS 8

Posted

技术标签:

【中文标题】本地通知不工作 iOS 8【英文标题】:Local Notification not working iOS 8 【发布时间】:2015-04-13 07:59:38 【问题描述】:

我想在每天的特定时间触发本地通知,我已经编写了这段代码 sn-p 但它没有收到通知。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

 //  running on ios8
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)])
    
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound) categories:nil];
        [application registerUserNotificationSettings:settings];
    
    else // iOS 7 or earlier
    
        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
        [application registerForRemoteNotificationTypes:myTypes];
    



-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
    NSLog(@"didReceiveLocalNotification----");
    application.applicationIconBadgeNumber = 0;


//ViewController类

- (void)viewDidLoad

    [super viewDidLoad];

    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];

    [dateComponents setHour:7];
    [dateComponents setMinute:59];
    NSDate *currentDate = [NSDate date]; //2015-04-13 07:56:09 +0000
    NSDate *fireDate = nil;
    fireDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents
                                          toDate:currentDate
                                         options:0];

    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
    localNotification.alertBody = @"Notiication success....";
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;




    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

我的日志显示didReceiveLocalNotification,但通知未出现在设备中:(

我哪里出错了,请帮忙。

提前致谢

【问题讨论】:

【参考方案1】:

应用在前台时不会出现通知。仅当应用程序完全关闭或在后台时才会显示通知。

【讨论】:

可以在每天的固定时间安排通知吗? 当然,检查这个问题***.com/questions/11303413/…

以上是关于本地通知不工作 iOS 8的主要内容,如果未能解决你的问题,请参考以下文章

iOS 后台获取本地通知

检查用户是不是允许本地通知。 iOS 8. Obj-C

关于 iOS 本地通知的问题

iOS创建本地通知和删除对应的通知,工作日通知

IOS 取消本地通知

xcode 8 和 iOS 10 本地通知