3 回答

TA貢獻(xiàn)1820條經(jīng)驗 獲得超10個贊
得到了答案,它是盡可能直接的。
您無法創(chuàng)建自定義重復(fù)間隔。
您必須使用NSCalendarUnit的內(nèi)置單位時間間隔。
我嘗試了上述所有解決方案,甚至嘗試了其他方法,但是它們都不起作用。
我花了很多時間來發(fā)現(xiàn)沒有辦法讓它在自定義時間間隔內(nèi)工作。

TA貢獻(xiàn)1788條經(jīng)驗 獲得超4個贊
我有一個想法如何做到這一點,我已經(jīng)在我的項目中實現(xiàn)了
首先,使用火災(zāi)日期(例如,每分鐘)創(chuàng)建本地通知。下一步-使用此通知的唯一ID(如果您以后希望刪除它)和您的自定義期限來填充用戶信息:
-(void) createLocalRepeatedNotificationWithId: (NSString*) Id
{
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
NSTimeInterval your_custom_fire_interval = 60; // interval in seconds
NSDate *remindDate = [[NSDate date] dateByAddingTimeInterval:your_custom_fire_interval];
localNotification.fireDate = remindDate;
localNotification.userInfo = @{@"uid":Id, @"period": [NSNumber numberWithInteger:your_custom_fire_interval]};
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
之后,-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification在您的AppDelegate中實施:
從用戶信息中獲取您的自定義期間。
更改下一個時期的開火日期
只需再次將其添加到隱藏的通知中即可!
NSInteger period = [[notification.userInfo objectForKey:@"period"] integerValue]; //1
NSTimeInterval t= 10 * period;
notification.fireDate =[[NSDate date] dateByAddingTimeInterval:t]; //2
[[UIApplication sharedApplication] scheduleLocalNotification:notification]; //3
如果您要刪除此通知,請執(zhí)行
UIApplication *app = [UIApplication sharedApplication];
NSArray *eventArray = [app scheduledLocalNotifications];
for (int i=0; i<[eventArray count]; i++)
{
UILocalNotification* oneEvent = [eventArray objectAtIndex:i];
NSDictionary *userInfoCurrent = oneEvent.userInfo;
NSString *uid=[NSString stringWithFormat:@"%@",[userInfoCurrent valueForKey:@"id"]];
if ([uid isEqualToString:notification_id_to_remove])
{
//Cancelling local notification
[app cancelLocalNotification:oneEvent];
break;
}
}
很重要!
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification當(dāng)您處于后臺時,請勿調(diào)用。因此,您必須設(shè)置長時間運行的后臺任務(wù),在該任務(wù)中您將再次創(chuàng)建通知。
- 3 回答
- 0 關(guān)注
- 722 瀏覽
添加回答
舉報