我正在尝试使用新的UNNotificationContentExtension来显示本地通知的自定义用户界面,但只显示默认通知警报.
我使用Xcode模板创建了扩展,并UNNotificationExtensionCategory在Info.plist中指定了该扩展.

在此之后,我注册了通知和设置UNNotificationCategory在application(_:didFinishLaunchingWithOptions:)
let center = UNUserNotificationCenter.current()
center.requestAuthorization([.alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
let action = UNNotificationAction(identifier: "Delete", title: "Delete", options: [])
let category = UNNotificationCategory(identifier: "notify-test", actions: [action], minimalActions: [], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([category])
Run Code Online (Sandbox Code Playgroud)
我安排通知在应用程序使用此代码进入后台后五秒触发:
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: "Hello!", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: "Hello_message_body", arguments: nil)
content.sound = UNNotificationSound.default()
content.categoryIdentifier = "notify-test"
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: false) …Run Code Online (Sandbox Code Playgroud) 我无法跨时区正确触发iOS 10本地通知(UNNotification)。
在+10小时的时区中,我希望它在17:00:00触发并安排它。
稍后,当我查看计划的通知时,我会得到它的下一个触发日期,仍在时区+10小时,它将显示17:00:00。
但是,当我在设备上将时区更改为+8小时,并查看预定的通知时,我会得到它的下一个触发日期,并且仍然显示 17:00:00。我希望它能在两个小时前的15:00:00触发。
如何使它像这样工作?我试图改变用于创建送入UNCalendarNotificationTrigger日期组件的日历时区(试过的事情是:TimeZone.init(identifier: "Somewhere"),TimeZone.autoupdatingCurrent,可能还有其他人)。但这不会改变,即使我更改了时区,我也总是得到完全相同的触发日期。
因为UILocalNotification现在已弃用,所以我将代码移至新UNNotificationRequestAPI。
它指出:' cancelLocalNotification ' 在 iOS 10.0 中被弃用:使用 UserNotifications Framework's-[UNUserNotificationCenter removePendingNotificationRequestsWithIdentifiers:]
但它似乎并不相等-虽然我可以随时删除消息cancelLocalNotification(即使它们已显示/已发送),但似乎removePendingNotificationRequestsWithIdentifiers只能删除未发送的通知。这真的很烦人。
所以我的问题是:是否有一种有效的方法可以删除排队的通知,UNNotificationRequest或者我应该忽略那些弃用警告?
我正在尝试检索仍显示在通知中心的所有已发送通知,但UNUserNotificationCenter getDeliveredNotifications(completionHandler:)无法正常工作。我可以获得所有待处理的通知,UNUserNotificationCenter getPendingNotificationRequests(completionHandler:)但getDeliveredNotifications即使通知中心有通知,也只会返回 0 计数。
TL; DR:需要在APNs通知有效负载JSON中设置哪些密钥才能对应对象的threadIdentifier属性UNNotificationContent?例如,"category"钥匙对应于categoryIdentifier财产.
iOS 10引入了Notification Content Extension允许我们在扩展通知时呈现视图控制器.
我们提供的视图控制器符合UNNotificationContentExtension协议,这要求我们实现该didReceive(_:)方法.
该方法的文档包括以下段落:
当您的视图控制器可见时,可以多次调用此方法.具体地说,当新的通知到达时,再次调用它的threadIdentifier值与已经显示的通知的线程标识符匹配.
threadIdentifier可以在本地通知的代码中设置该属性,但我不知道如何将其设置为从服务器发送到APN的远程通知.
该UNNotificationContent文档描述了此处的属性:http://developer.apple.com/reference/usernotifications/unnotificationcontent
以下JSON包含我尝试过的密钥("thread"和"thread-identifier"):
{
"aps" : {
"alert" : "Hello World!",
"sound" : "default",
"category" : "example-category",
"thread" : "example-thread",
"thread-identifier" : "example-thread-identifier"
}
"custom-field" : "some value",
}
Run Code Online (Sandbox Code Playgroud)
我找不到Apple提供的有关如何设置此文档的任何文档.有人可以帮忙吗?
我正在尝试使用Apple的UNUserNotificationCenter为我的应用创建本地通知.
这是我的代码:
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = task.name
content.body = task.notes
content.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: task.UUID, content: content, trigger: trigger)
center.add(request) { (error:Error?) in
if let theError = error {
print("Notification scheduling error: \(error)")
}else{
print("Notification was scheduled successfully")
}
}
Run Code Online (Sandbox Code Playgroud)
访问请求:
let center = UNUserNotificationCenter.current()
//request notification access
let options: UNAuthorizationOptions = [.alert, .sound]
center.requestAuthorization(options: options) { (granted, error) in
if !granted {
print("Notification …Run Code Online (Sandbox Code Playgroud) 我有一个触发器来向用户显示通知:
let content = UNMutableNotificationContent()
content.title = "Title"
content.body = "Body"
content.sound = UNNotificationSound.default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 20, repeats: false)
let request = UNNotificationRequest(identifier: "TestIdentifier", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
Run Code Online (Sandbox Code Playgroud)
一旦设置了该触发器,我有没有办法取消它?
如何在timeInterval用完之前取消通知并调用通知?
我有以下代码:
func sendLucidNotification() {
let content = UNMutableNotificationContent()
content.title = "10 Second Notification Demo"
content.subtitle = "From MakeAppPie.com"
content.body = "Notification after 10 seconds - Your pizza is Ready!!"
content.categoryIdentifier = "message"
}
Run Code Online (Sandbox Code Playgroud)
当我调用,UNMutableNotificationContent或任何"UNMutableNotification"变体时,我收到以下错误:
Use of unresolved identifier: "UNMutableNotificationContent"
Run Code Online (Sandbox Code Playgroud)
我在Xcode 8.0上,但是当我尝试自动完成时,它也无法识别它.有任何想法吗 ?谢谢!
我有一个简单的应用程序来播放推送通知.我有通知服务扩展工作.我可以使用图片URL发送远程通知并加载它.
我似乎无法让Notification Content Extension工作.我已经完成了多个教程,他们都说,只需从目标菜单创建一个Notification Content Extension,然后在Notification Content Extensions Info.plist中设置
UNNotificationCategory
Run Code Online (Sandbox Code Playgroud)
一些字符串.然后当您按下通知时,在"aps"json-block内部确保类别与UNNotificationCategory相同.
当我收到通知时,我会尝试向左或向右滑动它,并且没有任何实际发生.但是,服务扩展正在发挥作用.
我正在使用带有ios10和xCode 8.0的iPhone 5.我读到,只有具有3d touch的设备才可以查看内容扩展,但是自从改变了以后xCode 8已经超出测试版.
有任何想法吗?我该如何调试呢?我已经尝试选择通知扩展程序运行应用程序并打印出内部的东西
didReceive
Run Code Online (Sandbox Code Playgroud)
但我没有运气.
push-notification apple-push-notifications swift ios10 unnotificationrequest
这是我的代码 UNUserNotification
func scheduleNotification() {
UNUserNotificationCenter.current().getNotificationSettings { (notificationSettings) in
switch notificationSettings.authorizationStatus {
case .notDetermined:
self.requestAuthorization(completionHandler: { (success) in
guard success else { return }
// Schedule Local Notification
self.scheduleLocalNotification()
})
case .authorized:
// Schedule Local Notification
self.scheduleLocalNotification()
case .denied:
print("Application Not Allowed to Display Notifications")
}
}
}
private func scheduleLocalNotification() {
// Create Notification Content
let notificationContent = UNMutableNotificationContent()
// Configure Notification Content
notificationContent.title = "Hello"
notificationContent.body = ""
// Add Trigger
let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 10.0, repeats: false) …Run Code Online (Sandbox Code Playgroud) 这个问题已经在 SO 上得到了回答。这是参考:
但到目前为止我已经尝试过:
var fortnightPart1 = DateComponents()
fortnightPart1.weekday = Calendar.current.component(.day, from: Sdate) //(Day..here Sunday)
fortnightPart1.weekdayOrdinal = 2 //= n == (nth Day in the month...here 2nd Sunday in every month month)
fortnightPart1.hour = Calendar.current.component(.hour, from: Sdate)
fortnightPart1.minute = Calendar.current.component(.minute, from: Sdate)
fortnightPart1.second = Calendar.current.component(.second, from: Sdate)
trigger = UNCalendarNotificationTrigger(dateMatching: fortnightPart1, repeats: repeate)
Run Code Online (Sandbox Code Playgroud)
我无法在 14 天后实现它。虽然,它来自一个随机数据。任何人都可以检查代码有什么问题吗?
更新:
根据您的建议(/sf/answers/3225074821/),这是最终有效的代码!
//for 1st time notification
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = title
content.body = body …Run Code Online (Sandbox Code Playgroud) calendar uilocalnotification swift3 unnotificationrequest unnotificationtrigger