UILocalNotification 不发送通知

UILocalNotification not sending notifications

我有代码可以在接下来的几周内设置多个 UILocalNotifications。 在我的 didFinishLaunchingWithOptions 中,我有代码请求发送通知的权限,然后我有我的代码来设置我的通知。

但是,代码returns仍然出错:

Haven't received permission from the user to display alerts

我认为它正在尝试 运行 通知代码,然后用户才能批准权限。

如何让我的通知代码 运行 只有当用户同意接收通知时?

这是我的代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
    }
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"FirstTimeBool"]==nil) {
        [defaults setObject:@"YES" forKey:@"FirstTimeBool"];
        NSDate *today = [NSDate date];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"MMMM dd, yyyy"];

        NSString* path = [[NSBundle mainBundle] pathForResource:@"example"
                                                         ofType:@"txt"];
        NSString* content = [NSString stringWithContentsOfFile:path
                                                      encoding:NSUTF8StringEncoding
                                                         error:NULL];
        NSArray* allLinedStrings = [content componentsSeparatedByCharactersInSet:
                                    [NSCharacterSet newlineCharacterSet]];
        for (int i = 0; i < allLinedStrings.count; i++) {
            NSString* strsInOneLine = [allLinedStrings objectAtIndex:i];
            NSArray* singleStrs = [strsInOneLine componentsSeparatedByCharactersInSet:
                                   [NSCharacterSet characterSetWithCharactersInString:@";"]];
            NSString *date = [singleStrs objectAtIndex:0];
            NSDate *eventDate = [dateFormatter dateFromString:date];
            if ([today compare:eventDate] == NSOrderedAscending) {
                for (int j = 1; j < singleStrs.count; j+=2) {
                    UILocalNotification *notification = [[UILocalNotification alloc]init];
                    notification.fireDate = [NSDate dateWithTimeInterval:60*60*-24 sinceDate:eventDate];
                    notification.alertBody = [singleStrs objectAtIndex:j+1];
                    notification.alertTitle = [singleStrs objectAtIndex:j];
                    notification.timeZone = [NSTimeZone defaultTimeZone];
                    [[UIApplication sharedApplication]scheduleLocalNotification:notification];
                }
            }
        }
    }

    // Override point for customization after application launch.
    return YES;
}

对于 iOS 8 及更高版本,您应该使用此 AppDelegate 函数,该函数在用户接受通知时调用:

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings 

在 iOS 8 之前你应该使用这个:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

可在此处找到更多信息:About Local Notifications and Remote Notification