Easily create Local Notifications in swift - Wrapper of UserNotifications Framework

Last update: Jun 21, 2022

Swift Version CocoaPods Compatible Platform Build Status License

In IOS 10, apple updated their library for Notifications and separated Local and push notifications to a new framework:

User Notifications

This library makes it easy to setup a local notification and also includes easy configuration for repeating notifications using [ .None, .Minute, .Hourly, .Daily, .Monthly, .Yearly] .

It also includes all the new features, including inserting attachments and changing the launch image of a notification.

  1. Features
  2. Requirements
  3. Installation
  4. Usage
  5. Contribute

Features

  • Easily Repeat Notifications
  • Location Based Notifications
  • Category Action buttons
  • Queue to enforce 64 notification limit

Requirements

  • iOS 10.0+
  • Xcode 8.0+

Installation

CocoaPods

You can use CocoaPods to install DLLocalNotifications by adding it to your Podfile:

platform :ios, '10.0'
use_frameworks!

target 'MyApp' do
	pod 'DLLocalNotifications'
end

Note: your iOS deployment target must be 10.0+

Usage

Single fire notification (any date)

Notification that fires once at the date time inputted

Note: If you want the notification to repeat then you need to create a notification based on date components

// The date you would like the notification to fire at
let triggerDate = Date().addingTimeInterval(300)

let firstNotification = DLNotification(identifier: "firstNotification", alertTitle: "Notification Alert", alertBody: "You have successfully created a notification", date: triggerDate)

let scheduler = DLNotificationScheduler()
scheduler.scheduleNotification(notification: firstNotification)
scheduler.scheduleAllNotifications()

Repeating Notification based on date components

The configuration of the repetition is chosen in the repeats parameter that can be [ .none, .minute, .hourly, .daily, .monthly, .yearly] .

// The date you would like the notification to fire at :35 mins every hour

var dateComponents = DateComponents()
dateComponents.minute = 35
dateComponents.second = 0

let firstNotification = DLNotification(identifier: "hourlyNotification", alertTitle: "Notification Alert", alertBody: "You have successfully created a notification", fromDateComponents: dateComponents, repeatInterval: .hourly)

let scheduler = DLNotificationScheduler()
scheduler.scheduleNotification(notification: firstNotification)
scheduler.scheduleAllNotifications()

Notification that repeats from one Date to another with a time interval period

This is useful to setup notifications to repeat every specific time interval for in a specific time period of the day.

let scheduler = DLNotificationScheduler()

// This notification repeats every 15 seconds from a time period starting from 15 seconds from the current time till 5 minutes from the current time

scheduler.repeatsFromToDate(identifier: "First Notification", alertTitle: "Multiple Notifications", alertBody: "Progress", fromDate: Date().addingTimeInterval(15), toDate: Date().addingTimeInterval(300) , interval: 15, repeats: .none )
scheduler.scheduleAllNotifications()

Note: Since this library takes care of the 64 notification limit you would want to call scheduler.scheduleAllNotifications() in your AppDelegate file as well.

Modifying elements of the notification

You can modify elements of the notification before scheduling. Publically accessible variables include:

repeatInterval, alertBody, alertTitle, soundName, fireDate, attachments, launchImageName, category

let firstNotification = DLNotification(identifier: "firstNotification", alertTitle: "Notification Alert", alertBody: "You have successfully created a notification", date: Date(), repeats: .minute)

// You can now change the repeat interval here
firstNotification.repeatInterval = .yearly

// You can add a launch image name
firstNotification.launchImageName = "Hello.png"

let scheduler = DLNotificationScheduler()
scheduler.scheduleNotification(notification: firstNotification)
scheduler.scheduleAllNotifications()

Location Based Notification

The notification is triggered when a user enters a geo-fenced area.

let center = CLLocationCoordinate2D(latitude: 37.335400, longitude: -122.009201)
let region = CLCircularRegion(center: center, radius: 2000.0, identifier: "Headquarters")
region.notifyOnEntry = true
region.notifyOnExit = false

let locationNotification = DLNotification(identifier: "LocationNotification", alertTitle: "Notification Alert", alertBody: "You have reached work", region: region )

let scheduler = DLNotificationScheduler()
scheduler.scheduleNotification(notification: locationNotification)
scheduler.scheduleAllNotifications()

