当我创建本地通知时,会触发回调didReceiveLocalNotification
。单击本地通知时会触发相同的回调。目前我通过检查
if ([UIApplication sharedApplication].applicationState == UIApplicationStateInactive) {
//this means notification is clicked
}
但这里的主要问题是,当您在前台并滑动通知菜单,然后接收本地通知时,将调用此回调didReceiveLocalNotification
。在这种情况下,如果我的应用程序进入此。因此,当应用处于非活动状态时,我无法区分点击通知和创建本地通知。关于如何解决这个问题的任何想法?
这是用于安排本地通知的代码:
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = @"aaaa";
localNotification.alertTitle = @"title";
localNotification.userInfo = myUserInfo;
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
调用此功能后,我会触发didReceiveLocalNotification
委托。
答案 0 :(得分:2)
我必须在我的应用中解决完全相同的问题。我无法在API中找到一种正式的方法来区分,但这是一种解决方法。
创建本地通知时,将当前日期传递到userInfo
:
localNotification.userInfo = ["alertDate": NSDate()]
当您处理didReceiveLocalNotification
时,请再次检查当前日期,以确保它不会在刚刚开始时触发:
if application.applicationState == .Inactive {
if let alertDate = notification.userInfo?["alertDate"] as? NSDate
where (NSDate()).timeIntervalSinceDate(alertDate) > 0.1 {
// this means notification was initiated by user
}
}
答案 1 :(得分:0)
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
localNotification.alertBody = @"image inserted";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[self dismissViewControllerAnimated:YES completion:nil];