on master help sign in
Add notification authorization options
1af340dAli Abrar
jsaddle-wkwebview/cbits-uikit/AppDelegate.m+26−2
@@ -10,6 +10,10 @@ extern void handlerCString(const char * _Nonnull, HsStablePtr);
10
10
11HsStablePtr globalHandler = 0;
11HsStablePtr globalHandler = 0;
12BOOL global_requestAuthorizationWithOptions = NO;
12BOOL global_requestAuthorizationWithOptions = NO;
13+BOOL global_requestAuthorizationOptionBadge = NO;
14+BOOL global_requestAuthorizationOptionSound = NO;
15+BOOL global_requestAuthorizationOptionAlert = NO;
16+BOOL global_requestAuthorizationOptionCarPlay = NO;
13BOOL global_registerForRemoteNotifications = NO;
17BOOL global_registerForRemoteNotifications = NO;
14HsStablePtr global_didRegisterForRemoteNotificationsWithDeviceToken = 0;
18HsStablePtr global_didRegisterForRemoteNotificationsWithDeviceToken = 0;
15
19
@@ -23,10 +27,22 @@ HsStablePtr global_didRegisterForRemoteNotificationsWithDeviceToken = 0;
23 [self.window makeKeyAndVisible];
27 [self.window makeKeyAndVisible];
24 UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
28 UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
25 if (global_requestAuthorizationWithOptions) {
29 if (global_requestAuthorizationWithOptions) {
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+ }
26− [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge)
43+ [center requestAuthorizationWithOptions:(options)
27 completionHandler:^(BOOL granted, NSError * _Nullable error) {
44 completionHandler:^(BOOL granted, NSError * _Nullable error) {
28 // Handler used to alter application behavior based on types of notifications authorized
45 // Handler used to alter application behavior based on types of notifications authorized
29− // Perhaps we don't need to do anything here.
30 }];
46 }];
31 }
47 }
32 if (global_registerForRemoteNotifications) {
48 if (global_registerForRemoteNotifications) {
@@ -75,11 +91,19 @@ HsStablePtr global_didRegisterForRemoteNotificationsWithDeviceToken = 0;
75void runInWKWebView(HsStablePtr handler,
91void runInWKWebView(HsStablePtr handler,
76 const char * _Nonnull progName,
92 const char * _Nonnull progName,
77 const BOOL hs_requestAuthorizationWithOptions,
93 const BOOL hs_requestAuthorizationWithOptions,
94+ const BOOL hs_requestAuthorizationOptionBadge,
95+ const BOOL hs_requestAuthorizationOptionSound,
96+ const BOOL hs_requestAuthorizationOptionAlert,
97+ const BOOL hs_requestAuthorizationOptionCarPlay,
78 const BOOL hs_registerForRemoteNotifications,
98 const BOOL hs_registerForRemoteNotifications,
79 HsStablePtr hs_didRegisterForRemoteNotificationsWithDeviceToken) {
99 HsStablePtr hs_didRegisterForRemoteNotificationsWithDeviceToken) {
80 @autoreleasepool {
100 @autoreleasepool {
81 globalHandler = handler;
101 globalHandler = handler;
82 global_requestAuthorizationWithOptions = hs_requestAuthorizationWithOptions;
102 global_requestAuthorizationWithOptions = hs_requestAuthorizationWithOptions;
103+ global_requestAuthorizationOptionBadge = hs_requestAuthorizationOptionBadge;
104+ global_requestAuthorizationOptionSound = hs_requestAuthorizationOptionSound;
105+ global_requestAuthorizationOptionAlert = hs_requestAuthorizationOptionAlert;
106+ global_requestAuthorizationOptionCarPlay = hs_requestAuthorizationOptionCarPlay;
83 global_registerForRemoteNotifications = hs_registerForRemoteNotifications;
107 global_registerForRemoteNotifications = hs_registerForRemoteNotifications;
84 global_didRegisterForRemoteNotificationsWithDeviceToken = hs_didRegisterForRemoteNotificationsWithDeviceToken;
108 global_didRegisterForRemoteNotificationsWithDeviceToken = hs_didRegisterForRemoteNotificationsWithDeviceToken;
85 const char * _Nonnull argv [] = {"", 0};
109 const char * _Nonnull argv [] = {"", 0};
jsaddle-wkwebview/jsaddle-wkwebview.cabal+2−1
@@ -31,7 +31,8 @@ library
31 base <5,
31 base <5,
32 bytestring >=0.10.6.0 && <0.11,
32 bytestring >=0.10.6.0 && <0.11,
33 jsaddle >= 0.8.0.0 && <0.9,
33 jsaddle >= 0.8.0.0 && <0.9,
34− data-default
34+ data-default,
35+ containers
35 default-language: Haskell2010
36 default-language: Haskell2010
36 hs-source-dirs: src
37 hs-source-dirs: src
37 frameworks: Foundation, WebKit
38 frameworks: Foundation, WebKit
jsaddle-wkwebview/src-ghc/Language/Javascript/JSaddle/WKWebView.hs+56−9
@@ -5,10 +5,16 @@ module Language.Javascript.JSaddle.WKWebView
5 , run
5 , run
6 , runFile
6 , runFile
7 , mainBundleResourcePath
7 , mainBundleResourcePath
8+ , AppDelegateConfig (..)
9+ , AppDelegateNotificationConfig (..)
10+ , AuthorizationOption (..)
8 ) where
11 ) where
9
12
10import Data.ByteString (ByteString)
13import Data.ByteString (ByteString)
14+import Data.Default (Default, def)
15+import Data.Set (Set)
16+import qualified Data.Set as Set
11−import Language.Javascript.JSaddle.WKWebView.Internal (jsaddleMain, jsaddleMainFile, WKWebView(..), mainBundleResourcePath, AppDelegateConfig (..), boolToCChar)
17+import Language.Javascript.JSaddle.WKWebView.Internal (jsaddleMain, jsaddleMainFile, WKWebView(..), mainBundleResourcePath, boolToCChar)
12import System.Environment (getProgName)
18import System.Environment (getProgName)
13import Foreign.C.String (CString, withCString, peekCString)
19import Foreign.C.String (CString, withCString, peekCString)
14import Foreign.C.Types
20import Foreign.C.Types
@@ -17,18 +23,52 @@ import Language.Javascript.JSaddle (JSM)
17
23
18foreign import ccall runInWKWebView :: StablePtr (WKWebView -> IO ())
24foreign import ccall runInWKWebView :: StablePtr (WKWebView -> IO ())
19 -> CString
25 -> 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
21 -> CChar -- registerForRemoteNotifications
31 -> CChar -- registerForRemoteNotifications
22 -> StablePtr (CString -> IO ()) -- didRegisterForRemoteNotificationsWithDeviceToken
32 -> StablePtr (CString -> IO ()) -- didRegisterForRemoteNotificationsWithDeviceToken
23 -> IO ()
33 -> IO ()
24
34
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+
25-- | Run JSaddle in a WKWebView
65-- | Run JSaddle in a WKWebView
26run :: (CString -> IO ()) -> JSM () -> IO ()
66run :: (CString -> IO ()) -> JSM () -> IO ()
27run didRegisterForRemoteNotificationsWithDeviceToken f = do
67run didRegisterForRemoteNotificationsWithDeviceToken f = do
28 handler <- newStablePtr (jsaddleMain f)
68 handler <- newStablePtr (jsaddleMain f)
29 progName <- getProgName
69 progName <- getProgName
30 deviceTokenHandler <- newStablePtr didRegisterForRemoteNotificationsWithDeviceToken
70 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
32
72
33-- | Run JSaddle in a WKWebView first loading the specified file
73-- | Run JSaddle in a WKWebView first loading the specified file
34-- from the mainBundle (relative to the resourcePath).
74-- from the mainBundle (relative to the resourcePath).
@@ -40,13 +80,20 @@ runFile :: ByteString -- ^ The file to navigate to.
40runFile url allowing cfg f = do
80runFile url allowing cfg f = do
41 handler <- newStablePtr (jsaddleMainFile url allowing f)
81 handler <- newStablePtr (jsaddleMainFile url allowing f)
42 progName <- getProgName
82 progName <- getProgName
83+ let ncfg = _appDelegateConfig_appDelegateNotificationConfig cfg
43− let requestAuthorizationWithOptions = boolToCChar $ _appDelegateConfig_requestAuthorizationWithOptions cfg
84+ (requestAuthorizationWithOptions, authorizationOptions) =
44− registerForRemoteNotifications = boolToCChar $ _appDelegateConfig_registerForRemoteNotifications cfg
85+ case _appDelegateNotificationConfig_requestAuthorizationWithOptions ncfg of
86+ Nothing -> (False, Set.empty)
87+ Just opts -> (True, opts)
88+ registerForRemoteNotifications = _appDelegateNotificationConfig_registerForRemoteNotifications ncfg
45− didRegisterForRemoteNotificationsWithDeviceToken <- newStablePtr $ _appDelegateConfig_didRegisterForRemoteNotificationsWithDeviceToken cfg
89+ didRegisterForRemoteNotificationsWithDeviceToken <- newStablePtr $ _appDelegateNotificationConfig_didRegisterForRemoteNotificationsWithDeviceToken ncfg
46 withCString progName $ \pn -> runInWKWebView
90 withCString progName $ \pn -> runInWKWebView
47 handler
91 handler
48 pn
92 pn
49− requestAuthorizationWithOptions
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)
50− registerForRemoteNotifications
98+ (boolToCChar registerForRemoteNotifications)
51 didRegisterForRemoteNotificationsWithDeviceToken
99 didRegisterForRemoteNotificationsWithDeviceToken
52−
jsaddle-wkwebview/src/Language/Javascript/JSaddle/WKWebView/Internal.hs−15
@@ -4,7 +4,6 @@ module Language.Javascript.JSaddle.WKWebView.Internal
4 , jsaddleMainFile
4 , jsaddleMainFile
5 , WKWebView(..)
5 , WKWebView(..)
6 , mainBundleResourcePath
6 , mainBundleResourcePath
7− , AppDelegateConfig (..)
8 , boolFromCChar
7 , boolFromCChar
9 , boolToCChar
8 , boolToCChar
10 ) where
9 ) where
@@ -13,7 +12,6 @@ import Control.Monad (void, join)
13import Control.Concurrent (forkIO, forkOS)
12import Control.Concurrent (forkIO, forkOS)
14import Control.Concurrent.MVar (newEmptyMVar, putMVar, takeMVar)
13import Control.Concurrent.MVar (newEmptyMVar, putMVar, takeMVar)
15
14
16−import Data.Default
17import Data.Monoid ((<>))
15import Data.Monoid ((<>))
18import Data.ByteString (useAsCString, packCString)
16import Data.ByteString (useAsCString, packCString)
19import qualified Data.ByteString as BS (ByteString)
17import qualified Data.ByteString as BS (ByteString)
@@ -96,19 +94,6 @@ handlerCString c fptr = do
96 f <- deRefStablePtr fptr
94 f <- deRefStablePtr fptr
97 f c
95 f c
98
96
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−
112boolToCChar :: Bool -> CChar
97boolToCChar :: Bool -> CChar
113boolToCChar a = if a then 1 else 0
98boolToCChar a = if a then 1 else 0
114
99