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

标签 ios swift apple-push-notifications swift2

我实际上收到了带有 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/

相关文章:

python - Apple通过Python登录

ios - iOS 中的水平 UIScrollView 和多个缩略图?

ios - UINavigationBar 功能中的 UISwitch

ios - 更改p12文件上的密码

ios - 删除推送通知支持一段时间

ios - 使用 xcodebuild 运行的测试在终端中没有 NSLog 输出

ios - UITableView 未显示正确的行数

ios - ios应用程序即使终止也可以从服务器获取数据吗

iphone - 如何使用 XCode 4 中的 Leaks 识别泄漏对象?

ios - 使用建议栏快速获取键盘尺寸