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

时间:2015-09-30 15:31:01

标签: 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)

希望有人能提供帮助。谢谢大家

3 个答案:

答案 0 :(得分:7)

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

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

在你的回叫didReceiveRemoteNotification中,检查应用程序状态:

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

答案 1 :(得分:2)

您只需要创建一个视图,使用您的内容对其进行自定义,并在应用程序窗口中显示它。

还有一些框架可以帮助您,例如CWStatusBarNotification

答案 2 :(得分:1)

您不需要使用其他框架或库。在iOS 10中,您可以使用方法userNotificationCenter(_:willPresent:withCompletionHandler:)

只有在向有效负载添加属性content-available:1时才会调用此方法。有效载荷应该是这样的:

{
     "aps":{
          "alert":"Testing.. (7)",
          "badge":1,"sound":"default"
     },
     "content-available":1
}