gpt4 book ai didi

iOS 10,应用程序在前台时显示本地通知?

转载 作者:行者123 更新时间:2023-12-01 17:17:57 26 4
gpt4 key购买 nike

我想在应用程序处于前台时显示通知。下面是我为新的 usernotificationdelegate 方法所做的代码。

在应用委托(delegate)中:

- (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!");
}
}];

}
}

现在在执行此操作后我仍然没有在前台收到通知。

提前致谢

最佳答案

请实现这个功能:

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
completionHandler(UNNotificationPresentationOptionAlert + UNNotificationPresentationOptionSound);
}

关于iOS 10,应用程序在前台时显示本地通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40043769/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com