2013-06-05 42 views
1

我正在通過收集365個英語「sayings」來創建應用程序。在我的應用程序中,我想使用本地通知(每天從我的應用程序發出的言論應通知給用戶)。我想使用本地通知,而不是推送notification.is有可能每天(365天)通知用戶不同的說法。可以任何人請幫助我.....如何在iOS中創建365天的本地通知

+0

只能設置64 ['UILocalNotifications'](http://developer.apple.com/library/ios/#documentation/iPhone/Reference/UILocalNotification_Class /Reference/Reference.html)爲你的應用程序。 – rckoenes

+1

這是Apple的官方教程:http://developer.apple.com/library/ios/#DOCUMENTATION/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW13 – Raptor

+1

查看Adi鏈接中提到的教程,邏輯將保存說明到數據庫中,並在每次通知被觸發時選擇一個新的說法。最簡單的方法是使用索引作爲從數據庫中讀取的年份。 –

回答

1

有沒有辦法創建365個不同的本地通知。 根據documentation本地通知限於64個預定的本地通知。

但是,當用戶啓動您的應用程序時,您可以更新通知。 如果通知的內容是一樣的,你也可以安排與365個字典一個recurring basis

0

是你can.Just創建陣列交付通知。字典鍵應該是id和說。

UILocalNotification *localNotif = [[UILocalNotification alloc]init]; 
NSCalendar *calendar = [NSCalendar currentCalendar]; 
    NSDate *datePickerDate = [NSDate date]; 
    NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:datePickerDate]; 
    NSDateComponents *timeComponents = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:datePickerDate]; 

    NSDateComponents *dateComps = [[NSDateComponents alloc]init]; 
    [dateComps setYear:[dateComponents year]]; 
    [dateComps setMonth:[dateComponents month]]; 

    [dateComps setDay:[dateComponents day] + 1]; 

    [dateComps setHour:[timeComponents hour] ]; 
    [dateComps setMinute:[timeComponents minute]]; 
    [dateComps setSecond:00]; 

    NSDate *fireDate = [calendar dateFromComponents:dateComps]; 
    //NSLog(@"%@",fireDate); 
    localNotif.fireDate = fireDate; 
    localNotif.soundName = UILocalNotificationDefaultSoundName; 
    localNotif.alertAction = @"Alarm"; 
    localNotif.repeatInterval = NSDayCalendarUnit; 
    localNotif.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"id", nil]; 
    localNotif.alertBody = [saying with id 1];//you can use predicate here 
    [[UIApplication sharedApplication]scheduleLocalNotification:localNotif]; 
在應用程序委託

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { 
     int i = [[localNotif.userInfo objectForKey:@"id"] intValue] + 1; 
     localNotif.alertBody = [saying in index i ]; 
     [localNotif.userInfo setValue:i forKey:@"id"]; 
} 
+0

你的代碼有很多問題,首先你只能註冊64個通知,Seconde的'application:didReceiveLocalNotification:'只會觸發你的應用程序在觸發通知時運行。 – rckoenes

+0

@rckoenes:謝謝你的評論。實際上它不是365通知。每天只有一個重複間隔的通知。 2.你是對的。應用程序:didReceiveLocalNotification:僅在用戶收到它時調用。但這是唯一的可能性。否則你怎麼能做到這一點? – manujmv

+0

那麼這是問題的關鍵。只有64個註冊通知。您也可以通過檢查選項是否包含本地通知來檢查應用程序是否以'application:didFinishLaunchingWithOptions:'中的本地通知開始。一種可以使其工作的方式是在每次啓動應用程序時註冊64通知,這樣應用程序將始終在上次啓動後的63天內運行。但再次不是100%可靠。 – rckoenes