Norolim
Norolim

Reputation: 956

iOS app notifications when app is open (like whatsapp)

Im actually receiving the notifications with a JSON and a message, so what i wanted was handle that notification and show it to the user.

When the app is not running or in background, the message is shown, but when the app is open there is no notification. In fact, i receive the json when in didReceiveRemoteNotification, but what i want is a notification box like whatsapp do.

Like this:

enter image description here

I have this:

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

And this in ´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)

Hope anyone could help. Thanks for all

Upvotes: 4

Views: 17771

Answers (3)

Jorge Costa
Jorge Costa

Reputation: 317

You don't need to use additional frameworks or libraries. In iOS 10, you can use the method userNotificationCenter(_:willPresent:withCompletionHandler:).

This method is only called if you add to payload the attribute content-available:1. The payload should be something like:

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

Upvotes: 2

Duyen-Hoa
Duyen-Hoa

Reputation: 15784

When your application is on foreground, iOS will not show the notification banner. You have to show yourself. You can use some of those 3rd codes to show the banner and handle the touche on the banner to process the appropriated codes:

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

In your call-back didReceiveRemoteNotification, check the application state:

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

Upvotes: 7

EmilioPelaez
EmilioPelaez

Reputation: 19912

You just have to create a view, customize it with your content and show it on the application window.

There are also frameworks that do that for you, like CWStatusBarNotification

Upvotes: 2

Related Questions