代码之家  ›  专栏  ›  技术社区  ›  Faisal

本地通知重复间隔

  •  0
  • Faisal  · 技术社区  · 11 年前

    我的应用程序是关于显示学生时间表的,我想添加本地通知 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];
    }
        }
    

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

    1 回复  |  直到 11 年前
        1
  •  0
  •   Mehul Patel Nikita Khandelwal    11 年前

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

    在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);
    }
    
    推荐文章