繁体   English   中英

每个星期日和星期五的本地通知

[英]Local Notification every Sunday and Friday

我想在每个星期日和星期五下午1:00重复本地通知消息。

我该怎么做?

目前,我使用以下代码:

-

编辑:

- (void)applicationDidEnterBackground:(UIApplication *)application {

//Calendar
NSCalendar *gregCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *dateCompnent = [gregCalendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitWeekday fromDate:[NSDate date]];


//[dateCompnent setYear:2015];
//[dateCompnent setMonth:7];
//[dateCompnent setDay:28];
[dateCompnent setWeekday:4];
[dateCompnent setHour:15];
[dateCompnent setMinute:53];

UIDatePicker *dd = [[UIDatePicker alloc] init];
[dd setDate:[gregCalendar dateFromComponents:dateCompnent]];

//Notification
UILocalNotification *notification = [[UILocalNotification alloc] init];
[notification setAlertBody:@"Test Notification"];
[notification setFireDate:dd.date];
[notification setSoundName:@"sound.mp3"];
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
[application setScheduledLocalNotifications:[NSArray arrayWithObject:notification]];
}

尝试这个:

-(void) SetNotificationForday:(int)weekday :(UILocalNotification *)notification :(NSDate*) date{
    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit |  NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate:date];
    [componentsForFireDate setWeekday:weekday] ; // Set 1 if you need to fix for Sunday
    [componentsForFireDate setHour: 13] ; // Its Set for fixing 1 PM
    [componentsForFireDate setMinute:0] ;
    [componentsForFireDate setSecond:0] ;
    notification.repeatInterval = NSWeekCalendarUnit;
    notification.fireDate = [calendar dateFromComponents:componentsForFireDate];
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

希望它将为您工作。

Swift 3,特定日期的本地通知示例

let note = UILocalNotification()
note.alertBody = "text goes here"
note.soundName = UILocalNotificationDefaultSoundName                

let calendar = Calendar.autoupdatingCurrent
var comps = DateComponents()
comps.hour = 8
comps.minute = 15
comps.second = 0
comps.day = day

let date = calendar.date(from: comps)
note.fireDate = date
note.repeatInterval = .weekOfYear

UIApplication.shared.scheduleLocalNotification(note)

Swift中的快速解答。 iOS 10.0中已弃用“ UILocalNotification”。 因此,将建议使用UNNotificationRequest

let content = UNMutableNotificationContent()
content.title = "YOUR TITLE"
content.subtitle = "YOUR SUBTITLE"
content.body = "YOUR BODY"
content.badge = 1
content.sound = UNNotificationSound.default()

var date = DateComponents()
date.weekday = 0 ,6 // which is Sunday and Friday
date.hour = 17
date.minute = 46

let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
let request = UNNotificationRequest(identifier: "notificationName", content: content, trigger: trigger)
        UNUserNotificationCenter.current().add(request, withCompletionHandler: {error in
            print("success")
        })
    }
// add these lines to check if you added the notification successfully
UNUserNotificationCenter.current().getPendingNotificationRequests(
    completionHandler: { (notficiations) in
        for localNotification in notficiations {
            print(localNotification)
        }
    })

暂无
暂无

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

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