Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 本地通知未到达_Ios_Uilocalnotification - Fatal编程技术网

Ios 本地通知未到达

Ios 本地通知未到达,ios,uilocalnotification,Ios,Uilocalnotification,我正在运行背景代码,以检查网站是否有任何更改,如果有更改,则发送本地通知以提醒用户。但是,当我运行代码并尝试它时,本地通知不会到达。 这是我的appDelegate.m中的代码 - (void)applicationDidEnterBackground:(UIApplication *)application { NSLog(@"Has Entered Background"); timerCountDown = [NSTimer scheduledTimerWithTimeIn

我正在运行背景代码,以检查网站是否有任何更改,如果有更改,则发送本地通知以提醒用户。但是,当我运行代码并尝试它时,本地通知不会到达。 这是我的appDelegate.m中的代码

- (void)applicationDidEnterBackground:(UIApplication *)application {
    NSLog(@"Has Entered Background");
    timerCountDown = [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(backGroundParsing) userInfo:nil repeats:YES];
}

- (void)backGroundParsing {
    //This is where my background checking happens. I deleted the code here to make it shorter.
    if (totalNumberOfEvents > totalNumberOfEvetsRecorded) {
                                NSLog(@"Notificatoin sent");
                                NSString *finalNumberOfEventsRecorded = [NSString stringWithFormat:@"%d",totalNumberOfEvents];
                                NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                                [userDefaults setObject:finalNumberOfEventsRecorded forKey:@"finalRegisteredNumberOfEvents"];
                                UILocalNotification *notifyUserAboutNewEvent = [[UILocalNotification alloc] init];
                                [notifyUserAboutNewEvent setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]];
                                [notifyUserAboutNewEvent setTimeZone:[NSTimeZone defaultTimeZone]];
                                [notifyUserAboutNewEvent setAlertBody:@"New event has been organised"];
                                [[UIApplication sharedApplication] setScheduledLocalNotifications:[NSArray arrayWithObject:notifyUserAboutNewEvent]];
                            }
}
我确实记录了“已发送通知”,但没有收到任何通知。有什么遗漏或错误吗?我的其他无效操作中没有任何其他代码

编辑 在@rckoenes说了些什么之后,我将我的代码编辑成这样

- (void)application:(UIApplication * _Nonnull)application
performFetchWithCompletionHandler:(void (^ _Nonnull)(UIBackgroundFetchResult result))completionHandler {
    [self backGroundParsing];
}

- (void)backGroundParsing {
    //This is where my background checking happens. I deleted the code here to make it shorter.
                            if (totalNumberOfEvents > totalNumberOfEvetsRecorded) {
                                NSLog(@"Notificatoin sent");
                                NSString *finalNumberOfEventsRecorded = [NSString stringWithFormat:@"%d",totalNumberOfEvents];
                                NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                                [userDefaults setObject:finalNumberOfEventsRecorded forKey:@"finalRegisteredNumberOfEvents"];
                                UILocalNotification *notifyUserAboutNewEvent = [[UILocalNotification alloc] init];
                                [notifyUserAboutNewEvent setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]];
                                [notifyUserAboutNewEvent setTimeZone:[NSTimeZone defaultTimeZone]];
                                [notifyUserAboutNewEvent setAlertBody:@"New event has been organised"];
                                [[UIApplication sharedApplication] setScheduledLocalNotifications:[NSArray arrayWithObject:notifyUserAboutNewEvent]];
                            }
                        });
     }];
    [searchEventsTask resume];
}
但是,我仍然没有收到通知。我在模拟背景提取时收到警告。这是出现在我的NSLog中的警告
警告:应用程序代理收到了对-Application:performFetchWithCompletionHandler:的调用,但从未调用过完成处理程序。

模拟后台提取时,此警告正常吗?我的通知仍然没有出现。如何修复它?

在iOS 8+中,您必须获得用户的本地通知权限。在您的
应用程序didFinishLaunchingWithOptions
方法中添加以下行:

[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge| UIUserNotificationTypeSound categories:nil]];

您不能在后台使用计时器。当应用程序进入后台时,您设置了
UILocalNotification
,并使用
fireDate
将通知延迟60秒。但是如果我不使用计时器,它只会检查更改一次,只需从
applicationidenterbackground:
调用
backGroundParsing
,它应该可以工作。但是它不会只叫一次吗?你说只叫一次是什么意思?