Adding action buttons to a notification

 let scheduler = DLNotificationScheduler()

 let standingCategory = DLCategory(categoryIdentifier: "standingReminder")

 standingCategory.addActionButton(identifier: "willStand", title: "Ok, got it")
 standingCategory.addActionButton(identifier: "willNotStand", title: "Cannot")

 scheduler.scheduleCategories(categories: [standingCategory])

Don't forget to the set the notification category before scheduling the notification using

notification.category = "standingReminder"

Cancelling a notification

 scheduler.cancelNotification(notification: notification)

Contribute

We would love for you to contribute to DLLocalNotifications, check the LICENSE file for more info.

Meta

Devesh Laungani – @d7laungani

Distributed under the MIT license. See LICENSE for more information

https://github.com/d7laungani/

Hire Me

GitHub

https://github.com/d7laungani/DLLocalNotifications
Comments
  • 1. Not showing Action Button

    Im trying to show action buttons with the notification

    ` func dailyNotification(){

        let firstNotification = DLNotification(identifier: "firstNotification", alertTitle: "Notificaiton Alert", alertBody: "You have successfully created a notification", date: Date(), repeats: .Minute)
        
        // You can now change the repeat interval here
        firstNotification.repeatInterval = .Minute
        
        // You can add a launch image name
        firstNotification.launchImageName = "logo_ad.jpg"
        
        let scheduler = DLNotificationScheduler()
       
        let standingCategory = DLCategory(categoryIdentifier: "standingReminder")
        
        standingCategory.addActionButton(identifier: "willStand", title: "Ok, got it")
        standingCategory.addActionButton(identifier: "willNotStand", title: "Cannot")
        
        scheduler.scheduleCategories(categories: [standingCategory])
        scheduler.scheduleNotification(notification: firstNotification)
        firstNotification.category = "standingReminder"
    }`
    

    any reason why this isnt working

    thank you

    Reviewed by n4zg at 2017-03-17 21:36
  • 2. Crash when set Date to nil

    Crash at:

    https://github.com/d7laungani/DLLocalNotifications/blob/e4e483e13b16af0fb5f65618f6347f0d3aa0bee5/DLLocalNotifications/DLLocalNotifications.swift#L117

    by following code

    let notification = DLNotification(identifier: "", alertTitle: "", alertBody: "", date: nil, repeats: .none, soundName: "")
    let scheduler = DLNotificationScheduler()
    scheduler.scheduleNotification(notification: notification)
    scheduler.scheduleAllNotifications()
    
    Reviewed by Haraguroicha at 2019-03-09 19:03
  • 3. podfile install issue

    I have an issue installing this from the master branch.

    When i run 'pod install' on the app with the line pod 'DLLocalNotifications', '~>0.06' in my podfile, I get the following line of code

    content.sound = (notification.soundName == nil) ? UNNotificationSound.default() : UNNotificationSound.init(named: notification.soundName!)

    However, I noticed that this is replaced by

    content.sound = notification.soundName == "" ? UNNotificationSound.default : UNNotificationSound.init(named: UNNotificationSoundName(rawValue: notification.soundName))

    in the actual code. Why would the pod install of mine have the wrong line of code in it?

    Reviewed by raguiar2 at 2018-12-27 17:43
  • 4. How to schedule a notification that will repeat with some frequency after specific time passes

    I don't see support for scheduling such kind of notification i.e. that repeats only when a specific time passes. The problem is that the schedule fires immediately without waiting for the exact time although repeats correctly.

    e.g. here is the code for scheduling a repeating notification after 10 minutes. This doesn't wait for the 10 minutes - fires immediately in the next minute from now.

        let triggerDate = Date().addingTimeInterval(600)
        let firstNotification = DLNotification(identifier: "firstNotification", alertTitle: "Notification Alert",                        alertBody: "You have successfully created a notification", date: triggerDate, repeats: .Minute)
        
        let scheduler = DLNotificationScheduler()
        scheduler.scheduleNotification(notification: firstNotification)
    

    here is another block of code for a repeating notification that will fire daily after 3 days.

        let triggerDate = Date().addingTimeInterval(3*24*60*60)
        let firstNotification = DLNotification(identifier: "firstNotification", alertTitle: "Notification Alert", alertBody: "You have successfully created a notification", date: triggerDate, repeats: .Daily)
        
        let scheduler = DLNotificationScheduler()
        scheduler.scheduleNotification(notification: firstNotification)`
    

    This also doesn't wait for the 3 days but fires immediately on the next day.

    The main problem in both is that it ignores the time till which it should wait to start firing the notification. If anybody has understood the problem, will you take some time to figure out what's wrong. I will be really thankful.

    Reviewed by mibrahim025 at 2018-08-21 05:28
  • 5. firstNotification.launchImageName not working

    Hi,

    I would like to add an image on the localnotification but i've got this error: 'launchImageName' is inaccessible due to 'internal' protection level

    Can you help me to fix it please ?

    Thanks in advance

    Reviewed by fabien79 at 2019-10-24 20:23
  • 6. Help me! Support required

    I have 3 local notifications need to be repeated daily from (9am to 6pm with frequency of 3 hrs), I used repeatsFromToDate() but it didn't work.

    Note:- There are 3 LocalNotification with the different startTime& endTime & user can stop/start any of these at anytime.

    I need to repeat it 3-4 times a day till the user turns off the notification (ie., 10AM, 1PM, 4PM, 7PM today, tomorrow... ) - this is for notification1, similarly the app has 2 similar notifications (Notification2 might trigger at 9AM, 12PM, 3PM, 6PM today, tomorrow... Notification3- 4PM, 10PM today, tomorrow...). How to repeat them daily between these time intervals? Again user can't change the frequency, he could change the start & end time for each notifications or turn off.

    Reviewed by nagarajavrao at 2019-05-15 07:04
  • 7. made identifier public

    I need this for the functionality in my app- I uniquely generate the identifiers and can get groups of notifications by a specific prefix.

    Fixes issue https://github.com/d7laungani/DLLocalNotifications/issues/19

    Reviewed by raguiar2 at 2018-12-19 04:10
  • 8. Added a notificationsQueue function to get all queued notificaitons

    I noticed that it was difficult to reschedule notifications after they had been queued. This change will allow you to retrieve all notifications from the queue, and then

    Reviewed by raguiar2 at 2018-12-10 13:17
  • 9. unexpectedly found nil while unwrapping an Optional value

    let scheduler = DLNotificationScheduler() let standingCategory = DLCategory(categoryIdentifier: "standingReminder") standingCategory.addActionButton(identifier: "willStand", title: "Ok, got it") standingCategory.addActionButton(identifier: "willNotStand", title: "Cannot") scheduler.scheduleCategories(categories: [standingCategory])

    getting the following error fatal error: unexpectedly found nil while unwrapping an Optional value 2017-03-12 14:24:09.727084 fatal error: unexpectedly found nil while unwrapping an Optional value

    any suggestions please

    Reviewed by n4zg at 2017-03-12 14:25
  • 10. Fix bugs

    This PR fixes multiple issues and refactors the API to better reflect the limitations of the User Notifications Framework

    Notes -- You can only repeat notifications via date components -- Delayed repeated notifications are not supported in the underlying User Notifications Framework

    Reviewed by d7laungani at 2021-01-20 14:20
  • 11. Support required

    After scheduling multiple notifications using the method scheduler.repeatsFromToDate (repeats between start & end time) , i cant stop a particular notification.

    Requirement:-1. Start multiple notifications(one at a time) with the changeable start time, end time & range. 2. stop a particular notification.

    Could you help

    Reviewed by nagarajavrao at 2019-04-25 12:39
  • 12. Feature Ask: Custom Repeats

    I have a question about the notification scheduling capabilities of this library. I'm trying to build out a calendar-type application that needs to schedule many notifications with custom repetition. I have a couple questions about this library.

    1: Does this library still queue notifications after the app has been killed? e.g. do they persist if I queue the notification then kill the app and restart it?

    2: Is there an easy way to do custom repeats? For example, if i want something to trigger on monday, tuesday, wednesday each week, is there an easy way to do that?

    Thank you!

    Reviewed by raguiar2 at 2018-12-09 00:13
See all your scheduled local notifications in one place
See all your scheduled local notifications in one place

ScheduledNotificationsViewController Nice Photon is available for hire! Talk to

Jun 19, 2022
Facebook Analytics In-App Notifications Framework
Facebook Analytics In-App Notifications Framework

Facebook In-App Notifications enables you to create rich and customizable in-app notifications and deliver them via push notifications, based on the a

Mar 6, 2022
Push notifications allow developers to reach users, even when users aren't actively using an app!
Push notifications allow developers to reach users, even when users aren't actively using an app!

Push notifications allow developers to reach users, even when users aren't actively using an app! With the latest update of iOS Apple provide very useful extensions which are user-friendly. In this tutorial, I am going to share the configuration, set up of Notification with the media attachments like.

Mar 3, 2022
Library to send mock remote notifications to the iOS simulator
Library to send mock remote notifications to the iOS simulator

SimulatorRemoteNotifications SimulatorRemoteNotifications is a library to send mock remote notifications to the iOS simulator. The library extends UIA

May 27, 2022
Pushkin is a free open source tool for sending push notifications
Pushkin is a free open source tool for sending push notifications

Unmaintained This repository is no longer maintained. Pushkin Introduction Pushkin is a free open source tool for sending push notifications. It was d

Mar 13, 2022
Custom in-app notifications.
Custom in-app notifications.

CRNotifications CRNotifications are custom in-app notifications with 3 types of layouts. The notifications will animate in and out. They will hide whe

Jan 9, 2022
How to Add Local Push Notification With timeInterval & Test On Simulator

Technicalisto How to Add Local Push Notification With timeInterval & Test On Simulator In Project Target ( Signing & Capapilities ) Add Capapility : 1

Jun 24, 2022
ToDoAppCoreData - MVVM + Core Data + Local Notification
ToDoAppCoreData - MVVM + Core Data + Local Notification

ToDo App Features MVVM + Core Data + Local Notification Screen recordings Creadi

Apr 22, 2022
A fully customizable library to easily display Animated Toast Messages in iOS using Swift!
A fully customizable library to easily display Animated Toast Messages in iOS using Swift!

CustomToastView-swift A fully customizable library to easily display Animated Toast Messages in iOS using Swift! Preview - All the custom toasts you c

Jun 15, 2022
OS X and iOS application and framework to play with the Apple Push Notification service (APNs)
OS X and iOS application and framework to play with the Apple Push Notification service (APNs)

Pusher OS X and iOS application and framework to play with the Apple Push Notification service (APNs) Installation Install the Mac app using Homebrew

Jun 13, 2022
🚎 Simple type-safe event bus implementation in swift

?? RealEventsBus RealEventsBus is a small swift experiment package to implement a basic type-safe event bus mechanism. Some other implementations in G

Apr 26, 2022
Jan 7, 2022
A simple framework to output to a file, url, the console, or even register notification using UserNotifications

o is a simple framework to output to a file, url, the console, or even register notification using UserNotifications. o can also get input from a file, url, or console.

Mar 18, 2022
See all your scheduled local notifications in one place
See all your scheduled local notifications in one place

ScheduledNotificationsViewController Nice Photon is available for hire! Talk to

Jun 19, 2022
A framework for easily testing Push Notifications and Routing in XCUITests
A framework for easily testing Push Notifications and Routing in XCUITests

Mussel ?? ?? A framework for easily testing Push Notifications, Universal Links and Routing in XCUITests. As of Xcode 11.4, users are able to test Pus

Jun 27, 2022
CoffeeToast - A swift package to easily add Toast notifications to your app

CoffeeToast A simple Swift package to add Toast Notifications to your app. Insta

Feb 3, 2022
AutoMocker is a Swift framework that leverages the type system to let you easily create mocked instances of your data types.

AutoMocker Context AutoMocker is a Swift framework that leverages the type system to let you easily create mocked instances of your data types. Here's

May 19, 2022
An iOS framework to easily create simple animated walkthrough, written in Swift.
An iOS framework to easily create simple animated walkthrough, written in Swift.

Intro Overview An iOS framework to easily create simple animated walkthrough, written in Swift. Requirements iOS8 Installation with CocoaPods Intro is

Oct 1, 2021
Facebook Analytics In-App Notifications Framework
Facebook Analytics In-App Notifications Framework

Facebook In-App Notifications enables you to create rich and customizable in-app notifications and deliver them via push notifications, based on the a

Mar 6, 2022
Full configurable spreadsheet view user interfaces for iOS applications. With this framework, you can easily create complex layouts like schedule, gantt chart or timetable as if you are using Excel.
Full configurable spreadsheet view user interfaces for iOS applications. With this framework, you can easily create complex layouts like schedule, gantt chart or timetable as if you are using Excel.

kishikawakatsumi/SpreadsheetView has moved! It is being actively maintained at bannzai/SpreadsheetView. This fork was created when the project was mov

May 27, 2022