Local Notification on offline (Swift)

前端 未结 2 791
心在旅途
心在旅途 2020-12-22 05:54

I want to receive a local notification in my app (swift) when I\'m not connected to Internet and I have some information registered in my Local Data base. Is it possible to

2条回答
  •  误落风尘
    2020-12-22 06:18

    do like this :

        public func presentNotification(_ notifAction: String, notifBody: String) {
        let application = UIApplication.shared
        let applicationState = application.applicationState
    
        if applicationState == UIApplicationState.background {
            let localNotification = UILocalNotification()
            localNotification.alertBody = notifBody
            localNotification.alertAction = notifAction
            localNotification.soundName = UILocalNotificationDefaultSoundName
            localNotification.applicationIconBadgeNumber += 1
            application.presentLocalNotificationNow(localNotification)
        }
    }
    

    UIApplicationState has these states :

        case active
    
        case inactive
    
        case background
    

提交回复
热议问题