【发布时间】:2015-01-21 17:20:31
【问题描述】:
在我的应用委托中,永远不会调用方法 - (void)application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification。
这就是我创建通知的方式:
UILocalNotification *notification = [[UILocalNotification alloc] init];
// set UUID, which we will store in userDefaults for later
NSMutableDictionary *myUserInfo = [[NSMutableDictionary alloc] init];
NSString *uuid = [[NSProcessInfo processInfo] globallyUniqueString];
[myUserInfo setValue:uuid forKey:KEY_UUID];
[myUserInfo setValue:@"month" forKey:KEY_UNIT];
[myUserInfo setObject:@YES forKey:KEY_RESCHEDULE];
NSInteger row = [_wurmProphylaxePickerView selectedRowInComponent:0];
switch (row) {
case 0:
[myUserInfo setValue:@2 forKey:KEY_FREQUENCY];
break;
case 1:
[myUserInfo setValue:@4 forKey:KEY_FREQUENCY];
break;
case 2:
[myUserInfo setValue:@6 forKey:KEY_FREQUENCY];
break;
default:
[myUserInfo setValue:@4 forKey:KEY_FREQUENCY];
break;
}
notification.userInfo = myUserInfo;
// calculate date for next notification, depends on the user's selection
NSDate *today = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *myComps = [[NSDateComponents alloc] init];
[myComps setMinute:1];
notification.fireDate = [calendar dateByAddingComponents:myComps toDate:today options:0];
notification.timeZone = [NSTimeZone localTimeZone];
notification.alertBody = @"My alertBody";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
这是在我的应用委托中,但从未被调用:
- (void)application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification
{
NSDictionary *userInfo = notification.userInfo;
BOOL repeat = [[userInfo objectForKey:KEY_RESCHEDULE] boolValue];
if (repeat)
{
NSInteger frequency = (NSInteger)[userInfo objectForKey:KEY_FREQUENCY];
NSString *unit = (NSString *)[userInfo objectForKey:KEY_UNIT];
NSString *uuid = (NSString *)[userInfo objectForKey:KEY_UUID];
// calculate date for next notification
NSDate *today = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *myComps = [[NSDateComponents alloc] init];
if ([unit isEqualToString:@"month"]) {
//[myComps setMonth:frequency];
[myComps setMinute:frequency];
} else {
}
// create new notification
UILocalNotification *newNotification = [[UILocalNotification alloc] init];
newNotification.fireDate = [calendar dateByAddingComponents:myComps toDate:today options:0];
newNotification.timeZone = [NSTimeZone localTimeZone];
newNotification.alertAction = notification.alertAction;
newNotification.alertBody = notification.alertBody;
newNotification.userInfo = notification.userInfo;
// schedule it
[[UIApplication sharedApplication] scheduleLocalNotification:newNotification];
}
}
在 iOS 8 上测试,不确定 iOS 7...
【问题讨论】:
-
您是否先注册了通知?见Registering for Notification Types in iOS。
-
是的,我愿意,并且显示权限警报,并显示通知,但没有调用委托方法
-
...当这个通知进来时,应用程序在前台?
-
不,它在后台
-
好的。我知道了。如果您将其发布为答案,我会接受。 TX Rob。
标签: ios objective-c uilocalnotification appdelegate