@@ -9,6 +9,7 @@ module Language.Javascript.JSaddle.WKWebView
99 , runHTMLWithBaseURL
1010 , runFile
1111 , mainBundleResourcePath
12+ , ApplicationState (..)
1213 , AppDelegateConfig (..)
1314 , AppDelegateNotificationConfig (..)
1415 , AuthorizationOption (..)
@@ -24,7 +25,7 @@ import Language.Javascript.JSaddle.WKWebView.Internal
2425 (jsaddleMain, jsaddleMainHTMLWithBaseURL, jsaddleMainFile, WKWebView(..), mainBundleResourcePath)
2526import System.Environment (getProgName)
2627import Foreign.C.String (CString, withCString)
27−import Foreign.C.Types (CBool)
28+import Foreign.C.Types (CBool, CInt)
2829import Foreign.StablePtr (StablePtr, newStablePtr)
2930import Language.Javascript.JSaddle (JSM)
3031
@@ -40,6 +41,7 @@ foreign import ccall runInWKWebView
4041 -> StablePtr (IO ()) -- applicationWillTerminate
4142 -> StablePtr (IO ()) -- applicationSignificantTimeChange
4243 -> StablePtr (CString -> IO ()) -- applicationUniversalLink
44+ -> StablePtr (CInt -> CString -> IO ()) -- applicationDidReceiveRemoteNotification
4345 -> Word64 -- whether to run requestAuthorizationWithOptions
4446 -> Word64 -- Ask for Badge authorization
4547 -> Word64 -- Ask for Sound authorization
@@ -66,6 +68,11 @@ data AppDelegateConfig = AppDelegateConfig
6668 --
6769 -- The Core Foundation plist key CFBundleURLTypes controls which URL schemes
6870 -- the app should respond to.
71+ , _appDelegateConfig_applicationDidReceiveRemoteNotification :: ApplicationState -> CString -> IO ()
72+ -- ^ Called when the application receives a remote notification.
73+ -- 'ApplicationState' is the current state of the app.
74+ -- The 'CString' JSON-encoded user info contained in the notification.
75+ -- For more information see https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623117-application?language=objc
6976 , _appDelegateConfig_appDelegateNotificationConfig :: AppDelegateNotificationConfig
7077 , _appDelegateConfig_developerExtrasEnabled :: Bool
7178 -- ^ Allow devtools in the app. Defaults to 'True'
@@ -89,6 +96,7 @@ instance Default AppDelegateConfig where
8996 , _appDelegateConfig_applicationWillTerminate = return ()
9097 , _appDelegateConfig_applicationSignificantTimeChange = return ()
9198 , _appDelegateConfig_applicationUniversalLink = \_ -> return ()
99+ , _appDelegateConfig_applicationDidReceiveRemoteNotification = \_ _ -> return ()
92100 , _appDelegateConfig_appDelegateNotificationConfig = def
93101 , _appDelegateConfig_developerExtrasEnabled = True
94102 , _appDelegateConfig_applicationOpenFile = \_ -> return False
@@ -100,6 +108,12 @@ data AuthorizationOption = AuthorizationOption_Badge
100108 | AuthorizationOption_CarPlay
101109 deriving (Show, Read, Eq, Ord)
102110
111+data ApplicationState = ApplicationState_Active
112+ | ApplicationState_Inactive
113+ | ApplicationState_Background
114+ | ApplicationState_Unknown CInt
115+ deriving (Show, Read, Eq, Ord)
116+
103117data AppDelegateNotificationConfig = AppDelegateNotificationConfig
104118 { _appDelegateNotificationConfig_requestAuthorizationWithOptions :: Maybe (Set AuthorizationOption)
105119 , _appDelegateNotificationConfig_registerForRemoteNotifications :: Bool
@@ -162,6 +176,13 @@ run' cfg main = do
162176 applicationWillTerminate <- newStablePtr $ _appDelegateConfig_applicationWillTerminate cfg
163177 applicationSignificantTimeChange <- newStablePtr $ _appDelegateConfig_applicationSignificantTimeChange cfg
164178 applicationUniversalLink <- newStablePtr $ _appDelegateConfig_applicationUniversalLink cfg
179+ applicationDidReceiveRemoteNotification <- newStablePtr $ \n s -> do
180+ let appState = case n of
181+ 0 -> ApplicationState_Active
182+ 1 -> ApplicationState_Inactive
183+ 2 -> ApplicationState_Background
184+ _ -> ApplicationState_Unknown n
185+ _appDelegateConfig_applicationDidReceiveRemoteNotification cfg appState s
165186 applicationOpenFile <- newStablePtr $ fmap fromBool . _appDelegateConfig_applicationOpenFile cfg
166187
167188 -- AppDelegate notification configuration
@@ -185,6 +206,7 @@ run' cfg main = do
185206 applicationWillTerminate
186207 applicationSignificantTimeChange
187208 applicationUniversalLink
209+ applicationDidReceiveRemoteNotification
188210 (fromBool requestAuthorizationWithOptions)
189211 (fromBool $ Set.member AuthorizationOption_Badge authorizationOptions)
190212 (fromBool $ Set.member AuthorizationOption_Sound authorizationOptions)