本地通知 - 重复间隔/自定义时间不起作用
Local Notifications - Repeat Interval / custom time doesn't work
某些基本通知似乎无法正常工作。
我正在使用本地通知为用户带来警报声。
我可以每小时收到通知上班。
我似乎无法让一个人每半小时(每 30 分钟,或 30 分钟或 1 小时)或每 15 分钟工作一次。
如何对每小时重复间隔进行偏移?通知没有触发。
如果这不可能,我将如何手动编写一个循环直到达到 64 个排队通知的限制?
UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:[NSDate date]];
NSInteger hour = [components hour];
NSDate *h = [calendar dateBySettingHour:hour+1 minute:0 second:0 ofDate:[NSDate date] options:0];
NSDate *f = [calendar dateBySettingHour:hour minute:15 second:0 ofDate:[NSDate date] options:0];
NSDate *ha = [calendar dateBySettingHour:hour minute:30 second:0 ofDate:[NSDate date] options:0];
NSDate *q = [calendar dateBySettingHour:hour minute:45 second:0 ofDate:[NSDate date] options:0];
if (selected == 0)
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
if (notification)
{
notification.repeatInterval = NSCalendarUnitHour;
notification.alertBody = nil;
notification.alertAction = nil;
notification.soundName = @"4.mp3";
notification.fireDate = h;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}
else if (selected == 1)
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
if (notification)
{
notification.fireDate = ha;
notification.repeatInterval = NSCalendarUnitHour;
notification.alertBody = nil;
notification.alertAction = nil;
notification.soundName = @"2.mp3";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
notification.soundName = @"4.mp3";
notification.fireDate = h;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}
else
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
if (notification)
{
notification.fireDate = f;
notification.repeatInterval = NSCalendarUnitHour;
notification.alertBody = nil;
notification.alertAction = nil;
notification.soundName = @"1.mp3";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
notification.soundName = @"2.mp3";
notification.fireDate = ha;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
notification.soundName = @"3.mp3";
notification.fireDate = q;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
notification.soundName = @"4.mp3";
notification.fireDate = h;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}
嗯,我又试了一遍,果然可以,只是没有正确导入资源。我刚刚删除了 finder 中的声音文件并重新导入它们,应用程序正确地触发了通知!
某些基本通知似乎无法正常工作。
我正在使用本地通知为用户带来警报声。
我可以每小时收到通知上班。
我似乎无法让一个人每半小时(每 30 分钟,或 30 分钟或 1 小时)或每 15 分钟工作一次。
如何对每小时重复间隔进行偏移?通知没有触发。
如果这不可能,我将如何手动编写一个循环直到达到 64 个排队通知的限制?
UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:[NSDate date]];
NSInteger hour = [components hour];
NSDate *h = [calendar dateBySettingHour:hour+1 minute:0 second:0 ofDate:[NSDate date] options:0];
NSDate *f = [calendar dateBySettingHour:hour minute:15 second:0 ofDate:[NSDate date] options:0];
NSDate *ha = [calendar dateBySettingHour:hour minute:30 second:0 ofDate:[NSDate date] options:0];
NSDate *q = [calendar dateBySettingHour:hour minute:45 second:0 ofDate:[NSDate date] options:0];
if (selected == 0)
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
if (notification)
{
notification.repeatInterval = NSCalendarUnitHour;
notification.alertBody = nil;
notification.alertAction = nil;
notification.soundName = @"4.mp3";
notification.fireDate = h;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}
else if (selected == 1)
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
if (notification)
{
notification.fireDate = ha;
notification.repeatInterval = NSCalendarUnitHour;
notification.alertBody = nil;
notification.alertAction = nil;
notification.soundName = @"2.mp3";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
notification.soundName = @"4.mp3";
notification.fireDate = h;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}
else
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
if (notification)
{
notification.fireDate = f;
notification.repeatInterval = NSCalendarUnitHour;
notification.alertBody = nil;
notification.alertAction = nil;
notification.soundName = @"1.mp3";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
notification.soundName = @"2.mp3";
notification.fireDate = ha;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
notification.soundName = @"3.mp3";
notification.fireDate = q;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
notification.soundName = @"4.mp3";
notification.fireDate = h;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}
嗯,我又试了一遍,果然可以,只是没有正确导入资源。我刚刚删除了 finder 中的声音文件并重新导入它们,应用程序正确地触发了通知!