gpt4 book ai didi

ios - 当您的应用程序打开并位于前台时显示一个 iOS 通知横幅?

转载 作者:IT王子 更新时间:2023-10-29 07:28:53 26 4
gpt4 key购买 nike

当 Apple 的官方 iOS Messages 应用程序打开并位于前台时,来自其他联系人的新消息会触发一个常用的原生 iOS 通知警报横幅。请参见下图。

这在 App Store 的第 3 方应用程序中是否可行?当您的应用程序打开并在前台时,您的应用程序的本地和/或推送通知?

在测试我的应用时,会收到通知,但没有显示 iOS 警报 UI

但是这种行为苹果的官方消息应用程序中可以看到:

Messages is open and in the foreground. Still shows a notification alert.

Local and Remote Notification Programming Guide说:

When the operating system delivers a local notification or remote notification and the target app is not running in the foreground, it can present the notification to the user through an alert, icon badge number, or sound.

If the app is running in the foreground when the notification is delivered, the app delegate receives a local or remote notification.

所以是的,我们可以在前台接收通知数据。但我看不出有什么办法可以呈现原生 iOS 通知提醒 UI

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
{
// I know we still receive the notification `userInfo` payload in the foreground.
// This question is about displaying the stock iOS notification alert UI.

// Yes, one *could* use a 3rd party toast alert framework.
[self use3rdPartyToastAlertFrameworkFromGithub]
}

Messages 是否使用私有(private) API 在前台显示警报?

出于这个问题的目的,请不要在 github 或其他平台上建议任何第 3 方“ toast ”弹出警报。我只对可以使用 stock, native iOS 本地或推送通知提醒 UI 当您的应用程序打开并在前台时

最佳答案

iOS 10 添加了 UNUserNotificationCenterDelegate当您的应用程序在前台时处理通知的协议(protocol)。

The UNUserNotificationCenterDelegate protocol defines methods for receiving notifications and for handling actions. When your app is in the foreground, arriving notifications are delivered to your delegate object instead of displayed automatically using the system interfaces.

swift :

optional func userNotificationCenter(_ center: UNUserNotificationCenter, 
willPresent notification: UNNotification,
withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void)

objective-C :

- (void)userNotificationCenter:(UNUserNotificationCenter *)center 
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler;

UNNotificationPresentationOptions标志允许您指定 UNNotificationPresentationOptionAlert 以使用通知提供的文本显示警报。

这是关键,因为它允许您在您的应用程序打开并在前台时显示警报,这是 iOS 10 的新功能。


示例代码:

class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

// Set UNUserNotificationCenterDelegate
UNUserNotificationCenter.current().delegate = self

return true
}

}

// Conform to UNUserNotificationCenterDelegate
extension AppDelegate: UNUserNotificationCenterDelegate {

func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler(.alert)
}

}

关于ios - 当您的应用程序打开并位于前台时显示一个 iOS 通知横幅?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30852870/

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