第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將本地通知重復(fù)間隔設(shè)置為自定義時間間隔?

如何將本地通知重復(fù)間隔設(shè)置為自定義時間間隔?

iOS
千萬里不及你 2019-11-15 20:53:25
我正在制作一個iPhone應(yīng)用程序,該應(yīng)用程序需要本地通知。在本地通知中,有repeatInterval屬性,我們可以將單位重復(fù)間隔設(shè)置為分鐘,小時,天,周,年等。我希望重復(fù)間隔應(yīng)該是4個小時。因此,每4個小時就會收到一次本地通知。我不希望用戶為每個設(shè)置單獨的通知。我希望用戶能夠?qū)epeatInterval設(shè)置為4小時。我怎么做?iphone 
查看完整描述

3 回答

?
拉莫斯之舞

TA貢獻(xiàn)1820條經(jīng)驗 獲得超10個贊

得到了答案,它是盡可能直接的。

您無法創(chuàng)建自定義重復(fù)間隔。

您必須使用NSCalendarUnit的內(nèi)置單位時間間隔。

我嘗試了上述所有解決方案,甚至嘗試了其他方法,但是它們都不起作用。

我花了很多時間來發(fā)現(xiàn)沒有辦法讓它在自定義時間間隔內(nèi)工作。


查看完整回答
反對 回復(fù) 2019-11-15
?
尚方寶劍之說

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)建通知。


查看完整回答
反對 回復(fù) 2019-11-15
  • 3 回答
  • 0 關(guān)注
  • 722 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號