on master help sign in
Merge pull request #117 from obsidiansystems/eac@upstream-changes

Add applicationDidReceiveRemoteNotification

commit17e9e3a50e90602ee4c610f02c6fd9a6b43f96cf authorHamish Mackenzie <Hamish.K.Mackenzie@googlemail.com> dateMon 6 Jul 2020, 01:31 (6 years ago) parentd569be4d7a2d9f
4 files changed+56−2
jsaddle-wkwebview/cbits-uikit/AppDelegate.m+12 jsaddle-wkwebview/default.nix+14 jsaddle-wkwebview/src-ghc/Language/Javascript/JSaddle/WKWebView.hs+23−1 jsaddle-wkwebview/src/Language/Javascript/JSaddle/WKWebView/Internal.hs+7−1
jsaddle-wkwebview/cbits-uikit/AppDelegate.m+12
@@ -5,6 +5,7 @@
55
66extern void callIO(HsStablePtr);
77extern void callWithCString(const char * _Nonnull, HsStablePtr);
8+extern void callWithCIntCString(int n, const char * _Nonnull, HsStablePtr);
89
910@interface AppDelegate ()
1011
@@ -20,6 +21,7 @@ HsStablePtr global_applicationWillEnterForeground = 0;
2021HsStablePtr global_applicationWillTerminate = 0;
2122HsStablePtr global_applicationSignificantTimeChange = 0;
2223HsStablePtr global_applicationUniversalLink = 0;
24+HsStablePtr global_applicationDidReceiveRemoteNotification = 0;
2325uint64_t global_requestAuthorizationWithOptions = 0;
2426uint64_t global_requestAuthorizationOptionBadge = 0;
2527uint64_t global_requestAuthorizationOptionSound = 0;
@@ -115,6 +117,14 @@ HsStablePtr global_didFailToRegisterForRemoteNotificationsWithError = 0;
115117 if ([userInfo valueForKeyPath:@"aps.badge"] != nil) {
116118 [UIApplication sharedApplication].applicationIconBadgeNumber=[[[userInfo objectForKey:@"aps"] objectForKey:@"badge"] intValue];
117119 }
120+ NSError *error;
121+ NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userInfo options:0 error:&error];
122+ if (jsonData) {
123+ NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
124+ callWithCIntCString((int) application.applicationState, [jsonString UTF8String], global_applicationDidReceiveRemoteNotification);
125+ } else {
126+ NSLog(@"jsaddle-wkwebview didReceiveRemoteNotification handler: error while serialising push notification as JSON");
127+ }
118128 completionHandler(UIBackgroundFetchResultNewData);
119129}
120130
@@ -152,6 +162,7 @@ void runInWKWebView(HsStablePtr handler,
152162 HsStablePtr hs_applicationWillTerminate,
153163 HsStablePtr hs_applicationSignificantTimeChange,
154164 HsStablePtr hs_applicationUniversalLink,
165+ HsStablePtr hs_applicationDidReceiveRemoteNotification,
155166 const uint64_t hs_requestAuthorizationWithOptions,
156167 const uint64_t hs_requestAuthorizationOptionBadge,
157168 const uint64_t hs_requestAuthorizationOptionSound,
@@ -172,6 +183,7 @@ void runInWKWebView(HsStablePtr handler,
172183 global_applicationWillTerminate = hs_applicationWillTerminate;
173184 global_applicationSignificantTimeChange = hs_applicationSignificantTimeChange;
174185 global_applicationUniversalLink = hs_applicationUniversalLink;
186+ global_applicationDidReceiveRemoteNotification = hs_applicationDidReceiveRemoteNotification;
175187 global_requestAuthorizationWithOptions = hs_requestAuthorizationWithOptions;
176188 global_requestAuthorizationOptionBadge = hs_requestAuthorizationOptionBadge;
177189 global_requestAuthorizationOptionSound = hs_requestAuthorizationOptionSound;
jsaddle-wkwebview/default.nixnew file+14
@@ -0,0 +1,14 @@
1+{ filterSource ? (x: x) }:
2+self: super:
3+let
4+ pkgs = self.callPackage ({pkgs}: pkgs) {};
5+ inherit (pkgs) lib;
6+in
7+with pkgs.haskell.lib;
8+overrideCabal (self.callCabal2nix "jsaddle-wkwebview" (filterSource ./.) {}) (drv: {
9+ libraryFrameworkDepends = (drv.libraryFrameworkDepends or []) ++
10+ (if pkgs.stdenv.hostPlatform.useiOSPrebuilt then [
11+ "${pkgs.buildPackages.darwin.xcode}/Contents/Developer/Platforms/${pkgs.stdenv.hostPlatform.xcodePlatform}.platform/Developer/SDKs/${pkgs.stdenv.hostPlatform.xcodePlatform}.sdk/System"
12+ ] else (with pkgs.buildPackages.darwin.apple_sdk.frameworks; [ Cocoa WebKit ]));
13+ buildDepends = lib.optional (!pkgs.stdenv.hostPlatform.useiOSPrebuilt) [ pkgs.buildPackages.darwin.cf-private ];
14+})
jsaddle-wkwebview/src-ghc/Language/Javascript/JSaddle/WKWebView.hs+23−1
@@ -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)
jsaddle-wkwebview/src/Language/Javascript/JSaddle/WKWebView/Internal.hs+7−1
@@ -20,7 +20,7 @@ import qualified Data.Text as T (pack)
2020import Data.Text.Encoding (encodeUtf8)
2121
2222import Foreign.C.String (CString)
23−import Foreign.C.Types (CBool(..))
23+import Foreign.C.Types (CBool(..), CInt(..))
2424import Foreign.Ptr (Ptr, nullPtr)
2525import Foreign.StablePtr (StablePtr, newStablePtr, deRefStablePtr)
2626
@@ -37,6 +37,7 @@ foreign export ccall jsaddleStart :: StablePtr (IO ()) -> IO ()
3737foreign export ccall jsaddleResult :: StablePtr (Results -> IO ()) -> CString -> IO ()
3838foreign export ccall jsaddleSyncResult :: StablePtr (Results -> IO Batch) -> JSaddleHandler -> CString -> IO ()
3939foreign export ccall callWithWebView :: WKWebView -> StablePtr (WKWebView -> IO ()) -> IO ()
40+foreign export ccall callWithCIntCString :: CInt -> CString -> StablePtr (CInt -> CString -> IO ()) -> IO ()
4041foreign export ccall callWithCString :: CString -> StablePtr (CString -> IO ()) -> IO ()
4142foreign export ccall callWithCStringReturningBool :: CString -> StablePtr (CString -> IO CBool) -> IO CBool
4243foreign export ccall callIO :: StablePtr (IO ()) -> IO ()
@@ -128,6 +129,11 @@ callWithCString c fptr = do
128129 f <- deRefStablePtr fptr
129130 f c
130131
132+callWithCIntCString :: CInt -> CString -> StablePtr (CInt -> CString -> IO ()) -> IO ()
133+callWithCIntCString n s fptr = do
134+ f <- deRefStablePtr fptr
135+ f n s
136+
131137callWithCStringReturningBool :: CString -> StablePtr (CString -> IO CBool) -> IO CBool
132138callWithCStringReturningBool c fptr = do
133139 f <- deRefStablePtr fptr