簡體   English   中英

在本地通知中設置repeatInterval

[英]Set repeatInterval in local notification

我想將重復間隔設置為用戶從日期選擇器中選擇的值。我在應用程序中使用了類型為倒數模式的日期選擇器。如果用戶從日期選擇器中選擇了4小時15分鍾,那么我將使用以下代碼和警報聲來設置觸發日期。

 [NSDate dateWithTimeIntervalSinceNow:[pickerTimer countDownDuration]] 

但我希望該通知應每隔4小時15分鍾重復一次,直到用戶取消通知為止。 我已經做了很多R&D搜尋工作,但我不知道。到目前為止,我使用的程式碼是:

localNotification = [[UILocalNotification alloc] init]; 
[localNotification setFireDate:[NSDate dateWithTimeIntervalSinceNow:[pickerTimer countDownDuration]]]; 

if(localNotification.fireDate){

    [self _showAlert:@"Time is scheduled" withTitle:@"Daily Achiever"];
}
localNotification.timeZone = [NSTimeZone systemTimeZone];


localNotification.alertBody=@"alaram";
localNotification.soundName = UILocalNotificationDefaultSoundName;
[localNotification setAlertAction:@"View"];
[localNotification setRepeatInterval:[pickerTimer countDownDuration]];

//The button's text that launches the application and is shown in the alert
// [localNotification setAlertBody:[alertBodyField text]]; //Set the message in the notification from the textField's text
//[localNotification setHasAction: YES]; //Set that pushing the button will launch the application
[localNotification setApplicationIconBadgeNumber:[[UIApplication sharedApplication] applicationIconBadgeNumber]]; //Set the Application Icon Badge Number of the application's icon to the current Application Icon Badge Number plus 1

  localNotification.applicationIconBadgeNumber = 1;

localNotification.repeatInterval=NSHourCalendarUnit;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; //Schedule the notification with the system

//[alertNotification setHidden:NO]; //Set the alertNotification to be shown showing the user that the application has registered the local notification

請幫我解決。 非常感謝。

我們無法在UILocalNotificationrepeatInterval設置自定義值。

您可以通過每4.25小時創建一個新的localNotification並復制現有通知的詳細信息並在處理本地通知時將其刪除來實現此目的的最簡單方法。

另一種方法是在處理本地通知時更改firedate

我已經使用以下代碼解決了該問題:

-(void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {

testDate=notif.fireDate;
NSLog(@"Recieved Notification %@",notif);
[self playSoundWithNotification:notif];
[self _showAlert:@"Alaram" withTitle:@"Daily Achiever"];
 }


-(void)_showAlert:(NSString*)pushmessage withTitle:(NSString*)title
{

UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title message:pushmessage delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

[alertView show];

if (alertView) {
    FocusViewController *fvc=[[FocusViewController alloc]initWithNibName:@"FocusViewController" bundle:nil];

    [fvc insert:[NSDate dateWithTimeInterval:refTimeIntrval sinceDate:testDate]];

}}

這里的testDate和refTimeInterval是.pch文件中聲明的變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM