![](/img/trans.png)
[英]How can I set the Local notification which fire every seconds after first set notification?
[英]I am trying to fire local notification every 5 seconds using this code
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
if (localNotification == nil)
return;
//localNotification.fireDate = [NSDate dateWithTimeInterval:timeInterval sinceDate:now];
localNotification.fireDate = [NSDate date];
localNotification.repeatInterval = 5*NSSecondCalendarUnit;
localNotification.alertBody = @"Your alert message";
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
此代碼在前5秒后會生成通知,然后返回NSMinuteCalendarUnit,我一直在嘗試解決此問題,但沒有幫助。
我想每5秒設置一次通知,然后它將觸發它,直到我強制要停止它為止。
有人可以幫忙嗎?
UILocalNotification
的repeatInterval
的類型為NSCalendarUnit
而不是時間間隔。 您只能為NSCalendarUnit
枚舉分配一個值。
如果您想每5秒觸發一次本地通知,則需要設置多個通知。
這是代碼。 每隔5分鍾而不是每周使用一次。
-(void)setLocalNotificationwithOptions :(NSDictionary *)launchOptions
{
UILocalNotification *localNoti = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNoti) {
NSLog(@"Notification:%@",localNoti);
}
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSCalendar *gregCalendar12 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent12 = [gregCalendar12 components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
[dateComponent12 setWeekday:7];
[dateComponent12 setHour:14];
[dateComponent12 setMinute:46];
UILocalNotification *notification12 = [[UILocalNotification alloc]init];
[notification12 setAlertBody:@"ENJOY YOUR WEEKEND!"];
[notification12 setFireDate:[gregCalendar12 dateFromComponents:dateComponent12]];
notification12.repeatInterval = NSMinuteCalendarUnit;
[notification12 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification12];
}
希望這對您有所幫助。 謝謝..
如果您想每5秒重復一次計時器,則UILocalNotification允許每一種聲音持續30秒。 因此,在您的目錄中放30秒的聲音,然后
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSDate *date = "set your notification date"
for (int i = 0; i < 10; i++){
NSDate *repeatTime = [date dateByAddingTimeInterval:300 * i];
notification.fireDate = repeatTime;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.