2014-09-04 79 views
1

我已經在我的項目上實現了UILocalNotification成功,我在隨機化字符串失敗時出現了NSDictionary,我需要每5分鐘發送一次整個世界。每隔5分鐘,我需要在UILocalNotification中傳遞隨機字符串。使用UILocalNotification與隨機化字符串

這裏是我的示例代碼:

NSArray *word1Array= [_tmp objectForKey:@"word1"]; 

NSArray *word2Array = [_tmp objectForKey:@"word2"]; 

if([word1Array count] > 0) 
{ 
    int minCount = 0; 

    int totalcount = (int)[word1Array count]; 

    int randomIndex = (arc4random()%(totalcount-minCount))+minCount; 

    NSString *word1 = [word1Array objectAtIndex:randomIndex]; 

    NSString *word2 = [word2Array objectAtIndex:randomIndex]; 

    NSString *wordbody = [NSString stringWithFormat:@"%@ - %@",word1,word2]; 

    UILocalNotification *reminderNote = [[UILocalNotification alloc]init]; 

    reminderNote.repeatInterval = NSMinuteCalendarUnit; 

    reminderNote.alertBody = wordbody; 

    reminderNote.alertAction = @"Bak"; 

    reminderNote.soundName = @"sound.aif"; 

    reminderNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:60 * 5]; 

    reminderNote.applicationIconBadgeNumber = 1; 

    [[UIApplication sharedApplication] scheduleLocalNotification:reminderNote]; 


} 

此代碼開始與隨機字符串,但是每5分鐘發送通知,它通過4-5個字同時這實在是煩人。我如何解決它只發送每5分鐘一個字符串?

回答

1

如果我正確理解你的問題,你會在第5分鐘得到相同的字符串N次。這是因爲您同時安排本地通知。

你應該把上面的代碼到的東西,如: -

for(int i=1;i<=5 ;i++){ 
    //Your other code 

    reminderNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:60 * 5 * i]; 

    //Your other code 
} 

上面的代碼將安排到本地通知5次,將在分5,10,15,20,25通知用戶從當前時間開始。

更新應答: -

設備上的每個應用程序被限制爲64個定本地 通知。系統放棄超出此限制的 的預定通知,只保留64個通知,以最快速度觸發 。定期通知被視爲單個通知。

參見:https://developer.apple.com/Library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/WhatAreRemoteNotif.html

計劃陸續新的本地通知:UILocalNotification with various alert body

+0

怎麼樣,如果我想使它無限循環。我的意思是它會在5分鐘內發送本地通知。它會每隔5分鐘發送一次,而不是5個字* 5 = 25分鐘。 – 2014-09-04 14:00:57

+0

不幸的是,一切都有限制。您最多可以有64個預定的本地通知。除非你找到了一個接一個聰明的方法。查看上面更新的答案。 – Ricky 2014-09-04 14:03:41

+0

注意:**定期通知**將具有相同的警報消息。這可能會在一個又一個的時間表上幫助:http://stackoverflow.com/questions/18792997/uilocalnotification-with-various-alert-body – Ricky 2014-09-04 14:14:53