无法在前台显示 UIUserNotificationActions(默认上下文)
Cannot display UIUserNotificationActions in foreground (default context)
我正在尝试获取一组用户通知操作以显示 2 个以上的按钮。当应用程序在后台时,通知本身会使用最小上下文,这工作正常。
当在前台调用通知时,application:didReceiveLocalNotification:根据 UILocalNotification class 上的文档调用。
如何显示带有两个以上按钮(由操作类别的默认上下文提供)的警报?
下面的代码遵循使用 2 个以上按钮的文档,但仅调用 application:didReceiveLocalNotification:当应用程序位于前台时:
// Selecting YES activates the app in the foreground
UIMutableUserNotificationAction *actionYes = [ UIMutableUserNotificationAction new ];
actionYes.identifier = @"actionYes";
actionYes.title = @"Yes";
actionYes.activationMode = UIUserNotificationActivationModeForeground;
actionYes.destructive = NO;
actionYes.authenticationRequired = NO;
// Selecting NO activates the app in the background
UIMutableUserNotificationAction *actionNo = [ UIMutableUserNotificationAction new ];
actionNo.identifier = @"actionNo";
actionNo.title = @"No";
actionNo.activationMode = UIUserNotificationActivationModeBackground;
actionNo.destructive = NO;
actionNo.authenticationRequired = NO;
// Selecting NEVER activates the app in the background
UIMutableUserNotificationAction *actionNever = [ UIMutableUserNotificationAction new ];
actionNever.identifier = @"actionNever";
actionNever.title = @"Never";
actionNever.activationMode = UIUserNotificationActivationModeBackground;
actionNever.destructive = YES;
actionNever.authenticationRequired = NO;
UIMutableUserNotificationCategory *actionCategory = [ UIMutableUserNotificationCategory new ];
actionCategory.identifier = @"actionCategory";
[ actionCategory setActions: @[ actionYes, actionNo, actionNever ] forContext: UIUserNotificationActionContextDefault ];
[ actionCategory setActions: @[ actionNo, actionYes ] forContext: UIUserNotificationActionContextMinimal ];
NSSet *categories = [ NSSet setWithObjects: actionCategory, nil] ;
UIUserNotificationType notificationType = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *notificationSettings = [ UIUserNotificationSettings settingsForTypes: notificationType categories: categories ];
// The categories have to be registered for notification settings to the app
[ [ UIApplication sharedApplication ] registerUserNotificationSettings: notificationSettings ];
UILocalNotification *localNotification = [ UILocalNotification new ];
localNotification.fireDate = [ NSDate dateWithTimeIntervalSinceNow: 5.0 ];
localNotification.alertBody = @"Testing user notifications message";
localNotification.category = @"actionCategory"; // Same as category identifier
[ [ UIApplication sharedApplication ] scheduleLocalNotification: localNotification ];
对于超过 2 个按钮,用户必须 select "Alerts" 而不是应用程序设置菜单中的 "Banners"。警报将有一个选项按钮,该按钮在打开和关闭之间的列表中显示按钮。
无法以编程方式设置 Alert/Banner 选项。
如果应用程序在前台,所有通知将直接发送给应用程序委托application:didReceiveLocalNotification:无一例外;这意味着应用程序在前台时无法执行操作。
我希望这对阅读 Apple 文档的其他人有所帮助。
我正在尝试获取一组用户通知操作以显示 2 个以上的按钮。当应用程序在后台时,通知本身会使用最小上下文,这工作正常。
当在前台调用通知时,application:didReceiveLocalNotification:根据 UILocalNotification class 上的文档调用。
如何显示带有两个以上按钮(由操作类别的默认上下文提供)的警报?
下面的代码遵循使用 2 个以上按钮的文档,但仅调用 application:didReceiveLocalNotification:当应用程序位于前台时:
// Selecting YES activates the app in the foreground
UIMutableUserNotificationAction *actionYes = [ UIMutableUserNotificationAction new ];
actionYes.identifier = @"actionYes";
actionYes.title = @"Yes";
actionYes.activationMode = UIUserNotificationActivationModeForeground;
actionYes.destructive = NO;
actionYes.authenticationRequired = NO;
// Selecting NO activates the app in the background
UIMutableUserNotificationAction *actionNo = [ UIMutableUserNotificationAction new ];
actionNo.identifier = @"actionNo";
actionNo.title = @"No";
actionNo.activationMode = UIUserNotificationActivationModeBackground;
actionNo.destructive = NO;
actionNo.authenticationRequired = NO;
// Selecting NEVER activates the app in the background
UIMutableUserNotificationAction *actionNever = [ UIMutableUserNotificationAction new ];
actionNever.identifier = @"actionNever";
actionNever.title = @"Never";
actionNever.activationMode = UIUserNotificationActivationModeBackground;
actionNever.destructive = YES;
actionNever.authenticationRequired = NO;
UIMutableUserNotificationCategory *actionCategory = [ UIMutableUserNotificationCategory new ];
actionCategory.identifier = @"actionCategory";
[ actionCategory setActions: @[ actionYes, actionNo, actionNever ] forContext: UIUserNotificationActionContextDefault ];
[ actionCategory setActions: @[ actionNo, actionYes ] forContext: UIUserNotificationActionContextMinimal ];
NSSet *categories = [ NSSet setWithObjects: actionCategory, nil] ;
UIUserNotificationType notificationType = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *notificationSettings = [ UIUserNotificationSettings settingsForTypes: notificationType categories: categories ];
// The categories have to be registered for notification settings to the app
[ [ UIApplication sharedApplication ] registerUserNotificationSettings: notificationSettings ];
UILocalNotification *localNotification = [ UILocalNotification new ];
localNotification.fireDate = [ NSDate dateWithTimeIntervalSinceNow: 5.0 ];
localNotification.alertBody = @"Testing user notifications message";
localNotification.category = @"actionCategory"; // Same as category identifier
[ [ UIApplication sharedApplication ] scheduleLocalNotification: localNotification ];
对于超过 2 个按钮,用户必须 select "Alerts" 而不是应用程序设置菜单中的 "Banners"。警报将有一个选项按钮,该按钮在打开和关闭之间的列表中显示按钮。
无法以编程方式设置 Alert/Banner 选项。
如果应用程序在前台,所有通知将直接发送给应用程序委托application:didReceiveLocalNotification:无一例外;这意味着应用程序在前台时无法执行操作。
我希望这对阅读 Apple 文档的其他人有所帮助。