用户通知 Swift 3
Posted
技术标签:
【中文标题】用户通知 Swift 3【英文标题】:User Notifications Swift 3 【发布时间】:2017-01-18 20:11:38 【问题描述】:我想在按下按钮时发送简单的用户通知。用了网上的教程,还是不行。 错误是 AppDelegate 中的“线程 1:信号 SIGABRT”
这里是代码:
import UIKit
import UserNotifications
class Map: UIViewController, UNUserNotificationCenterDelegate
override func viewDidLoad()
super.viewDidLoad()
//Senden von Mitteilungen?
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) (granted, error) in
// Do any additional setup after loading the view.
@IBAction func Notification(_ sender: Any)
//Set the content of the notification
let content = UNMutableNotificationContent()
content.title = "Achtung!"
content.subtitle = "Verbindung zum Beacon wurde getrennt"
content.body = "---"
//Set the trigger of the notification -- here a timer.
let trigger = UNTimeIntervalNotificationTrigger(
timeInterval: 10.0,
repeats: false)
//Set the request for the notification from the above
let request = UNNotificationRequest(
identifier: "10.second.message",
content: content,
trigger: trigger
)
//Add the notification to the currnet notification center
UNUserNotificationCenter.current().add(
request, withCompletionHandler: nil)
这是应用代理:
import UIKit
import Firebase
import CoreLocation
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
FIRApp.configure()
return true
func applicationWillResignActive(_ application: UIApplication)
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
func applicationDidEnterBackground(_ application: UIApplication)
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
func applicationWillEnterForeground(_ application: UIApplication)
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
func applicationDidBecomeActive(_ application: UIApplication)
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
func applicationWillTerminate(_ application: UIApplication)
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
//EXTENSIONS
// Die Tastatur wird geschlossen, sobald der User außerhalb von ihr klickt
extension UIViewController
func hideKeyboardWhenTappedAround()
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
func dismissKeyboard()
view.endEditing(true)
我希望有人知道我的错误:)
【问题讨论】:
你能提供教程的链接吗? 当然:makeapppie.com/2016/08/08/… 【参考方案1】:-
将您的
@IBAction
方法重命名为其他名称。您有命名空间冲突。
确保@IBAction
实际链接到按钮并触发(向方法添加注释或断点)。
编辑:将您的 FIRApp.configure()
移出 applicationDidFinishLaunchingWithOptions。已知这会导致崩溃。
...
override init()
FIRApp.configure()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
return true
...
提示:方法名称以小写字母开头。带有大写字母的类。这样您以后可以轻松识别它们。
【讨论】:
你的意思是我应该重命名“通知”?当我这样做时,错误仍然显示 我需要更多 SIGABRT(信号中止)会引发致命错误,但它实际上可能是一千种不同的事情。您能否使用所有相关代码(包括 AppDelegate)更新您的问题 更新了我的答案。 让我们continue this discussion in chat。以上是关于用户通知 Swift 3的主要内容,如果未能解决你的问题,请参考以下文章