on master help sign in
Preliminary support for push notifications
commite22e06e2adf82160b13406d24fff8199c8d5ae99 authorAli Abrar <aliabrar@gmail.com> dateThu 4 May 2017, 21:20 (9 years ago) parentdbd6394
4 files changed+45−12
jsaddle-wkwebview/cbits-uikit/AppDelegate.m+18−2 jsaddle-wkwebview/jsaddle-wkwebview.cabal+12−6 jsaddle-wkwebview/src-ghc/Language/Javascript/JSaddle/WKWebView.hs+7−4 jsaddle-wkwebview/src/Language/Javascript/JSaddle/WKWebView/Internal.hs+8
jsaddle-wkwebview/cbits-uikit/AppDelegate.m+18−2
@@ -1,12 +1,15 @@
11#import "AppDelegate.h"
22#import "ViewController.h"
3+#import <UserNotifications/UserNotifications.h>
34
4−@interface AppDelegate ()
5+extern void didRegisterForRemoteNotificationsWithDeviceTokenCallback(HsStablePtr, const char * _Nonnull);
56
7+@interface AppDelegate ()
68
79@end
810
911HsStablePtr globalHandler = 0;
12+HsStablePtr globalDeviceTokenHook = 0;
1013
1114@implementation AppDelegate
1215
@@ -16,6 +19,13 @@ HsStablePtr globalHandler = 0;
1619 self.window.rootViewController = [[ViewController alloc] initWithHandler:globalHandler];
1720 self.window.backgroundColor = [UIColor whiteColor];
1821 [self.window makeKeyAndVisible];
22+ UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
23+ [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge)
24+ completionHandler:^(BOOL granted, NSError * _Nullable error) {
25+ // Handler used to alter application behavior based on types of notifications authorized
26+ // Perhaps we don't need to do anything here.
27+ }];
28+ [application registerForRemoteNotifications];
1929 return YES;
2030}
2131
@@ -45,11 +55,17 @@ HsStablePtr globalHandler = 0;
4555 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
4656}
4757
58+- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
59+ NSString *deviceTokenString = [deviceToken base64EncodedStringWithOptions: 0];
60+ didRegisterForRemoteNotificationsWithDeviceTokenCallback(globalDeviceTokenHook, [deviceTokenString UTF8String]);
61+}
62+
4863@end
4964
50−void runInWKWebView(HsStablePtr handler, const char * _Nonnull progName) {
65+void runInWKWebView(HsStablePtr handler, const char * _Nonnull progName, HsStablePtr deviceTokenHook) {
5166 @autoreleasepool {
5267 globalHandler = handler;
68+ globalDeviceTokenHook = deviceTokenHook;
5369 const char * _Nonnull argv [] = {"", 0};
5470 UIApplicationMain(0, argv, nil, NSStringFromClass([AppDelegate class]));
5571 }
jsaddle-wkwebview/jsaddle-wkwebview.cabal+12−6
@@ -14,6 +14,10 @@ description:
1414category: Web, Javascript
1515author: Hamish Mackenzie
1616
17+flag include-app-delegate
18+ description: Include default AppDelegate C sources.
19+ default: True
20+
1721source-repository head
1822 type: git
1923 location: https://github.com/ghcjs/jsaddle
@@ -38,11 +42,13 @@ library
3842 else
3943 hs-source-dirs: src-ghc
4044 if os(ios)
41− frameworks: UIKit
42− c-sources:
43− cbits-uikit/AppDelegate.m
44− cbits-uikit/ViewController.m
45+ frameworks: UIKit, UserNotifications
46+ if flag(include-app-delegate)
47+ c-sources:
48+ cbits-uikit/AppDelegate.m
49+ cbits-uikit/ViewController.m
4550 else
4651 frameworks: Cocoa
47− c-sources:
48− cbits-cocoa/AppDelegate.m
52+ if flag(include-app-delegate)
53+ c-sources:
54+ cbits-cocoa/AppDelegate.m
jsaddle-wkwebview/src-ghc/Language/Javascript/JSaddle/WKWebView.hs+7−4
@@ -10,18 +10,19 @@ module Language.Javascript.JSaddle.WKWebView
1010import Data.ByteString (ByteString)
1111import Language.Javascript.JSaddle.WKWebView.Internal (jsaddleMain, jsaddleMainFile, WKWebView(..), mainBundleResourcePath)
1212import System.Environment (getProgName)
13−import Foreign.C.String (CString, withCString)
13+import Foreign.C.String (CString, withCString, peekCString)
1414import Foreign.StablePtr (StablePtr, newStablePtr)
1515import Language.Javascript.JSaddle (JSM)
1616
17−foreign import ccall runInWKWebView :: StablePtr (WKWebView -> IO ()) -> CString -> IO ()
17+foreign import ccall runInWKWebView :: StablePtr (WKWebView -> IO ()) -> CString -> StablePtr (CString -> IO ()) -> IO ()
1818
1919-- | Run JSaddle in a WKWebView
2020run :: JSM () -> IO ()
2121run f = do
2222 handler <- newStablePtr (jsaddleMain f)
2323 progName <- getProgName
24− withCString progName $ runInWKWebView handler
24+ deviceTokenHandler <- newStablePtr (\s -> putStrLn =<< peekCString s)
25+ withCString progName $ \pn -> runInWKWebView handler pn deviceTokenHandler
2526
2627-- | Run JSaddle in a WKWebView first loading the specified file
2728-- from the mainBundle (relative to the resourcePath).
@@ -31,4 +32,6 @@ runFile :: ByteString -- ^ The file to navigate to.
3132runFile url allowing f = do
3233 handler <- newStablePtr (jsaddleMainFile url allowing f)
3334 progName <- getProgName
34− withCString progName $ runInWKWebView handler
35+ deviceTokenHandler <- newStablePtr (\s -> putStrLn =<< peekCString s)
36+ withCString progName $ \pn -> runInWKWebView handler pn deviceTokenHandler
37+
jsaddle-wkwebview/src/Language/Javascript/JSaddle/WKWebView/Internal.hs+8
@@ -29,6 +29,7 @@ newtype WKWebView = WKWebView (Ptr WKWebView)
2929foreign export ccall jsaddleStart :: StablePtr (IO ()) -> IO ()
3030foreign export ccall jsaddleResult :: StablePtr (Results -> IO ()) -> CString -> IO ()
3131foreign export ccall withWebView :: WKWebView -> StablePtr (WKWebView -> IO ()) -> IO ()
32+foreign export ccall didRegisterForRemoteNotificationsWithDeviceTokenCallback :: StablePtr (CString -> IO ()) -> CString -> IO ()
3233foreign import ccall addJSaddleHandler :: WKWebView -> StablePtr (IO ()) -> StablePtr (Results -> IO ()) -> IO ()
3334foreign import ccall loadHTMLString :: WKWebView -> CString -> IO ()
3435foreign import ccall loadBundleFile :: WKWebView -> CString -> CString -> IO ()
@@ -85,6 +86,12 @@ withWebView webView ptrF = do
8586 f <- deRefStablePtr ptrF
8687 f webView
8788
89+-- | A base64 encoded device token
90+didRegisterForRemoteNotificationsWithDeviceTokenCallback :: StablePtr (CString -> IO ()) -> CString -> IO ()
91+didRegisterForRemoteNotificationsWithDeviceTokenCallback f token = do
92+ f' <- deRefStablePtr f
93+ f' token
94+
8895jsaddleJs :: ByteString
8996jsaddleJs = ghcjsHelpers <> "\
9097 \runJSaddleBatch = (function() {\n\
@@ -112,3 +119,4 @@ mainBundleResourcePath = do
112119 if bs == nullPtr
113120 then return Nothing
114121 else Just <$> packCString bs
122+