gpt4 book ai didi

ios - 本地通知 View 显示在前台

转载 作者:行者123 更新时间:2023-12-01 18:37:32 28 4
gpt4 key购买 nike

我需要在前台显示本地通知。我读到这在 iOS10 UserNotifications 中的介绍中是可能的框架。我尝试实现它并仅在后台获取通知。在前台 -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification被调用但未显示通知 View 。我希望它同时显示在前景和背景上,可以点击并在点击时触发一个方法。这可能吗?如果是,我的代码中缺少什么。这是代码:
在 AppDelegate 中:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
UNAuthorizationOptions options = UNAuthorizationOptionAlert + UNAuthorizationOptionSound;
[center requestAuthorizationWithOptions:options
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!granted) {
NSLog(@"Something went wrong");
}
}];

return YES;
}

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
NSLog(@"Will present notification");
}

-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
NSLog(@"Did Receive Notification Response");
}

在 View Controller 中:
- (void)locationManagerDidEnterRegionOfCamera:(NSString *)cameraName {
NSDictionary *info = @{ kRegionNotificationDictionaryCameraName : cameraName };

// Check if the switch was previously set to off and not fire notification
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"switch"]) {

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0.0")) {
UNMutableNotificationContent *content = [UNMutableNotificationContent new];
content.title = @"Don't forget";
content.body = @"Buy some milk";
content.sound = [UNNotificationSound defaultSound];

UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:1
repeats:NO];

NSString *identifier = @"UYLLocalNotification";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier
content:content trigger:trigger];

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Something went wrong: %@",error);
} else {

}
}];

} else {
[NotificationsHelper scheduleRegionNotificationWithInfo:info];
}
}
}

最佳答案

重要的方法是这个:userNotificationCenter:willPresentNotification:withCompletionHandler:
来自 documentation :

completionHandler The block to execute with the presentation option for the notification. Always execute this block at some point during your implementation of this method. Specify an option indicating how you want the system to alert the user, if at all. This block has no return value and takes the following parameter:

options The option for notifying the user. Specify UNNotificationPresentationOptionNone to silence any alerts. Pass other values to specify which types of alerts you want to allow. For information about the available alert options, see UNNotificationPresentationOptions.



关于 Apple Programming Guide 的其他信息

有什么计划:

假设您收到两种类型的通知,您想以不同的方式处理它们,一种是即使应用程序在前台也想显示警报,另一种只是声音。
这就是你使用这种方法的原因。

分析 UNNotification ,并根据您的选择,最后调用 completionHandler(someValue) , 其中 someValue UNNotificationPresentationOptions (它实际上是一个标志,你应该能够将它们组合起来)。你至少要的是 UNNotificationPresentationOptionAlert .

示例代码:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
UNNotificationPresentationOptions options;
//Read the notification object, parse some info, decide on what do to by settings options
if (something)
{
//Show badge
options = UNNotificationPresentationOptionBadge;
}
else if (somethingElse)
{
//Show alert and make sound
options = UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert;
}
else ...

completionHandler(options);
}

关于ios - 本地通知 View 显示在前台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49902505/

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