当您的应用程序打开并在前台显示股票 iOS 通知横幅?

人气:496 发布:2022-10-16 标签: ios toast apple-push-notifications uikit uilocalnotification

问题描述

当 Apple 的官方 iOS 消息应用程序打开并处于前台时,来自其他联系人的新消息会触发原生 iOS 通知提醒横幅.见下图.

When Apple's official iOS Messages app is open and in the foreground, new messages from other contacts trigger a stock, native iOS notification alert banner. See image below.

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

Is this possible in 3rd party apps on the App Store? Local and/or Push Notifications for your app while your app is open and in the foreground?

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

When testing my app, notifications are received but no iOS alert UI is shown.

但这种行为在 Apple 的官方消息应用中可见:

But this behavior is seen in Apple's official Messages app:

本地和远程通知编程指南 说:

当操作系统下发本地通知或远程通知且目标应用未在前台运行时,它可以通过警报向用户呈现通知、图标徽章编号或声音.

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.

So yes, we can receive the notification data while in the foreground. But I see no way to present the native iOS notification alert 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 是否使用私有 API 在前台显示警报?

Is Messages then using a private API to display the alert while in the foreground?

出于这个问题的目的,请不要建议任何第 3 方敬酒".github 等上的弹出警报.我只对在您的应用程序打开时使用 stock、本机 iOS 本地或推送通知警报 UI 来完成此操作感兴趣并在前台.

For the purpose of this question, please do not suggest any 3rd party "toast" pop-up alerts on github or etc. I'm only interested if this can be done using the stock, native iOS Local or Push Notification alerts UI while your application is open and in the foreground.

推荐答案

iOS 10 添加了 UNUserNotificationCenterDelegate 协议,用于在您的应用处于前台时处理通知.

iOS 10 adds the UNUserNotificationCenterDelegate protocol for handling notifications while your app is in the foreground.

UNUserNotificationCenterDelegate 协议定义了接收通知和处理动作的方法.当您的应用处于前台时,到达的通知会传递到您的委托对象,而不是使用系统界面自动显示.

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.

斯威夫特:

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

目标-C:

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

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

The UNNotificationPresentationOptions flags allow you to specify UNNotificationPresentationOptionAlert to display an alert using the text provided by the notification.

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

This is key as it allows you to display the alert while your app is open and in the foreground, which is new for 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)
    }
    
}

953