编辑:所以把应用程序放在后台就行了.
原版的:
所以我一直在尝试向新的UNUserNotificationCenter添加通知,但我似乎没有得到它.
我的视图控制器有一个动作:
@IBAction func sendPressed(_ sender: AnyObject) {
let content = UNMutableNotificationContent()
content.title = "Hello"
content.body = "What up?"
content.sound = UNNotificationSound.default()
// Deliver the notification in five seconds.
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: false)
let request = UNNotificationRequest.init(identifier: "FiveSecond", content: content, trigger: trigger)
// Schedule the notification.
let center = UNUserNotificationCenter.current()
center.add(request) { (error) in
print(error)
}
print("should have been added")
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let center = UNUserNotificationCenter.current()
center.requestAuthorization([.alert, .sound]) { …Run Code Online (Sandbox Code Playgroud) uilocalnotification swift swift3 ios10 unusernotificationcenter
我想使用iOS 10安排本地通知.我想知道如何做到这一点.我已经浏览了网络,但我一直在寻找线索,只是为了注册和处理通知.不适用于本地通知的安排.
那么,有谁知道怎么做?
我正在设置多个UNUsernotnotifications,如下所示,
- (void)viewDidLoad {
[super viewDidLoad];
notifCount = 0;
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(@"request succeeded!");
[self set10Notif];
}
}];
}
Run Code Online (Sandbox Code Playgroud)
在该set10Notif方法中,我设置多个(8个用于测试)通知,时间与当前时间相差10秒.
-(void) set10Notif
{
notifCount = notifCount+1;
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0") && notifCount < 10)
{
// create actions
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
NSDate *fireD = [[NSDate date] dateByAddingTimeInterval:notifCount*10];
NSString *fireStr = [self returnStringFromDate:fireD withFormat:@"hh/mm/ss dd/MM/yyyy"];
NSDateComponents *components …Run Code Online (Sandbox Code Playgroud) objective-c ios ios10 usernotifications unusernotificationcenter