Objective-C: 使用 UNUserNotificationCenter 设置定时器显示提醒
Objective-C: Set timer to display a reminder using UNUserNotificationCenter
我希望在时间到时在应用程序的前台或后台显示自定义提醒或弹出消息。我试过 但似乎没有用。
启动我的应用程序时没有任何反应。它会说本地通知成功,但我没有看到任何弹出的自定义消息框。请帮忙。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//===Every 20 secs I will call setSchedule method
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(setSchedule) userInfo:nil repeats:YES];
return YES;
}
-(void)setSchedule{
NSDate *now = [NSDate date];
NSLog(@"NSDate:%@",now);
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSTimeZoneCalendarUnit fromDate:now];
UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
objNotificationContent.title = [NSString localizedUserNotificationStringForKey:@"Notification!" arguments:nil];
objNotificationContent.body = [NSString localizedUserNotificationStringForKey:@"This is local notification message!" arguments:nil];
objNotificationContent.sound = [UNNotificationSound defaultSound];
/// 4. update application icon badge number
objNotificationContent.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);
UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"ten" content:objNotificationContent trigger:trigger];
/// 3. schedule localNotification
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"Local Notification succeeded");
}
else {
NSLog(@"Local Notification failed");
}
}];
}
首先是 UNUserNotification
没有在前台开火。
第二,如果你想每 20 秒或任何时间间隔触发通知,那么不要使用 UNCalendarNotificationTrigger
。
检查下面的触发方法并在您的代码中设置。
let trigger2 = UNTimeIntervalNotificationTrigger(timeInterval: YourInterval, repeats: true)
我希望在时间到时在应用程序的前台或后台显示自定义提醒或弹出消息。我试过
启动我的应用程序时没有任何反应。它会说本地通知成功,但我没有看到任何弹出的自定义消息框。请帮忙。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//===Every 20 secs I will call setSchedule method
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(setSchedule) userInfo:nil repeats:YES];
return YES;
}
-(void)setSchedule{
NSDate *now = [NSDate date];
NSLog(@"NSDate:%@",now);
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSTimeZoneCalendarUnit fromDate:now];
UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
objNotificationContent.title = [NSString localizedUserNotificationStringForKey:@"Notification!" arguments:nil];
objNotificationContent.body = [NSString localizedUserNotificationStringForKey:@"This is local notification message!" arguments:nil];
objNotificationContent.sound = [UNNotificationSound defaultSound];
/// 4. update application icon badge number
objNotificationContent.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);
UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"ten" content:objNotificationContent trigger:trigger];
/// 3. schedule localNotification
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"Local Notification succeeded");
}
else {
NSLog(@"Local Notification failed");
}
}];
}
首先是 UNUserNotification
没有在前台开火。
第二,如果你想每 20 秒或任何时间间隔触发通知,那么不要使用 UNCalendarNotificationTrigger
。
检查下面的触发方法并在您的代码中设置。
let trigger2 = UNTimeIntervalNotificationTrigger(timeInterval: YourInterval, repeats: true)