在ios10中添加本地通知 - swift 3
编辑: 因此,将应用程序置于后台的方法确实有效。在ios10中添加本地通知 - swift 3
原文:
所以我一直在努力的通知添加到新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]) { (granted, error) in
}
}
而且我在项目Notification Content Extension
为好,但它似乎并没有在所有触发,任何想法是什么我失踪?我正在尝试用户文档中的示例,但它没有告诉我更多,或者我错过了它。
这里:https://developer.apple.com/reference/usernotifications/unmutablenotificationcontent
另外: https://developer.apple.com/reference/usernotificationsui https://developer.apple.com/reference/usernotifications
您需要为通知注册...我想和这个作品。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let center = UNUserNotificationCenter.current()
center.requestAuthorization([.alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
return true
}
编辑:你不需要把你的应用程序在后台从iOS的10起PRESENT通知。
使用下面的回调来配置前台通知。
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
Here是一个示例项目。
用Objective-C的implemation:
我在这里写了一个演示项目:iOS10AdaptationTips。
-
进口UserNotifications
///Notification become independent from Foundation @import UserNotifications;
-
为localNotification
请求授权UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) { if (!error) { NSLog(@"request authorization succeeded!"); [self showAlert]; } }];
时间表localNotification
-
更新应用程序图标徽章数量
// //Deliver the notification at 08:30 everyday // NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; // dateComponents.hour = 8; // dateComponents.minute = 30; // UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComponents repeats:YES]; UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init]; content.title = [NSString localizedUserNotificationStringForKey:@"Elon said:" arguments:nil]; content.body = [NSString localizedUserNotificationStringForKey:@"Hello Tom!Get up, let's play with Jerry!" arguments:nil]; content.sound = [UNNotificationSound defaultSound]; /// 4. update application icon badge number content.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1); // Deliver the notification in five seconds. UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5.f repeats:NO]; UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond" content:content trigger:trigger]; /// 3. schedule localNotification UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { if (!error) { NSLog(@"add NotificationRequest succeeded!"); } }];
那么就会出现这样的:
如果重复用默认只显示一个 ,而不是显示在许多的iOS9锁定屏幕上: http://i67.tinypic.com/98t75s.jpg ,同时还支持3D触摸自动 http://a67.tinypic.com/dorw3b.jpg
我这里写了演示:iOS10AdaptationTips。
我解决我的问题如下:(火力地堡,斯威夫特3):
查找AppDelegate中此方法:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
找到这行:
completionHandler()
年底成立:如果你不能通过你的演示选项,以completionHandler方法
completionHandler([.alert,.sound,.badge])
通知不点火。
您可以参考以下链接了解详细使用http://jayprakashdubey.blogspot.in/2016/07/local- notification-in-ios-10-for_31.html –
如何设置不同的日期组件或如何多次调用本地通知? – ArgaPK