2014-06-12 12 views
0

我使用以下代碼行獲取本地通知。使用UILocalNotification從Array獲取隨機消息的問題

Class cls = NSClassFromString(@"UILocalNotification"); 
if (cls != nil) { 

    UILocalNotification *notif = [[cls alloc] init]; 
    notif.fireDate = itemDate; 
    notif.timeZone = [NSTimeZone defaultTimeZone]; 

    NSMutableArray *newArray=[[self appDelegate].defaultsvalue objectForKey:@"ArrayMessage"]; 

    //int index = arc4random() % [newArray count]; 
    notif.alertBody =[newArray objectAtIndex:arc4random() % [newArray count]]; 

    notif.alertAction = @"Show me"; 
    notif.soundName = UILocalNotificationDefaultSoundName; 
    notif.applicationIconBadgeNumber = 1; 

    notif.repeatInterval = NSDayCalendarUnit; 

    NSDictionary *userDict = [NSDictionary dictionaryWithObject:[newArray objectAtIndex:arc4random() % [newArray count]] 
                 forKey:@"kRemindMeNotificationDataKey"]; 
    notif.userInfo = userDict; 

    [[UIApplication sharedApplication] scheduleLocalNotification:notif]; 
} 

從上面的代碼「newArray」是NSMutable Array,它存儲了100條消息。現在,我想每天早上8:00從該陣列隨機通知消息。

從上面的代碼我每天都收到相同的消息。我想從該數組中隨機發送消息。

謝謝你..!

+0

你有什麼問題?更新你的問題。 – Harin

+0

你的問題沒有意義。請重寫它。 – trojanfoe

+0

現在請參考問題 – jigs

回答

0

嘗試這樣的,你必須創建多個通知「對象」

NSInteger intervals = 0; 
    for (int i =0; i<60; i++) { 
     // creating notification interval 
     // where 60 = seconds, so add your seconds here 
     intervals = intervals + 60; 
     NSDate *newDate = [NSDate dateWithTimeIntervalSinceNow:intervals]; 
     Class cls = NSClassFromString(NSStringFromClass([UILocalNotification class])); 
     if (cls != nil) { 
      UILocalNotification *notification = [[cls alloc] init]; 
      notification.fireDate = newDate; 
      notification.timeZone = [NSTimeZone localTimeZone]; 
      notification.alertAction = @"Clear"; 

      //if you want to repeat same notification with some time interavals 
      notification.repeatInterval = kCFCalendarUnitSecond; 

      //notification with sound 
      notification.soundName = UILocalNotificationDefaultSoundName; 

      //notification with vibrate 
      AudioServicesPlayAlertSound(kSystemSoundID_Vibrate); 

      if (array.count>0) { 
       NSInteger ran = arc4random() % array.count; 
       notification.alertBody = [self randomMessage:ran]; 
       NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"ANYIDENTIFIER" forKey:kRemindMeNotificationDataKey]; 
       notification.userInfo = userDict; 
      } 

      [[UIApplication sharedApplication] scheduleLocalNotification:notification]; 
     } 
    }