2013-07-26 28 views
7

我正在開發一個日曆應用程序,其中包含由生日組成的多個事件(例如500個事件)。我正在從Web服務下載事件。我想在生日的上午10點的特定時間向用戶提供每個生日的警報。我使用本地通知來安排警報,但由於iOS僅允許每個應用程序發出64個通知,並且我有多個生日,所以我陷入了僵局。一旦應用超出限制,我不知道如何安排更多通知。請建議我如何解決這個問題。下面是我的代碼安排的通知如何在通知限制爲64時在iOS中安排更多本地通知

- (void)scheduleNotification:(NSMutableArray *)datesArray withMessage:(NSMutableArray *)messagesArray NotificationID:(NSString *)notificationID 
{ 

    NSCalendar *calendar = [NSCalendar currentCalendar]; 

    NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; 
    [formatter setDateStyle:NSDateFormatterNoStyle]; 
    [formatter setTimeStyle:NSDateFormatterShortStyle]; 


    NSDateFormatter *formatter1 = [[NSDateFormatter alloc]init]; 
    [formatter1 setDateStyle:NSDateFormatterMediumStyle]; 
    [formatter1 setDateFormat:@"dd/MM/yyyy"]; 

    // NSDate *myTime = [formatter dateFromString:@"06:10 PM"]; 
    NSDate *myTime = [formatter dateFromString:fireTime]; 

    for (int i = 0; i < [datesArray count]; i++) 
    { 
     NSDate *myDate = [formatter1 dateFromString:[datesArray objectAtIndex:i]]; 

     NSDateComponents *dateComponents = [calendar components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:myDate]; 
     NSDateComponents *timeComponents = [calendar components:NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:myTime]; 

     NSDateComponents *newCompoents = [[NSDateComponents alloc]init]; 

     NSDateComponents *todayComponents = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]]; 

     int day = [dateComponents day]; 
     int month = [dateComponents month]; 

     [newCompoents setDay:[dateComponents day]]; 
     [newCompoents setMonth:[dateComponents month]]; 

     if (day >= [todayComponents day] && month >= [todayComponents month]) { 

      NSLog(@"day = %d, month = %d", day, month); 
      [newCompoents setYear:[todayComponents year]]; 
     } 
     else{ 
      NSLog(@"%d, %d", day, month); 
      [newCompoents setYear:[todayComponents year]+1]; 
     } 


     [newCompoents setHour:[timeComponents hour]]; 
     [newCompoents setMinute:[timeComponents minute]]; 

     NSDate *combDate = [calendar dateFromComponents:newCompoents]; 

     localNotif = [[UILocalNotification alloc] init]; 
     if (localNotif == nil) 
      return; 
     localNotif.fireDate = combDate; 
     localNotif.timeZone = [NSTimeZone defaultTimeZone]; 

     // Notification details 

     NSString *message = [@"Wish" stringByAppendingFormat:@" %@%@", [messagesArray objectAtIndex:i],@" On His Birthday"]; 
     localNotif.alertBody = message; 

     // Set the action button 
     localNotif.alertAction = @"View"; 

     localNotif.soundName = UILocalNotificationDefaultSoundName; 
     localNotif.applicationIconBadgeNumber = 1; 

     // Specify custom data for the notification 
     NSDictionary *infoDict = [NSDictionary dictionaryWithObject:notificationID forKey:notificationID]; 
     localNotif.userInfo = infoDict; 

     // Schedule the notification 
     [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
    } 

} 

糾正我,如果我犯錯誤,並建議如何一次64限制交叉安排更多的通知。

+0

國際海事組織你可以重新安排LocalNotification當你打開你的應用程序,如檢查總計劃localNotifications計數,如果它小於64,重新安排那裏.. – Bala

+0

,只是看看這個答案http://stackoverflow.com/a/7721445/1059705 – Bala

+0

是的,我可以使用該方法計算通知的總數。當你打開你的應用時,你的意思是什麼。我是否需要檢查我的應用程序委託中的計數並安排更多通知。 – suhit

回答

1

我有一個類似的問題和一些額外的選項,可以幫助你,雖然他們都有缺陷,這意味着正確的行爲不能保證。

請記住,重疊的生日在這裏可能很有用。如果兩個人最終在同一天生日,則取消並重新安排他們的兩個相關信息。如果您還可以使用通用消息,則可以找到發生重複的其他間隔。

顯然,鼓勵您的用戶按通知以打開應用程序將有所幫助(可能與您的廣告收入)。你可以爲你最後一個嘗試一個好消息。

這很不方便,但您可以使用geofencing(但可能不是遠程通知),如下所述:Can push notifications be used to run code without notifying user?。如果您添加使用它的功能,您甚至可以獲得批准。

我已經考慮過傳遞一個自定義日曆,但NSCalendar免費橋接到CFCalendarRef,故事似乎是我不會有運氣試圖挖掘(至少得到批准)。我很高興被告知我錯了。