@@ -9,6 +9,7 @@ module Language.Javascript.JSaddle.WKWebView
9 , runHTMLWithBaseURL
9 , runHTMLWithBaseURL
11 , mainBundleResourcePath
11 , mainBundleResourcePath
12+ , ApplicationState (..)
12 , AppDelegateConfig (..)
13 , AppDelegateConfig (..)
13 , AppDelegateNotificationConfig (..)
14 , AppDelegateNotificationConfig (..)
14 , AuthorizationOption (..)
15 , AuthorizationOption (..)
@@ -24,7 +25,7 @@ import Language.Javascript.JSaddle.WKWebView.Internal
24 (jsaddleMain, jsaddleMainHTMLWithBaseURL, jsaddleMainFile, WKWebView(..), mainBundleResourcePath)
25 (jsaddleMain, jsaddleMainHTMLWithBaseURL, jsaddleMainFile, WKWebView(..), mainBundleResourcePath)
25import System.Environment (getProgName)
26import System.Environment (getProgName)
26import Foreign.C.String (CString, withCString)
27import Foreign.C.String (CString, withCString)
27−import Foreign.C.Types (CBool)
28+import Foreign.C.Types (CBool, CInt)
28import Foreign.StablePtr (StablePtr, newStablePtr)
29import Foreign.StablePtr (StablePtr, newStablePtr)
29import Language.Javascript.JSaddle (JSM)
30import Language.Javascript.JSaddle (JSM)
@@ -40,6 +41,7 @@ foreign import ccall runInWKWebView
40 -> StablePtr (IO ()) -- applicationWillTerminate
41 -> StablePtr (IO ()) -- applicationWillTerminate
41 -> StablePtr (IO ()) -- applicationSignificantTimeChange
42 -> StablePtr (IO ()) -- applicationSignificantTimeChange
42 -> StablePtr (CString -> IO ()) -- applicationUniversalLink
43 -> StablePtr (CString -> IO ()) -- applicationUniversalLink
44+ -> StablePtr (CInt -> CString -> IO ()) -- applicationDidReceiveRemoteNotification
43 -> Word64 -- whether to run requestAuthorizationWithOptions
45 -> Word64 -- whether to run requestAuthorizationWithOptions
44 -> Word64 -- Ask for Badge authorization
46 -> Word64 -- Ask for Badge authorization
45 -> Word64 -- Ask for Sound authorization
47 -> Word64 -- Ask for Sound authorization
@@ -66,6 +68,11 @@ data AppDelegateConfig = AppDelegateConfig
67 -- The Core Foundation plist key CFBundleURLTypes controls which URL schemes
69 -- The Core Foundation plist key CFBundleURLTypes controls which URL schemes
68 -- the app should respond to.
70 -- 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
69 , _appDelegateConfig_appDelegateNotificationConfig :: AppDelegateNotificationConfig
76 , _appDelegateConfig_appDelegateNotificationConfig :: AppDelegateNotificationConfig
70 , _appDelegateConfig_developerExtrasEnabled :: Bool
77 , _appDelegateConfig_developerExtrasEnabled :: Bool
71 -- ^ Allow devtools in the app. Defaults to 'True'
78 -- ^ Allow devtools in the app. Defaults to 'True'
@@ -89,6 +96,7 @@ instance Default AppDelegateConfig where
89 , _appDelegateConfig_applicationWillTerminate = return ()
96 , _appDelegateConfig_applicationWillTerminate = return ()
90 , _appDelegateConfig_applicationSignificantTimeChange = return ()
97 , _appDelegateConfig_applicationSignificantTimeChange = return ()
91 , _appDelegateConfig_applicationUniversalLink = \_ -> return ()
98 , _appDelegateConfig_applicationUniversalLink = \_ -> return ()
99+ , _appDelegateConfig_applicationDidReceiveRemoteNotification = \_ _ -> return ()
92 , _appDelegateConfig_appDelegateNotificationConfig = def
100 , _appDelegateConfig_appDelegateNotificationConfig = def
93 , _appDelegateConfig_developerExtrasEnabled = True
101 , _appDelegateConfig_developerExtrasEnabled = True
94 , _appDelegateConfig_applicationOpenFile = \_ -> return False
102 , _appDelegateConfig_applicationOpenFile = \_ -> return False
@@ -100,6 +108,12 @@ data AuthorizationOption = AuthorizationOption_Badge
100 | AuthorizationOption_CarPlay
108 | AuthorizationOption_CarPlay
101 deriving (Show, Read, Eq, Ord)
109 deriving (Show, Read, Eq, Ord)
111+data ApplicationState = ApplicationState_Active
112+ | ApplicationState_Inactive
113+ | ApplicationState_Background
114+ | ApplicationState_Unknown CInt
115+ deriving (Show, Read, Eq, Ord)
103data AppDelegateNotificationConfig = AppDelegateNotificationConfig
117data AppDelegateNotificationConfig = AppDelegateNotificationConfig
104 { _appDelegateNotificationConfig_requestAuthorizationWithOptions :: Maybe (Set AuthorizationOption)
118 { _appDelegateNotificationConfig_requestAuthorizationWithOptions :: Maybe (Set AuthorizationOption)
105 , _appDelegateNotificationConfig_registerForRemoteNotifications :: Bool
119 , _appDelegateNotificationConfig_registerForRemoteNotifications :: Bool
@@ -162,6 +176,13 @@ run' cfg main = do
162 applicationWillTerminate <- newStablePtr $ _appDelegateConfig_applicationWillTerminate cfg
176 applicationWillTerminate <- newStablePtr $ _appDelegateConfig_applicationWillTerminate cfg
163 applicationSignificantTimeChange <- newStablePtr $ _appDelegateConfig_applicationSignificantTimeChange cfg
177 applicationSignificantTimeChange <- newStablePtr $ _appDelegateConfig_applicationSignificantTimeChange cfg
164 applicationUniversalLink <- newStablePtr $ _appDelegateConfig_applicationUniversalLink cfg
178 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
165 applicationOpenFile <- newStablePtr $ fmap fromBool . _appDelegateConfig_applicationOpenFile cfg
186 applicationOpenFile <- newStablePtr $ fmap fromBool . _appDelegateConfig_applicationOpenFile cfg
167 -- AppDelegate notification configuration
188 -- AppDelegate notification configuration
@@ -185,6 +206,7 @@ run' cfg main = do
185 applicationWillTerminate
206 applicationWillTerminate
186 applicationSignificantTimeChange
207 applicationSignificantTimeChange
187 applicationUniversalLink
208 applicationUniversalLink
209+ applicationDidReceiveRemoteNotification
188 (fromBool requestAuthorizationWithOptions)
210 (fromBool requestAuthorizationWithOptions)
189 (fromBool $ Set.member AuthorizationOption_Badge authorizationOptions)
211 (fromBool $ Set.member AuthorizationOption_Badge authorizationOptions)
190 (fromBool $ Set.member AuthorizationOption_Sound authorizationOptions)
212 (fromBool $ Set.member AuthorizationOption_Sound authorizationOptions)