亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

如何將本地通知重復間隔設置為自定義時間間隔?

如何將本地通知重復間隔設置為自定義時間間隔?

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

3 回答

?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

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

您無法創建自定義重復間隔。

您必須使用NSCalendarUnit的內置單位時間間隔。

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

我花了很多時間來發現沒有辦法讓它在自定義時間間隔內工作。


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

TA貢獻1788條經驗 獲得超4個贊

我有一個想法如何做到這一點,我已經在我的項目中實現了


首先,使用火災日期(例如,每分鐘)創建本地通知。下一步-使用此通知的唯一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

如果您要刪除此通知,請執行


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當您處于后臺時,請勿調用。因此,您必須設置長時間運行的后臺任務,在該任務中您將再次創建通知。


查看完整回答
反對 回復 2019-11-15
  • 3 回答
  • 0 關注
  • 731 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號