我想在每个星期日和星期五下午1点重复本地通知消息。
我该怎么做?
目前我使用此代码:
-
修改
- (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]];
}
答案 0 :(得分:2)
试试这个:
-(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];
}
希望它能为你效劳。
答案 1 :(得分:0)
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)
答案 2 :(得分:0)
快速中的快速解答。 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)
}
})