繁体   English   中英

本地通知repeatInterval

[英]Local notification repeatInterval

我的应用程序要显示学生时间表,我想添加一个本地通知repeatInterval
我打算为此使用开关按钮。

我尝试了这段代码,但现在无法正常工作:

    -(IBAction)theSwitch:(id)sender{
if (switcher.on) {
    NSCalendar *gregCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *dateComponent = [gregCalendar components:NSYearCalendarUnit  | NSWeekCalendarUnit fromDate:[NSDate date]];

    [dateComponent setWeekday:3]; // For tuesday
    [dateComponent setHour:13];
    [dateComponent setMinute:41];

    NSDate *fireDate = [gregCalendar dateFromComponents:dateComponent];

    UILocalNotification *notification = [[UILocalNotification alloc]init];
    [notification setAlertBody:@"ALARM!"];
    [notification setFireDate:fireDate];
    notification.repeatInterval = NSWeekCalendarUnit;
    [notification setTimeZone:[NSTimeZone defaultTimeZone]];
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
else  {UIApplication *app=[UIApplication sharedApplication];
[app scheduleLocalNotification:nil];
}
    }

因此,我在此需要更多指导,以及有关如何实现此目标的一些建议。

如果您无法获得本地通知,请检查以下代码。

在AppDelegate.m文件中添加代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Handle launching from a notification
UILocalNotification *localNotification =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotification) {
    NSLog(@"Recieved Notification %@", localNotification);
}
return YES;
}

添加方法来处理本地通知

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notification {
// Handle the notificaton when the app is running
NSLog(@"Recieved Notification %@", notification);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM