on master help sign in
Add notification authorization options
commit1af340dad75ecce4b872f7ee2dbeaa9808e1305b authorAli Abrar <aliabrar@gmail.com> dateMon 8 May 2017, 12:51 (9 years ago) parent837417c
4 files changed+84−27
jsaddle-wkwebview/cbits-uikit/AppDelegate.m+26−2 jsaddle-wkwebview/jsaddle-wkwebview.cabal+2−1 jsaddle-wkwebview/src-ghc/Language/Javascript/JSaddle/WKWebView.hs+56−9 jsaddle-wkwebview/src/Language/Javascript/JSaddle/WKWebView/Internal.hs−15
jsaddle-wkwebview/cbits-uikit/AppDelegate.m+26−2
@@ -10,6 +10,10 @@ extern void handlerCString(const char * _Nonnull, HsStablePtr);
1010
1111HsStablePtr globalHandler = 0;
1212BOOL global_requestAuthorizationWithOptions = NO;
13+BOOL global_requestAuthorizationOptionBadge = NO;
14+BOOL global_requestAuthorizationOptionSound = NO;
15+BOOL global_requestAuthorizationOptionAlert = NO;
16+BOOL global_requestAuthorizationOptionCarPlay = NO;
1317BOOL global_registerForRemoteNotifications = NO;
1418HsStablePtr global_didRegisterForRemoteNotificationsWithDeviceToken = 0;
1519
@@ -23,10 +27,22 @@ HsStablePtr global_didRegisterForRemoteNotificationsWithDeviceToken = 0;
2327 [self.window makeKeyAndVisible];
2428 UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
2529 if (global_requestAuthorizationWithOptions) {
26− [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge)
30+ UNAuthorizationOptions options = (UNAuthorizationOptions)0;
31+ if (global_requestAuthorizationOptionBadge) {
32+ options = options + UNAuthorizationOptionBadge;
33+ }
34+ if (global_requestAuthorizationOptionSound) {
35+ options = options + UNAuthorizationOptionSound;
36+ }
37+ if (global_requestAuthorizationOptionAlert) {
38+ options = options + UNAuthorizationOptionAlert;
39+ }
40+ if (global_requestAuthorizationOptionCarPlay) {
41+ options = options + UNAuthorizationOptionCarPlay;
42+ }
43+ [center requestAuthorizationWithOptions:(options)
2744 completionHandler:^(BOOL granted, NSError * _Nullable error) {
2845 // Handler used to alter application behavior based on types of notifications authorized
29− // Perhaps we don't need to do anything here.
3046 }];
3147 }
3248 if (global_registerForRemoteNotifications) {
@@ -75,11 +91,19 @@ HsStablePtr global_didRegisterForRemoteNotificationsWithDeviceToken = 0;
7591void runInWKWebView(HsStablePtr handler,
7692 const char * _Nonnull progName,
7793 const BOOL hs_requestAuthorizationWithOptions,
94+ const BOOL hs_requestAuthorizationOptionBadge,
95+ const BOOL hs_requestAuthorizationOptionSound,
96+ const BOOL hs_requestAuthorizationOptionAlert,
97+ const BOOL hs_requestAuthorizationOptionCarPlay,
7898 const BOOL hs_registerForRemoteNotifications,
7999 HsStablePtr hs_didRegisterForRemoteNotificationsWithDeviceToken) {
80100 @autoreleasepool {
81101 globalHandler = handler;
82102 global_requestAuthorizationWithOptions = hs_requestAuthorizationWithOptions;
103+ global_requestAuthorizationOptionBadge = hs_requestAuthorizationOptionBadge;
104+ global_requestAuthorizationOptionSound = hs_requestAuthorizationOptionSound;
105+ global_requestAuthorizationOptionAlert = hs_requestAuthorizationOptionAlert;
106+ global_requestAuthorizationOptionCarPlay = hs_requestAuthorizationOptionCarPlay;
83107 global_registerForRemoteNotifications = hs_registerForRemoteNotifications;
84108 global_didRegisterForRemoteNotificationsWithDeviceToken = hs_didRegisterForRemoteNotificationsWithDeviceToken;
85109 const char * _Nonnull argv [] = {"", 0};
jsaddle-wkwebview/jsaddle-wkwebview.cabal+2−1
@@ -31,7 +31,8 @@ library
3131 base <5,
3232 bytestring >=0.10.6.0 && <0.11,
3333 jsaddle >= 0.8.0.0 && <0.9,
34− data-default
34+ data-default,
35+ containers
3536 default-language: Haskell2010
3637 hs-source-dirs: src
3738 frameworks: Foundation, WebKit
jsaddle-wkwebview/src-ghc/Language/Javascript/JSaddle/WKWebView.hs+56−9
@@ -5,10 +5,16 @@ module Language.Javascript.JSaddle.WKWebView
55 , run
66 , runFile
77 , mainBundleResourcePath
8+ , AppDelegateConfig (..)
9+ , AppDelegateNotificationConfig (..)
10+ , AuthorizationOption (..)
811 ) where
912
1013import Data.ByteString (ByteString)
11−import Language.Javascript.JSaddle.WKWebView.Internal (jsaddleMain, jsaddleMainFile, WKWebView(..), mainBundleResourcePath, AppDelegateConfig (..), boolToCChar)
14+import Data.Default (Default, def)
15+import Data.Set (Set)
16+import qualified Data.Set as Set
17+import Language.Javascript.JSaddle.WKWebView.Internal (jsaddleMain, jsaddleMainFile, WKWebView(..), mainBundleResourcePath, boolToCChar)
1218import System.Environment (getProgName)
1319import Foreign.C.String (CString, withCString, peekCString)
1420import Foreign.C.Types
@@ -17,18 +23,52 @@ import Language.Javascript.JSaddle (JSM)
1723
1824foreign import ccall runInWKWebView :: StablePtr (WKWebView -> IO ())
1925 -> CString
20− -> CChar -- requestAuthorizationWithOptions
26+ -> CChar -- whether to run requestAuthorizationWithOptions
27+ -> CChar -- Ask for Badge authorization
28+ -> CChar -- Ask for Sound authorization
29+ -> CChar -- Ask for Alert authorization
30+ -> CChar -- Ask for CarPlay authorization
2131 -> CChar -- registerForRemoteNotifications
2232 -> StablePtr (CString -> IO ()) -- didRegisterForRemoteNotificationsWithDeviceToken
2333 -> IO ()
2434
35+
36+data AppDelegateConfig = AppDelegateConfig
37+ { _appDelegateConfig_appDelegateNotificationConfig :: AppDelegateNotificationConfig
38+ }
39+
40+instance Default AppDelegateConfig where
41+ def = AppDelegateConfig
42+ { _appDelegateConfig_appDelegateNotificationConfig = def
43+ }
44+
45+data AuthorizationOption = AuthorizationOption_Badge
46+ | AuthorizationOption_Sound
47+ | AuthorizationOption_Alert
48+ | AuthorizationOption_CarPlay
49+ deriving (Show, Read, Eq, Ord)
50+
51+data AppDelegateNotificationConfig = AppDelegateNotificationConfig
52+ { _appDelegateNotificationConfig_requestAuthorizationWithOptions :: Maybe (Set AuthorizationOption)
53+ , _appDelegateNotificationConfig_registerForRemoteNotifications :: Bool
54+ , _appDelegateNotificationConfig_didRegisterForRemoteNotificationsWithDeviceToken :: CString -> IO ()
55+ }
56+
57+instance Default AppDelegateNotificationConfig where
58+ def = AppDelegateNotificationConfig
59+ { _appDelegateNotificationConfig_requestAuthorizationWithOptions = Nothing
60+ , _appDelegateNotificationConfig_registerForRemoteNotifications = False
61+ , _appDelegateNotificationConfig_didRegisterForRemoteNotificationsWithDeviceToken = \_ -> return ()
62+ }
63+
64+
2565-- | Run JSaddle in a WKWebView
2666run :: (CString -> IO ()) -> JSM () -> IO ()
2767run didRegisterForRemoteNotificationsWithDeviceToken f = do
2868 handler <- newStablePtr (jsaddleMain f)
2969 progName <- getProgName
3070 deviceTokenHandler <- newStablePtr didRegisterForRemoteNotificationsWithDeviceToken
31− withCString progName $ \pn -> runInWKWebView handler pn 1 1 deviceTokenHandler
71+ withCString progName $ \pn -> runInWKWebView handler pn 1 1 1 1 1 1 deviceTokenHandler
3272
3373-- | Run JSaddle in a WKWebView first loading the specified file
3474-- from the mainBundle (relative to the resourcePath).
@@ -40,13 +80,20 @@ runFile :: ByteString -- ^ The file to navigate to.
4080runFile url allowing cfg f = do
4181 handler <- newStablePtr (jsaddleMainFile url allowing f)
4282 progName <- getProgName
43− let requestAuthorizationWithOptions = boolToCChar $ _appDelegateConfig_requestAuthorizationWithOptions cfg
44− registerForRemoteNotifications = boolToCChar $ _appDelegateConfig_registerForRemoteNotifications cfg
45− didRegisterForRemoteNotificationsWithDeviceToken <- newStablePtr $ _appDelegateConfig_didRegisterForRemoteNotificationsWithDeviceToken cfg
83+ let ncfg = _appDelegateConfig_appDelegateNotificationConfig cfg
84+ (requestAuthorizationWithOptions, authorizationOptions) =
85+ case _appDelegateNotificationConfig_requestAuthorizationWithOptions ncfg of
86+ Nothing -> (False, Set.empty)
87+ Just opts -> (True, opts)
88+ registerForRemoteNotifications = _appDelegateNotificationConfig_registerForRemoteNotifications ncfg
89+ didRegisterForRemoteNotificationsWithDeviceToken <- newStablePtr $ _appDelegateNotificationConfig_didRegisterForRemoteNotificationsWithDeviceToken ncfg
4690 withCString progName $ \pn -> runInWKWebView
4791 handler
4892 pn
49− requestAuthorizationWithOptions
50− registerForRemoteNotifications
93+ (boolToCChar requestAuthorizationWithOptions)
94+ (boolToCChar $ Set.member AuthorizationOption_Badge authorizationOptions)
95+ (boolToCChar $ Set.member AuthorizationOption_Sound authorizationOptions)
96+ (boolToCChar $ Set.member AuthorizationOption_Alert authorizationOptions)
97+ (boolToCChar $ Set.member AuthorizationOption_CarPlay authorizationOptions)
98+ (boolToCChar registerForRemoteNotifications)
5199 didRegisterForRemoteNotificationsWithDeviceToken
52−
jsaddle-wkwebview/src/Language/Javascript/JSaddle/WKWebView/Internal.hs−15
@@ -4,7 +4,6 @@ module Language.Javascript.JSaddle.WKWebView.Internal
44 , jsaddleMainFile
55 , WKWebView(..)
66 , mainBundleResourcePath
7− , AppDelegateConfig (..)
87 , boolFromCChar
98 , boolToCChar
109 ) where
@@ -13,7 +12,6 @@ import Control.Monad (void, join)
1312import Control.Concurrent (forkIO, forkOS)
1413import Control.Concurrent.MVar (newEmptyMVar, putMVar, takeMVar)
1514
16−import Data.Default
1715import Data.Monoid ((<>))
1816import Data.ByteString (useAsCString, packCString)
1917import qualified Data.ByteString as BS (ByteString)
@@ -96,19 +94,6 @@ handlerCString c fptr = do
9694 f <- deRefStablePtr fptr
9795 f c
9896
99−data AppDelegateConfig = AppDelegateConfig
100− { _appDelegateConfig_requestAuthorizationWithOptions :: Bool
101− , _appDelegateConfig_registerForRemoteNotifications :: Bool
102− , _appDelegateConfig_didRegisterForRemoteNotificationsWithDeviceToken :: CString -> IO ()
103− }
104−
105−instance Default AppDelegateConfig where
106− def = AppDelegateConfig
107− { _appDelegateConfig_requestAuthorizationWithOptions = False
108− , _appDelegateConfig_registerForRemoteNotifications = False
109− , _appDelegateConfig_didRegisterForRemoteNotificationsWithDeviceToken = \_ -> return ()
110− }
111−
11297boolToCChar :: Bool -> CChar
11398boolToCChar a = if a then 1 else 0
11499