[ios] UILocalNotification हर सप्ताह के दिन दोहराना माना जाता है, लेकिन सप्ताहांत पर भी आग लग जाती है


1 Answers

आपको स्थानीय अधिसूचना के लिए अलग-अलग दिन सेट करना होगा और एनएसवीईकेकैलेंडर यूनिट को स्थानीय अधिसूचना दोहराए जाने का समय निर्धारित करना होगा। जैसा

            [self notificationWithItem:tempDict Date:[self SetDateForAlarmWithWeekday:2 :tempDict] andRepeatInterval:NSWeekCalendarUnit];
            [self notificationWithItem:tempDict Date:[self SetDateForAlarmWithWeekday:3 :tempDict]andRepeatInterval:NSWeekCalendarUnit];
            [self notificationWithItem:tempDict Date:[self SetDateForAlarmWithWeekday:4 :tempDict] andRepeatInterval:NSWeekCalendarUnit];
            [self notificationWithItem:tempDict Date:[self SetDateForAlarmWithWeekday:5 :tempDict] andRepeatInterval:NSWeekCalendarUnit];
            [self notificationWithItem:tempDict Date:[self SetDateForAlarmWithWeekday:6 :tempDict] andRepeatInterval:NSWeekCalendarUnit];

यहां मैंने विधियां सेटडेट डेटएफ़ायरअल्मरव्वेज वेक्डेड की हैं जो कि तारीख को स्थानीय अधिसूचना और अधिसूचना की फायरडेट संपत्ति में सेट की जाती हैं, जिसमें स्थानीय अधिसूचना निर्धारित की जाती है।

-(void) notificationWithItem:(NSDictionary*)tmpdict Date:(NSDate *)date andRepeatInterval:(NSCalendarUnit)CalUnit
{
    UILocalNotification *localNotification =[[UILocalNotification alloc]init];

    if (localNotification==nil) {
        return;
    }

    localNotification.fireDate=date;
    localNotification.timeZone=[NSTimeZone defaultTimeZone];
    localNotification.repeatCalendar=[NSCalendar currentCalendar];
    localNotification.alertBody=[NSString stringWithFormat:@"%@",[tmpdict objectForKey:@"Reminder"]];

    NSDictionary *snoozeDic=[tmpdict objectForKey:@"Snooze"];
    if ([[snoozeDic valueForKey:@"Switch"]intValue]==1) {
        localNotification.alertAction=@"Snooze";
    }else
    {
        localNotification.hasAction=NO;
    }

    localNotification.repeatInterval=CalUnit;
    localNotification.soundName=[NSString stringWithFormat:@"%@.caf",[tmpdict objectForKey:@"Tone"]];
    localNotification.userInfo=[NSDictionary dictionaryWithObject:tmpdict forKey:@"AlarmInfo"];
    localNotification.applicationIconBadgeNumber=1;

    [[UIApplication sharedApplication]scheduleLocalNotification:localNotification];

}


-(NSDate*)SetDateForAlarmWithWeekday:(int)WeekDay:(NSDictionary*)dics
{
    NSLog(@"set date for alarm called");
    NSCalendar *calendar=[NSCalendar currentCalendar];
    [calendar setTimeZone:[NSTimeZone defaultTimeZone]];

    unsigned currentFlag=NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit|NSWeekdayCalendarUnit;

    NSDateComponents *comp=[calendar components:currentFlag fromDate:[NSDate date]];

    NSArray *array=[[dics objectForKey:@"Time"] componentsSeparatedByString:@" "];
    NSInteger hour=[[[[array objectAtIndex:0] componentsSeparatedByString:@":"] objectAtIndex:0] intValue];
    NSInteger min=[[[[array objectAtIndex:0] componentsSeparatedByString:@":"] objectAtIndex:1] intValue];

    if ([[array objectAtIndex:1] isEqualToString:@"PM"]) {
        hour=hour+12;
    }
    else
    {
        if (hour==12) {
            hour=0;
        }
    }

    comp.hour=hour;
    comp.minute=min;
    comp.second=0;

    NSLog(@"set date for alarm (%i:%i:%i)",comp.hour,comp.minute,comp.second);
    NSLog(@"weekday :%i ",WeekDay);
    NSLog(@"comp weekday %i",comp.weekday);
    int diff=(WeekDay-comp.weekday);
    NSLog(@"difference :%i",diff);

    int multiplier;
    if (WeekDay==0) {
     multiplier=0;
    }else
    {
     multiplier=diff>0?diff:(diff==0?diff:diff+7);
    }

    NSLog(@"multiplier :%i",multiplier);

    return [[calendar dateFromComponents:comp]dateByAddingTimeInterval:multiplier*secondsInOneDay];
}

मुझे आशा है कि यह आपकी मदद करेगा

Question

मेरे पास एक यूआईएलोकल नोटिफिकेशन है जो दिन में एक बार, सोमवार से शुक्रवार को आग लगना है, लेकिन सप्ताहांत पर नहीं। मैंने सोचा था कि NSWeekdayCalendarUnit के लिए अधिसूचना की दोहराना अंतराल संपत्ति की स्थापना इस पूरा होगा दुर्भाग्य से मेरे लिए, मेरी सूचनाएं अभी भी सप्ताहांत पर गोलीबारी कर रही हैं क्या कोई सुझाव दे सकता है? यहां मेरा कोड है:

UILocalNotification *localNotification = [[UILocalNotification alloc] init];

localNotification.alertAction = @"View";
localNotification.alertBody = NSLocalizedString(@"ALERT_MESSAGE", nil);
localNotification.soundName = UILocalNotificationDefaultSoundName;

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM-dd-yyyy HH:mm"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"America/Toronto"]];

// Notification fire times are set by creating a notification whose fire date 
// is an arbitrary weekday at the correct time, and having it repeat every weekday
NSDate *fireDate = [dateFormatter dateFromString:@"01-04-2012 11:00"]; 

localNotification.fireDate = fireDate;
localNotification.repeatInterval = NSWeekdayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
            break;

[localNotification release];



समान लेकिन स्विफ्ट में

याद रखें कि सप्ताह के दिन 1 = रविवार से शुरू होता है और 7 जाता है। ऐप्पल डॉक्स देखें: https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSDateComponents_Class/

func scheduleLocalNotification(date:NSDate, weekDay:Int){

        guard let calendar =  NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian),
    let groupName = dateFormatter?.stringFromDate(date) else { return }

        calendar.timeZone = NSTimeZone.defaultTimeZone()

        let dateComponents = NSDateComponents()
        dateComponents.calendar = calendar
        dateComponents.hour = calendar.component(.Hour, fromDate: date)
        dateComponents.minute = calendar.component(.Minute, fromDate: date)
        dateComponents.weekOfYear = calendar.component(.WeekOfYear, fromDate: date)
        dateComponents.weekday = weekDay

        let localNotificaion = UILocalNotification()
        localNotificaion.fireDate = calendar.dateFromComponents(dateComponents)
        localNotificaion.alertBody = "Hey!"
        localNotificaion.timeZone = NSTimeZone.defaultTimeZone()
        localNotificaion.userInfo = ["Time": groupName]
        localNotificaion.repeatInterval = .WeekOfYear
        UIApplication.sharedApplication().scheduleLocalNotification(localNotificaion)

}





Related