所以我一直在尝试向新的UNUserNotificationCenter添加通知,但我似乎无法得到它。
我的视图控制器有一个动作:
Notification Content Extension
我在项目中也有一个DECLARE @employee table(EmployeeID varchar(10), DOB date);
INSERT INTO @employee(EmployeeID, DOB)
VALUES('0001', '01-Dec-1990'),
('0002', '06-Jan-1993'),
('0003', '04-Mar-1987'),
('0004', '12-Feb-1996');
DECLARE @dateStart date = '01-Jan-1990';
DECLARE @dateEnd date = '27-Feb-1997';
DECLARE @includeYear bit = 0;
If @includeYear = 0
Begin
SET @dateStart = CAST(('2000-' + CAST(MONTH(@dateStart) AS varchar(10)) + '-' + CAST(DAY(@dateStart) as varchar(10))) AS date);
SET @dateEnd = CAST(('2000-' + CAST(MONTH(@dateEnd) AS varchar(10)) + '-' + CAST(DAY(@dateEnd) as varchar(10))) AS date);
End
If @includeYear = 1
Begin
SELECT *
FROM @employee
WHERE DOB BETWEEN @dateStart AND @dateEnd
End
Else
Begin
SELECT *
FROM @employee
WHERE CAST(('2000-' + CAST(MONTH(DOB) AS varchar(10)) + '-' + CAST(DAY(DOB) as varchar(10))) AS date) BETWEEN @dateStart AND @dateEnd
End
,但它似乎根本没有被触发,任何想法我都错过了什么?我尝试了用户文档中的示例,但它没有告诉我更多或者我错过了它。
此处:https://developer.apple.com/reference/usernotifications/unmutablenotificationcontent
此外: https://developer.apple.com/reference/usernotificationsui https://developer.apple.com/reference/usernotifications
编辑:
因此将应用程序置于后台就可以了。
答案 0 :(得分:29)
你需要注册通知......我试过了,这很有用。
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开始提供通知。
使用以下回调配置通知以显示在前台。
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
Here是一个示例项目。
答案 1 :(得分:17)
使用Objective-C实现:
我在这里写了一个演示项目: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 Touch http://a67.tinypic.com/dorw3b.jpg
我在这里写了一个演示:iOS10AdaptationTips。
答案 2 :(得分:1)
以下是几个步骤:
确保您具有权限。如果不是,请使用 UNUserNotificationCenter.current()。requestAuthorization 来获取。或者,如果您想显示多次弹出请求,请遵循answer。
如果要显示通知前景,则必须将 UNUserNotificationCenterDelegate 分配给某个地方。
给我看看代码
@IBAction func sendPressed(_ sender: AnyObject) {
let content = UNMutableNotificationContent()
content.title = "Hello"
content.body = "What up?"
content.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: false)
let request = UNNotificationRequest.init(identifier: "FiveSecond", content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request) { (error) in
print(error)
}
}
override func viewDidLoad(_ animated: Bool) {
super.viewDidLoad(animated)
// Assign the delegate
UNUserNotificationCenter.current().delegate = self
// Ask the permission
let center = UNUserNotificationCenter.current()
center.requestAuthorization([.alert, .sound]) { (granted, error) in
if granted {
// do something
}
}
}
// Remember to add UNUserNotificationCenterDelegate to your view controller
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("Got the msg...")
completionHandler([.badge, .sound, .alert])
}
答案 3 :(得分:0)
我解决了我的问题如下(Firebase,Swift 3):
在AppDelegate上找到此方法:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
找到这一行:
completionHandler()
结束设置:
completionHandler([.alert,.sound,.badge])
如果您未将演示文稿选项传递给completionHandler方法,则不会触发。
答案 4 :(得分:0)
我已经为Swift 3做了一个可能有用的实现,你可以在这里查看:https://stackoverflow.com/a/45381380/2296630