ios10 内未触发本地通知
Local notifications are not firing in ios10
我正在为 ios 10 使用 UNUserNotificationCenter
。为了测试,我将本地通知设置为从当前时间起 10 秒。
这是我试过的,
- (void)viewDidLoad {
[super viewDidLoad];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(@"request succeeded!");
[self set10Notifs];
}
}];
}
-(void) set10Notifs
{
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
#if XCODE_VERSION_GREATER_THAN_OR_EQUAL_TO_8
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
NSDateComponents *components = [calendar components:NSCalendarUnitTimeZone fromDate:[[NSDate date] dateByAddingTimeInterval:10]];
UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
objNotificationContent.title = [NSString localizedUserNotificationStringForKey:@"Prayer!" arguments:nil];
objNotificationContent.body = [NSString localizedUserNotificationStringForKey:@"Time now"
arguments:nil];
objNotificationContent.sound = [UNNotificationSound defaultSound];
UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"Prayer"
content:objNotificationContent trigger:trigger];
UNUserNotificationCenter *userCenter = [UNUserNotificationCenter currentNotificationCenter];
[userCenter addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"Local Notification succeeded");
}
else {
NSLog(@"Local Notification failed");
}
}];
#endif
}
}
我可以看到日志"Local Notification succeeded"。但是本地通知没有在设备中触发。
在 Appdelegate 中,我添加了
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
return YES;
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
NSLog(@"Notification is triggered");
completionHandler(UNNotificationPresentationOptionBadge);
}
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
NSLog(@"User Info : %@",response.notification.request.content.userInfo);
completionHandler();
}
我做错了什么?
为什么没有触发应用程序通知?
像这样设置 NSDateComponents:
NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitTimeZone fromDate:[[NSDate date] dateByAddingTimeInterval:10]];
或者使用时间触发器而不是日历触发器
@IBAction func sendNotification(_ sender: Any) {
let content = UNMutableNotificationContent()
content.title = "Hello"
content.body = "Ved Rauniyar !!!"
// content.badge = 1
content.sound = UNNotificationSound.default()
// Deliver the notification in five seconds.
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: false)
let url = Bundle.main.url(forResource:"ved", withExtension: "png")
let attachment = try? UNNotificationAttachment(identifier: "FiveSecond",
url: url!,
options: [:])
content.attachments = [attachment!]
let request = UNNotificationRequest.init(identifier: "FiveSecond", content: content, trigger: trigger)
// Schedule the notification.
let center = UNUserNotificationCenter.current()
center.add(request) { (error) in
// print(error!)
}
}
我正在为 ios 10 使用 UNUserNotificationCenter
。为了测试,我将本地通知设置为从当前时间起 10 秒。
这是我试过的,
- (void)viewDidLoad {
[super viewDidLoad];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(@"request succeeded!");
[self set10Notifs];
}
}];
}
-(void) set10Notifs
{
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
#if XCODE_VERSION_GREATER_THAN_OR_EQUAL_TO_8
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
NSDateComponents *components = [calendar components:NSCalendarUnitTimeZone fromDate:[[NSDate date] dateByAddingTimeInterval:10]];
UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
objNotificationContent.title = [NSString localizedUserNotificationStringForKey:@"Prayer!" arguments:nil];
objNotificationContent.body = [NSString localizedUserNotificationStringForKey:@"Time now"
arguments:nil];
objNotificationContent.sound = [UNNotificationSound defaultSound];
UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"Prayer"
content:objNotificationContent trigger:trigger];
UNUserNotificationCenter *userCenter = [UNUserNotificationCenter currentNotificationCenter];
[userCenter addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"Local Notification succeeded");
}
else {
NSLog(@"Local Notification failed");
}
}];
#endif
}
}
我可以看到日志"Local Notification succeeded"。但是本地通知没有在设备中触发。
在 Appdelegate 中,我添加了
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
return YES;
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
NSLog(@"Notification is triggered");
completionHandler(UNNotificationPresentationOptionBadge);
}
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
NSLog(@"User Info : %@",response.notification.request.content.userInfo);
completionHandler();
}
我做错了什么? 为什么没有触发应用程序通知?
像这样设置 NSDateComponents:
NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitTimeZone fromDate:[[NSDate date] dateByAddingTimeInterval:10]];
或者使用时间触发器而不是日历触发器
@IBAction func sendNotification(_ sender: Any) {
let content = UNMutableNotificationContent()
content.title = "Hello"
content.body = "Ved Rauniyar !!!"
// content.badge = 1
content.sound = UNNotificationSound.default()
// Deliver the notification in five seconds.
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: false)
let url = Bundle.main.url(forResource:"ved", withExtension: "png")
let attachment = try? UNNotificationAttachment(identifier: "FiveSecond",
url: url!,
options: [:])
content.attachments = [attachment!]
let request = UNNotificationRequest.init(identifier: "FiveSecond", content: content, trigger: trigger)
// Schedule the notification.
let center = UNUserNotificationCenter.current()
center.add(request) { (error) in
// print(error!)
}
}