我使用以下代码行来获取本地通知。
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点从该阵列随机通知消息。
从上面的代码我每天都得到同样的信息。我想要来自该数组的随机消息。
先谢谢。!
答案 0 :(得分: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];
}
}