Giter Site home page Giter Site logo

d7laungani / dllocalnotifications Goto Github PK

View Code? Open in Web Editor NEW
213.0 7.0 37.0 296 KB

:speech_balloon: Easily create Local Notifications in swift - Wrapper of UserNotifications Framework

License: MIT License

Ruby 1.75% Objective-C 1.19% Swift 97.06%
local-notifications notifications scheduled-notifications user-notifications-framework local-notification-helper

dllocalnotifications's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.