-
Notifications
You must be signed in to change notification settings - Fork 3
Description
I corrected the existing errors to make it compile, but even after that, it simple never sends the notification. Doesn't seem to work in iOS 10 on the simulator and on iPhone 5s with 10 installed.
Do you know how to fix it?
---------------------------- CODE BELOW
import UserNotifications
enum NotificationActions: String {
case HighFive = "highfiveidentifier"
}
@objc(Notifications) class Notifications : CDVPlugin, UNUserNotificationCenterDelegate {
override func pluginInitialize() {
}
func registerForNotifications(_ success: ((_ ret: String) -> Void)!) {
DispatchQueue.main.async {
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
if granted {
print("*************** Yay! ****************")
//if(success) {
print("*************** Executing Success! ****************")
success("Success")
//}
} else {
print("D'oh")
}
}
} else {
// Fallback on earlier versions
}
}
}
func sendNotification(_ ret: String) {
print("~~~~~~~~~~~ sendNotification ~~~~~~~~~~~");
if #available(iOS 10.0, *) {
print("~~~~~~~~~~~ Doing the deed like is iOS 10.0 ~~~~~~~~~~~");
/*
let center = UNUserNotificationCenter.current()
center.removeAllPendingNotificationRequests()
center.delegate = self
let content = UNMutableNotificationContent()
content.title = "Late wake up call"
content.body = "The early bird catches the worm, but the second mouse gets the cheese."
content.categoryIdentifier = "alarm"
content.userInfo = ["customData": "fizzbuzz"]
content.sound = UNNotificationSound.default()
var dateComponents = DateComponents()
dateComponents.hour = 14
dateComponents.minute = 45
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
center.add(request) { (error) in
if (error != nil){
print("OOOOOOOOOOOOOOOOOOOOOPS")
} else {
print("SCHEDULED >=iOS10:")
}
}
*/
// 1
let highFiveAction = UNNotificationAction(identifier: NotificationActions.HighFive.rawValue, title: "High Five", options: [])
let category = UNNotificationCategory(identifier: "wassup", actions: [highFiveAction], intentIdentifiers: [], options: [UNNotificationCategoryOptions.customDismissAction])
UNUserNotificationCenter.current().setNotificationCategories([category])
// 2
let highFiveContent = UNMutableNotificationContent()
highFiveContent.title = "Wassup?"
highFiveContent.body = "Can I get a high five?"
// 3
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
// 4
let highFiveRequestIdentifier = "sampleRequest"
let highFiveRequest = UNNotificationRequest(identifier: highFiveRequestIdentifier, content: highFiveContent, trigger: trigger)
UNUserNotificationCenter.current().add(highFiveRequest) { (error) in
// handle the error if needed
if (error != nil){
print("OOOOOOOOOOOOOOOOOOOOOPS")
} else {
print("SCHEDULED >=iOS10:")
print("SUCCESS")
}
}
} else {
// Fallback on earlier versions
}
print("~~~~~~~~~~~ END sendNotification ~~~~~~~~~~~");
}
// App in foreground
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
print("~~~~~~~~~~~~~~~~~~~ YAY - in foreground!!!")
}
//On Action click
@available(iOS 10.0, *)
private func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: () -> Void) {
print("~~~~~~~~~~~~~~~~~~~ got Action?")
}
func schedule(_ command: CDVInvokedUrlCommand) {
var pluginResult = CDVPluginResult(
status: CDVCommandStatus_ERROR
)
registerForNotifications() {
(ret: String) in
self.sendNotification(ret)
}
let msg = command.arguments[0] as? String ?? ""
if msg.characters.count > 0 {
pluginResult = CDVPluginResult(
status: CDVCommandStatus_OK,
messageAs: msg
)
}
self.commandDelegate!.send(
pluginResult,
callbackId: command.callbackId
)
}
}