如何为localNotification安排随机消息。
- (void)viewDidLoad {
// Do any additional setup after loading the view.
[super viewDidLoad];
[self scheduleDailyLocalNotification];
}
-(void)scheduleDailyLocalNotification{
UNMutableNotificationContent *localNotification = [UNMutableNotificationContent new];
localNotification.title = [NSString localizedUserNotificationStringForKey:@“Title!” arguments:nil];
srand(time(NULL));
int r = rand() % 6;
localNotification.body = [NSString localizedUserNotificationStringForKey:[self.notificationMessages objectAtIndex:r] arguments:nil];
localNotification.sound = [UNNotificationSound defaultSound];
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 * 60 * 24 repeats:YES];
// UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"Time for a run!" content:localNotification trigger:trigger];
NSString *identifier = @"LOCALNOTIFICATION";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier
content:localNotification trigger:trigger];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
NSLog(@"NOTIFICATION CREATED");
}];
}它一次又一次地显示相同的信息!
发布于 2018-07-13 21:57:50
如果您不想一次又一次地重复此通知,请将重复间隔设置为
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 * 60 * 24 repeats:NO];发布于 2018-07-13 19:55:55
这里有一种方法可以做到这一点
var index = 0
- (void)viewDidLoad {
// Do any additional setup after loading the view.
[super viewDidLoad];
[self scheduleDailyLocalNotification];
}
-(void)scheduleDailyLocalNotification{
UNMutableNotificationContent *localNotification = [UNMutableNotificationContent new];
localNotification.title = [NSString localizedUserNotificationStringForKey:@“Title!” arguments:nil];
srand(time(NULL));
localNotification.body = [NSString localizedUserNotificationStringForKey:[self.notificationMessages objectAtIndex:index] arguments:nil];
if index < self.notificationMessage.count{
index += 1
}else{
index = 0
}
localNotification.sound = [UNNotificationSound defaultSound];
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 * 60 * 24 repeats:YES];
// UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"Time for a run!" content:localNotification trigger:trigger];
NSString *identifier = @"LOCALNOTIFICATION";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier
content:localNotification trigger:trigger];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
NSLog(@"NOTIFICATION CREATED");
}];
}https://stackoverflow.com/questions/51323417
复制相似问题