Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 824
Norolim Avatar asked Dec 24 '22 13:12

Norolim


1 Answers

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
}
like image 184
Duyen-Hoa Avatar answered Dec 31 '22 12:12

Duyen-Hoa