ios - swift 4在后台自定义本地通知
问题描述
我已经编写了一些代码来在我的应用程序中实现自定义通知,但是当应用程序处于后台模式时这似乎不起作用这里是下面的代码:
let content = UNMutableNotificationContent()
content.title = "test notifaction"
content.body = "test notification after 5 second"
content.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: true)
let request = UNNotificationRequest(identifier: "testidentifire", content: content, trigger: trigger)
应用内委托
//user notification method
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert,.sound])
}
//response to user notification
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if response.notification.request.identifier == "testidentifire"
{
print("test")
}
completionHandler()
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound,.badge]) { (granted, error) in
print("granted\(granted)")
}
return true
}
现在,当我搜索时,我发现每个我无法理解我的代码有什么问题的地方都有相同的代码
解决方案
在你看来试试这个确实加载它对我有用
//sendign local notification you need three object a contant,trigger,represh
let content = UNMutableNotificationContent()
content.title = "test notifaction"
content.body = "test notification after 5 second"
content.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: true)
let request = UNNotificationRequest(identifier: "testidentifire", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { (error) in
print("error\(error )")
}
推荐阅读
- python-3.x - 根据来自另一个张量的值将值分配给一个张量
- r - 使用 R 中的 DBI 包连接到 teradata
- c# - 撤消计算并将其从运行总计中减去
- python-3.x - 找出人们喝咖啡的平均数量——几乎是正确的(在异常处理方面遇到了一些麻烦)
- c++ - 如何调用 GetAltMonthNames 来填充一个安全的外国语言环境月份字符串数组?
- android - recyclerview of recyclerviews with title
- regex - 如何在 VB.net 中获取文本字符串并检查 Regex 是否找到文本字符串?
- javascript - 检查元素是否具有类,如果确实将其添加到 n 元素 - 但动态
- scala - 错误:未找到:值 PlayScala - sbt.compiler.EvalException:表达式中的类型错误
- java - 使用 vaadin Page.getCurrent().open() 方法时有没有办法传递参数?