iOS 10,当应用程序处于前台时显示本地通知?
Posted
技术标签:
【中文标题】iOS 10,当应用程序处于前台时显示本地通知?【英文标题】:iOS 10, Local notification show when app is in foreground? 【发布时间】:2016-10-14 12:56:54 【问题描述】:我想在应用程序处于前台时显示通知。下面是我为新的 usernotificationdelegate 方法做的代码。
在应用委托中:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0"))
//ios 10 handling
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error)
if (!error)
NSLog(@"request authorization succeeded!");
];
#pragma mark - User notification delegate
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
NSLog(@"willPresentNotification");
NSLog(@"%@", notification.request.content.userInfo);
completionHandler (UNNotificationPresentationOptionAlert);
这是我触发本地通知的方法
-(void) fireLocalNotification:(NSString *) message
NSLog(@"fire Local Notification");
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0"))
//Notification Content
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.body = [NSString stringWithFormat:@"%@",message];
content.sound = [UNNotificationSound defaultSound];
//Set Badge Number
content.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);
// Deliver the notification in five seconds.
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:1.0f repeats:NO];
//Notification Request
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"LocalNotification" content:content trigger:trigger];
//schedule localNotification
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error)
if (!error)
NSLog(@"add Notification Request succeeded!");
];
现在这样做之后我仍然没有在前台收到通知。
提前致谢。
【问题讨论】:
参考我的回答from here它可能对你有帮助!willPresentNotification
是否出现在控制台日志中?如果应用不在在前台,您会看到通知警报吗?
是的@matt,当我的应用程序不在前台时,我会看到警报,即通知,但它没有出现在前台。
@Lion,您提到的答案是针对 UILocalNotification。我想使用 iOS 10 UserNotification 框架。用于在前台显示通知。
另外,还有一件事……我已经设置了委托,但没有调用 UserNotification 委托。
【参考方案1】:
请实现这个功能:
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
completionHandler(UNNotificationPresentationOptionAlert + UNNotificationPresentationOptionSound);
【讨论】:
在他给出的示例中不明白这已经实现了吗?【参考方案2】:为了在应用程序处于前台时获得通知,您需要在 iOS10 中实现 UNUserNotificationCenterDelegate 的以下委托方法。
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
completionHandler([.alert,.badge])
有关更多信息,您可以参考苹果文档。这是链接:
https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate?language=objc
【讨论】:
【参考方案3】:请参阅此代码以在前台处理本地通知。 /*******************************/
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNAuthorizationOptions options = UNAuthorizationOptionAlert + UNAuthorizationOptionSound;
[center requestAuthorizationWithOptions:options
completionHandler:^(BOOL granted, NSError * _Nullable error)
if (!granted)
//NSLog(@"Something went wrong");
];
int dayCounter =5;
int minute = 48;
NSDateComponents *components = [[NSDateComponents alloc] init];
components.weekday = dayCounter;
dayCounter++;
components.hour = 12;
components.minute = minute;
UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];
UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
objNotificationContent.title = [NSString localizedUserNotificationStringForKey:@"Notification!" arguments:nil];
objNotificationContent.body = [NSString localizedUserNotificationStringForKey:@"We made a surprise Edit for You" arguments:nil];
objNotificationContent.sound = [UNNotificationSound defaultSound];
objNotificationContent.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);
UNNotificationAttachment *attachment = nil;
NSURL* outputURL = [[NSURL alloc] initFileURLWithPath:filePath];
NSError *attachmentError = nil;
attachment = [UNNotificationAttachment attachmentWithIdentifier:@"image"
URL: outputURL
options:nil
error:&attachmentError];
if (attachmentError)
return;
objNotificationContent.attachments=@[attachment];
NSString *identifier = @"UYLLocalNotification";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier
content:objNotificationContent trigger: trigger];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error)
if (error != nil)
NSLog(@"Something went wrong: %@",error);
else
];
/****************************/
// 处理 -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive)
dispatch_async(dispatch_get_main_queue(), ^
UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Reminder" message:notification.alertBody delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Yes", nil];
alert.tag = 330;
[alert show];
);
else
// handle for background
【讨论】:
【参考方案4】:要在应用程序处于前台时显示横幅消息,请使用以下方法。
将其添加到 AppDelegate 中的委托方法并符合此协议 - UNUserNotificationCenterDelegate
iOS 10、斯威夫特 3:
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
completionHandler(UNNotificationPresentationOptions.alert)
【讨论】:
【参考方案5】:在屏幕顶部显示警报
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
completionHandler(UNNotificationPresentationOptionAlert + UNNotificationPresentationOptionSound);
【讨论】:
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler completionHandler(UNNotificationPresentationOptionAlert + UNNotificationPresentationOptionSound);以上是关于iOS 10,当应用程序处于前台时显示本地通知?的主要内容,如果未能解决你的问题,请参考以下文章
当应用程序在 iOS 10 的前台时,不会调用“willPresent 通知”并且本地通知不会显示