Local Notification on offline (Swift)

前端 未结 2 706
心在旅途
心在旅途 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
    
    0 讨论(0)
  • 2020-12-22 06:34

    Local Notification doesnot requires internet. About Local Notification from apple developer site

    Local notifications give you a way to alert the user at times when your app might not be running. You schedule local notifications at a time when your app is running either in the foreground or background. After scheduling a notification, the system takes on the responsibility of delivering the notification to the user at the appropriate time. Your app does not need to be running for the system to deliver the notification.

    For more info check this link. You can also check this link for tutorial.

    0 讨论(0)
提交回复
热议问题