【问题标题】:Local Notification not working iOS 8本地通知不工作 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,但通知未出现在设备中:(

我哪里出错了,请帮忙。

提前致谢

【问题讨论】:

    标签: ios iphone ipad notifications uilocalnotification


    【解决方案1】:

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 2015-07-10
    • 2014-11-23
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多