Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add app badge support #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions DLLocalNotifications/DLLocalNotifications.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,13 @@ public class DLNotificationScheduler {
return notification.identifier
}

public func scheduleNotification ( notification: DLNotification) {
public func scheduleNotification (notification: DLNotification) {

queueNotification(notification: notification)

}

public func scheduleAllNotifications () {
public func scheduleAllNotifications(appBadge: Int) {

let queue = DLQueue.queue.notificationsQueue()

Expand All @@ -194,15 +194,15 @@ public class DLNotificationScheduler {

if count < min(DLNotificationScheduler.maximumScheduledNotifications, MAX_ALLOWED_NOTIFICATIONS) {
let popped = DLQueue.queue.pop()
scheduleNotificationInternal(notification: popped)
scheduleNotificationInternal(notification: popped, appBadge: appBadge)
count += 1
} else { break }

}
}

// Refactored for backwards compatability
fileprivate func scheduleNotificationInternal ( notification: DLNotification) -> String? {
fileprivate func scheduleNotificationInternal(notification: DLNotification, appBadge: Int) -> String? {

if notification.scheduled {
return nil
Expand Down Expand Up @@ -253,6 +253,7 @@ public class DLNotificationScheduler {

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

content.badge = (appBadge + 1) as? NSNumber

if (notification.soundName == "1") { content.sound = nil}

Expand Down
13 changes: 11 additions & 2 deletions DLLocalNotifications/DLNotification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,14 @@ public class DLNotification {

// Internal variable needed when changint Notification types
var hasDataFromBefore = false

// Holds date components for a repeating notification
public var fromDateComponents: DateComponents?


//Specify the number 0 to remove the current badge, if present. Specify a number greater than 0 to display a badge with that number. Specify nil to leave the current badge unchanged.
public var appBadge: Int? = 0

enum CodingKeys: String, CodingKey {
case localNotificationRequest
case repeatInterval
Expand All @@ -74,6 +78,7 @@ public class DLNotification {
case region
case hasDataFromBefore
case fromDateComponents
case appBadge
}


Expand All @@ -88,7 +93,8 @@ public class DLNotification {
}
}

public init (identifier: String, alertTitle: String, alertBody: String, fromDateComponents: DateComponents, repeatInterval: RepeatingInterval ) {

public init (identifier: String, alertTitle: String, alertBody: String, fromDateComponents: DateComponents, repeatInterval: RepeatingInterval, appBadge: Int? = nil) {

self.alertBody = alertBody
self.alertTitle = alertTitle
Expand All @@ -100,6 +106,7 @@ public class DLNotification {
} else {
self.repeats = true
}
self.appBadge = appBadge

}

Expand Down Expand Up @@ -144,6 +151,7 @@ public class DLNotification {
self.launchImageName = try container.decodeIfPresent(String.self, forKey: .launchImageName)
self.category = try container.decodeIfPresent(String.self, forKey: .category)
self.hasDataFromBefore = try container.decode(Bool.self, forKey: .hasDataFromBefore)
self.appBadge = try container.decodeIfPresent(Int.self, forKey: .appBadge)
//self.fromDateComponents = try container.decode(DateComponents.self, forKey: .fromDateComponents)

}
Expand All @@ -163,6 +171,7 @@ public class DLNotification {
try container.encode(launchImageName, forKey: .launchImageName)
try container.encode(category, forKey: .category)
try container.encode(hasDataFromBefore, forKey: .hasDataFromBefore)
try container.encodeIfPresent(appBadge, forKey: .appBadge)
//try container.encode(fromDateComponents, forKey: .fromDateComponents)

}
Expand Down