Local Notifications make sound but do not display (Swift)

痞子三分冷 提交于 2019-12-01 08:14:19

I had the same problem.

If your content body is blank ie(content.body == "") then you won't get a notification even if you do have a title.

So the solution is to make sure that your content body is not an empty string.

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])
        }
    }

Add this code into didFinishLaunchingWithOption in AppDelegate.swift file

let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) 
{ (granted, error) in
// Enable or disable features based on authorization.
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!