gpt4 book ai didi

应用程序打开时的 iOS 应用程序通知(如 whatsapp)

转载 作者:搜寻专家 更新时间:2023-10-31 08:15:37 25 4
gpt4 key购买 nike

我实际上收到了带有 JSON 和消息的通知,所以我想要的是处理该通知并将其显示给用户。

当应用程序未运行或处于后台时,会显示消息,但当应用程序打开时,不会有任何通知。事实上,我在 didReceiveRemoteNotification 中收到 json,但我想要的是像 whatsapp 那样的通知框。

像这样:

enter image description here

我有这个:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
print("Notification received: \(userInfo)")
let notification = userInfo["aps"] as? NSDictionary
let message = notification?.valueForKey("alert")
}

这在“didfinishlaunchWithOptions”中

let readAction = UIMutableUserNotificationAction()
readAction.identifier = "READ_IDENTIFIER"
readAction.title = "Read"
readAction.activationMode = UIUserNotificationActivationMode.Foreground
readAction.destructive = false
readAction.authenticationRequired = true

let deleteAction = UIMutableUserNotificationAction()
deleteAction.identifier = "DELETE_IDENTIFIER"
deleteAction.title = "Delete"
deleteAction.activationMode = UIUserNotificationActivationMode.Foreground
deleteAction.destructive = true
deleteAction.authenticationRequired = true

let ignoreAction = UIMutableUserNotificationAction()
ignoreAction.identifier = "IGNORE_IDENTIFIER"
ignoreAction.title = "Ignore"
ignoreAction.activationMode = UIUserNotificationActivationMode.Foreground
ignoreAction.destructive = false
ignoreAction.authenticationRequired = false

let messageCategory = UIMutableUserNotificationCategory()
messageCategory.identifier = "MESSAGE_CATEGORY"
messageCategory.setActions([readAction, deleteAction], forContext: UIUserNotificationActionContext.Minimal)
messageCategory.setActions([readAction, deleteAction, ignoreAction], forContext: UIUserNotificationActionContext.Default)

let types: UIUserNotificationType = [UIUserNotificationType.Badge, UIUserNotificationType.Sound, UIUserNotificationType.Alert]

application.registerUserNotificationSettings(
UIUserNotificationSettings(
forTypes: types,
categories: (NSSet(array: [messageCategory])) as? Set<UIUserNotificationCategory>))

UIApplication.sharedApplication().registerForRemoteNotifications()

let notTypes:UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]
let noteSettings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: notTypes, categories: nil)

UIApplication.sharedApplication().registerUserNotificationSettings(noteSettings)

希望大家帮帮忙。感谢大家

最佳答案

当您的应用程序在前台运行时,iOS 将不会显示通知横幅。你必须展示你自己。您可以使用其中一些第三代码来显示横幅并处理横幅上的触摸以处理适当的代码:

https://github.com/KrauseFx/TSMessages https://github.com/terryworona/TWMessageBarManager

在您的回调 didReceiveRemoteNotification 中,检查应用程序状态:

if ( application.applicationState == UIApplicationStateActive ) {
// show your banner
}

关于应用程序打开时的 iOS 应用程序通知(如 whatsapp),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32870113/

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