Local Notifications make sound but do not display (Swift)

前端 未结 3 922
死守一世寂寞
死守一世寂寞 2021-01-13 10:38

I am trying to create a local notification for my app using Apple\'s UNUserNotificationCenter.

Here is my code:

let center = UNUserNotificationCenter         


        
3条回答
  •  别那么骄傲
    2021-01-13 10:56

    Add this Protocoal UNUserNotificationCenterDelegate. and add this delegate into your controller. and dont forget to set delegate.

    UNUserNotificationCenter.current().delegate = self
    
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
            print("Notification being triggered")
            //You can either present alert ,sound or increase badge while the app is in foreground too with ios 10
            //to distinguish between notifications
            if notification.request.identifier == "yourrequestid"{
                completionHandler( [.alert,.sound,.badge])
            }
        }
    

提交回复
热议问题