Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add action buttons/actions to UILocalNotification alert?

I have a local notification scheduled in my app, and right now I get a generic cancel (cross) button as I swipe the alert to the left.

I'm curious if I can add custom buttons/actions to it like on the image below?

enter image description here

like image 884
Eugene Gordin Avatar asked Dec 07 '15 22:12

Eugene Gordin


People also ask

How can I add buttons to local notification?

First, add awesme_notifications package in your project by adding the following lines in pubspec. yaml file. Now import the package in your script: import 'package:awesome_notifications/awesome_notifications.

How do you add an action button on notification flutter?

You may want to consider using awesome_notifications plugin in the meantime since it has support for notification buttons. You can then add the icon for the Action Button by adding the icon property on NotificationActionButton. The icon should be a String resource of the image mapped in the assets.

What are actionable notifications?

These notifications can be sent to either iOS or Android. Some useful examples of actionable notifications: A notification is sent whenever motion is detected in your home while you're away or asleep. A "Sound Alarm" action button is displayed alongside the notification, that when tapped, will sound your burglar alarm.

How many types of notification are there in Swift?

Apple provides three different types of notifications in iOS: NSNotificationCenter, UILocalNotification, and Remote Notifications.


1 Answers

I prepared for you some snipped code which shows notification with one button 10 second after ViewDidLoad method did shown.

  import UIKit

    class TestViewController: UIViewController {

        let category = UIMutableUserNotificationCategory()

        override func viewDidLoad() {
            super.viewDidLoad()

            let restartAction = UIMutableUserNotificationAction()
            restartAction.identifier = "xx"
            restartAction.destructive = false
            restartAction.title = "Restart"
            restartAction.activationMode = .Background
            restartAction.authenticationRequired = false

            let categoryIdentifier = "category.identifier"
            category.identifier = categoryIdentifier
            category.setActions([restartAction], forContext: .Minimal)
            category.setActions([restartAction], forContext: .Default)

            let categories = Set(arrayLiteral: category)
            let settings = UIUserNotificationSettings(forTypes: [.Alert, .Sound], categories: categories)
            UIApplication.sharedApplication().registerUserNotificationSettings(settings)


            let localNotif = UILocalNotification()
            localNotif.alertBody = "testBody"
            localNotif.category = categoryIdentifier

            // Notification will be shown after 10 second (IMPORTANT: if you want to see notification you have to close or put app into background)
            localNotif.fireDate = NSDate().dateByAddingTimeInterval(10)
            UIApplication.sharedApplication().scheduleLocalNotification(localNotif)
        }

    }

Note: you have to handle action in AppDelegate method:

func application(application: UIApplication, handleActionWithIdentifier identifier: String?,
                forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {

  completionHandler()
}

Of course my code is not as clean as it should be, but you have to know that I wrote it only for presentation purposes.

This code is written in Swift but convertion to Objective C should be very simple.

like image 66
Robert Avatar answered Oct 16 '22 23:10

Robert