本地通知中未显示的操作
Posted
技术标签:
【中文标题】本地通知中未显示的操作【英文标题】:actions not showing in local notification 【发布时间】:2017-04-28 19:29:26 【问题描述】:我正在尝试使用 2 个操作生成本地通知。但是,即使弹出通知。我看不到动作。
这是因为 ios 10 中的新功能吗
我的视图控制器,h 有代码:
-(void)createNotifications: (int)seconds
UILocalNotification *local = [[UILocalNotification alloc]init];
local.fireDate = [[NSDate date]dateByAddingTimeInterval:seconds];
local.timeZone = nil;
local.alertBody = @"Alert body";
local.alertTitle = @"Alert Title";
local.alertAction = @"Okay";
local.soundName = UILocalNotificationDefaultSoundName;
local.applicationIconBadgeNumber = 4127;
local.category = @"MAIN_CATEGORY";
[[UIApplication sharedApplication] scheduleLocalNotification:local];
-(void)requestPermissionToNotify
UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc]init];
action.identifier = @"FLOAT_ACTION";
action.title = @"float";
action.activationMode = UIUserNotificationActivationModeBackground;
action.destructive = YES;
action.authenticationRequired = NO;
UIMutableUserNotificationAction *stingAction = [[UIMutableUserNotificationAction alloc]init];
stingAction.identifier = @"STING_ACTION";
stingAction.title = @"sting";
stingAction.activationMode = UIUserNotificationActivationModeForeground;
stingAction.destructive = NO;
stingAction.authenticationRequired = NO;
UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc]init];
category.identifier = @"MAIN_CATEGORY";
NSSet *categories = [NSSet setWithObjects:category, nil];
UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[category setActions:@[action,stingAction] forContext:UIUserNotificationActionContextDefault];
我的 appdelegate.m 有回调处理程序:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
application.applicationIconBadgeNumber = 0;
UILocalNotification *localnotif = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
if(localnotif)
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Received while launch" message:localnotif.alertBody preferredStyle:UIAlertViewStyleDefault];
UIAlertAction *aa = [UIAlertAction actionWithTitle:@"okay" style:UIAlertViewStyleDefault handler:nil];
[alert addAction:aa];
dispatch_async(dispatch_get_main_queue(), ^
[application.keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
);
return YES;
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
application.applicationIconBadgeNumber = 0;
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Received while running" message:notification.alertBody preferredStyle:UIAlertViewStyleDefault];
UIAlertAction *aa = [UIAlertAction actionWithTitle:@"okay" style:UIAlertViewStyleDefault handler:nil];
[alert addAction:aa];
dispatch_async(dispatch_get_main_queue(), ^
[application.keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
);
-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Received while action" message:identifier preferredStyle:UIAlertViewStyleDefault];
UIAlertAction *aa = [UIAlertAction actionWithTitle:@"okay" style:UIAlertViewStyleDefault handler:nil];
[alert addAction:aa];
dispatch_async(dispatch_get_main_queue(), ^
[application.keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
);
completionHandler();
【问题讨论】:
UILocalNotification is deprecated in iOS10的可能重复 谢谢你,我正在调查它:) 查看此链接:jamesrochabrunsite.wordpress.com/2016/11/09/… 以获得清晰的理解 【参考方案1】:在本地通知中添加操作可能会走错方向。您需要在请求许可时添加类别以及操作标识符。请检查以下代码以在通知中添加动作和处理动作点击
// Ask for permission to allow Notification
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0"))
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNNotificationAction *action = [UNNotificationAction actionWithIdentifier:@"ActionIdentifier" title:@"NewAction" options:UNNotificationActionOptionNone];
NSArray *notificationActions = @[action];
UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"Notification" actions:notificationActions intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
NSSet *categories = [NSSet setWithObject:category];
[center setNotificationCategories:categories];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error)
if(!granted)
];
else
UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
action.identifier = @"ActionIdentifier";
action.title = @"NewAction";
action.activationMode = UIUserNotificationActivationModeBackground;
action.destructive = NO;
action.authenticationRequired = NO;
UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];
category.identifier = @"ActionIdentifier";
[category setActions:@[action] forContext:UIUserNotificationActionContextDefault];
[category setActions:@[action] forContext:UIUserNotificationActionContextMinimal];
NSSet *categories = [NSSet setWithObjects:category, nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:categories]];
// Handle Notification for iOS 10
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
// Handle Notification here
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler
NSString *actionIdentifier = response.actionIdentifier;
BOOL newActionTapped = [actionIdentifier isEqualToString:@"NewAction"];
if(snooze)
// Handle NewAction action button here
else
// Handle Notification here
completionHandler();
// Handle Notification for iOS 9 or lower
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
// Handle Notification here
- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler
// This method will be called when user tap on Action
// Handle NewAction action button here
if (completionHandler)
completionHandler();
【讨论】:
以上是关于本地通知中未显示的操作的主要内容,如果未能解决你的问题,请参考以下文章