[英]Local notifications sent to background works in simulator but not on device
在模擬器中,我可以獲得所需的確切結果:當我觸發通知並且模擬器手機被鎖定時,通知會被推送到手表上。
這曾經在設備上運行(我的iOS 9.1上的iPhone 6和watchOS 2.0上的Watch)。 由於某種原因,它停止工作,我不知道如何調試問題。
因此,在設備上,我要進入主屏幕並鎖定手機,並確保我的手表處於睡眠狀態,以確保該應用程序在后台運行。 觸發通知后,什么也不會發生。 當我打開應用程序時,即會最終觸發通知。 IE(如果我觸發3條通知),直到我將應用打開回到前台,它們都沒有注冊。 這不是它在模擬器中的工作方式(模擬器具有上述正確的行為)。
該通知是由我更改存儲在Firebase數據庫中的文本對象觸發的。 更改將調用sendNotification函數。 同樣,這在模擬器中可以很好地工作,並且可以在設備上使用,但是由於某種原因,它不再起作用。
在應用程序委托中,我有:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Alert, categories: nil))
// Setting up Local Notification
let assistAction = UIMutableUserNotificationAction()
assistAction.activationMode = UIUserNotificationActivationMode.Background
assistAction.identifier = categoryID
assistAction.title = "Assist"
assistAction.authenticationRequired = false
assistAction.destructive = false
// Category
let assistCategory = UIMutableUserNotificationCategory()
assistCategory.identifier = categoryID
assistCategory.setActions([assistAction], forContext: UIUserNotificationActionContext.Default)
// Only way to get this to work with parameter
let categories = NSSet(object: assistCategory)
// Settings
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: categories as? Set<UIUserNotificationCategory>)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
// UIApplication.sharedApplication().registerForRemoteNotifications()
return true
}
然后在我的視圖控制器中,我有:
func sendNotification(customerName: String, helpText: String) {
// Local Notification
let notification = UILocalNotification()
notification.alertBody = "\(customerName) \(helpText)"
notification.soundName = UILocalNotificationDefaultSoundName
notification.fireDate = NSDate()
notification.category = categoryID
UIApplication.sharedApplication().presentLocalNotificationNow(notification)
print("Sent notification");
}
模擬器的工作方式似乎不同於設備。 模擬器本地通知的行為就像遠程推送通知,這很奇怪。
我最終放棄嘗試讓本地通知在應用程序在設備上處於后台時顯示,因為我了解到這不是預期的行為。
正確的解決方案是僅設置遠程推送通知,這是我使用Parse進行的。 現在它正在按預期工作。 我有一個使用Parse REST API將POST請求發送到Parse的Web服務器,然后將我的iOS應用設置為接收遠程推送通知,當手機鎖定時,該通知現在顯示在手表上。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.