找不到任何方法可以做到这一点,但正如我看到许多应用可以做到这一点。我经常在skype应用程序中注意到,当我在锁定屏幕上看到通知但是在通知中心时,通知没有出现在历史记录中。反之亦然,在锁定屏幕上看不到任何通知,但我在通知中心看到它们。
如果app状态位于前台,是的,我可以通过AppDelegate文件中的此代码阻止它们:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
if (application.applicationState == UIApplicationStateActive) {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}
但是这种方法在用户与后台通知进行交互时调用,此时通知已经在中心。
那么有什么建议吗? THX。
答案 0 :(得分:0)
你可以这样做,
if (application.applicationState == UIApplicationStateInactive) {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
//or
if (application.applicationState == UIApplicationStateBackground) {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
采用相同的didReceiveLocalNotification
方法!!
答案 1 :(得分:0)
我想测试一下该方法,并决定在UILocalNotification
的帮助下取消dispatch_after
。这是代码:
UILocalNotification *theLocalNotification = [UILocalNotification new];
theLocalNotification.fireDate = [NSDate dateWithTimeInterval:10 sinceDate:[NSDate new]];
theLocalNotification.alertBody = @"test";
[[UIApplication sharedApplication] scheduleLocalNotification:theLocalNotification];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(12 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^
{
[[UIApplication sharedApplication] cancelLocalNotification:theLocalNotification];
});
// the below lines are needed so that `dispatch_after` runs when app is in background
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:5];
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
它确实成功取消UILocalNotification
,但事实证明,cancelLocalNotification:
方法会从 锁定屏幕和历史记录屏幕中删除通知。因此,我们甚至没有朝着正确的方向寻找,应该有其他方式来做这件事。