WatchOS swift 4.0 local notification
var content = UNMutableNotificationContent() content.title = "ALERT !" content.body = msg content.sound = UNNotificationSound.default() as? UNNotificationSound // Time var trigger: UNTimeIntervalNotificationTrigger? trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false) // Actions var snoozeAction = UNNotificationAction(identifier: "Track", title: "Track", options: .foreground) var category = UNNotificationCategory(identifier: "UYLReminderCategory", actions: [snoozeAction], intentIdentifiers: [] as? [String] ?? [String](), options: .customDismissAction) var categories = Set<AnyHashable>([category]) center.setNotificationCategories(categories as? Set<UNNotificationCategory> ?? Set<UNNotificationCategory>()) content.categoryIdentifier = "UYLReminderCategory" var identifier: String = stringUUID() var request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger) center.add(request, withCompletionHandler: {(_ error: Error?) -> Void in if error != nil { print("Something went wrong: \(error)") } })
Unique Request ID Methods
func stringUUID() -> String { let uuid = UUID() let str: String = uuid.uuidString return str }
Goal c
// Objective-C UNMutableNotificationContent *content = [UNMutableNotificationContent new]; content.title = @"ALERT !"; content.body = msg; content.sound = [UNNotificationSound defaultSound]; // Time UNTimeIntervalNotificationTrigger *trigger; trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:1 repeats:NO]; // Actions UNNotificationAction *snoozeAction = [UNNotificationAction actionWithIdentifier:@"Track" title:@"Track" options:UNNotificationActionOptionForeground]; // Objective-C UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"UYLReminderCategory" actions:@[snoozeAction] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction]; NSSet *categories = [NSSet setWithObject:category]; // Objective-C [center setNotificationCategories:categories]; // Objective-C content.categoryIdentifier = @"UYLReminderCategory"; NSString *identifier = [self stringUUID]; UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger]; [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { if (error != nil) { NSLog(@"Something went wrong: %@",error); } }];
Unique Request ID Methods
-(NSString *)stringUUID { NSUUID *uuid = [NSUUID UUID]; NSString *str = [uuid UUIDString]; return str; }
source share