Reputation: 4409
how to schedule random message for 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");
}];
}
Its shows same message again and again!
Upvotes: 0
Views: 287
Reputation: 53
If you do not want to repeat this notification again and again set the repeats interval NO
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 * 60 * 24 repeats:NO];
Upvotes: 1
Reputation: 3016
Here is one way to do this
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");
}];
}
Upvotes: 0