objective-c - 如何在 localNotification 上安排随机消息
问题描述
如何为 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");
}];
}
它一次又一次地显示相同的信息!
解决方案
如果您不想一次又一次地重复此通知,请设置重复间隔NO
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 * 60 * 24 repeats:NO];
推荐阅读
- bash - 使用 sed 和 grep 的脚本会产生意外的输出
- c++ - 错误 C2679:二进制“>>”:未定义采用“”类型右侧操作数的运算符(或没有可接受的转换)执行错误
- angular - 使用平台浏览器进行离子社交登录,我尝试在我的项目中使用 openfb 如何使用?
- mysql - 使用 MySQL 的 INSERT IGNORE 来防止重复条目(性能问题?)
- sql - Oracle / PLSQL:SELECT 语句出现 ORA-00933 错误
- python-3.x - 更改文本文档的值时遇到问题
- acumatica - 在 Acumatica 中使用自定义屏幕的批准
- dart - 导航器在 Firebase 消息回调方法中不起作用
- php - 将默认值动态设置为变化
- android - 从回收站列表中删除的数据,但不是从 Sqlite 数据库中删除的数据