@@ -15,28 +15,38 @@ import platform.UserNotifications.UNNotificationRequest
15
15
import platform.UserNotifications.UNNotificationSound
16
16
import platform.UserNotifications.UNTimeIntervalNotificationTrigger
17
17
import platform.UserNotifications.UNUserNotificationCenter
18
+ import platform.darwin.dispatch_async
19
+ import platform.darwin.dispatch_get_main_queue
18
20
19
21
actual fun Notify (message : String , duration : NotificationDuration ) {
20
- val viewController = UIApplication .sharedApplication.keyWindow?.rootViewController?.modalViewController
21
- val alertController = UIAlertController .alertControllerWithTitle(
22
- title = UIDevice .currentDevice.systemName,
23
- message = message,
24
- preferredStyle = UIAlertControllerStyle .MAX_VALUE
25
- )
26
- alertController.addAction(
27
- UIAlertAction .actionWithTitle(
28
- " OK" ,
29
- style = UIAlertControllerStyle .MAX_VALUE ,
30
- handler = null
22
+ val viewController = UIApplication .sharedApplication.keyWindow?.rootViewController
23
+ if (viewController != null ) {
24
+ val alertController = UIAlertController .alertControllerWithTitle(
25
+ title = UIDevice .currentDevice.systemName,
26
+ message = message,
27
+ preferredStyle = UIAlertControllerStyleAlert
31
28
)
32
- )
33
- viewController?.presentViewController(alertController, animated = true , completion = {
34
- val delay = if (duration == NotificationDuration .LONG ) 3.0 else 1.5
35
- NSTimer .scheduledTimerWithTimeInterval(delay, repeats = false ) {
36
- alertController.dismissViewControllerAnimated(true , completion = null )
29
+ alertController.addAction(
30
+ UIAlertAction .actionWithTitle(
31
+ " OK" ,
32
+ style = UIAlertActionStyleDefault ,
33
+ handler = null
34
+ )
35
+ )
36
+
37
+ dispatch_async(dispatch_get_main_queue()) {
38
+ viewController.presentViewController(alertController, animated = true , completion = {
39
+ val delay = if (duration == NotificationDuration .LONG ) 3.0 else 1.5
40
+ NSTimer .scheduledTimerWithTimeInterval(delay, repeats = false ) {
41
+ alertController.dismissViewControllerAnimated(true , completion = null )
42
+ }
43
+ })
37
44
}
38
- })
45
+ } else {
46
+ NSLog (" Error: rootViewController is null" )
47
+ }
39
48
}
49
+
40
50
actual fun createNotification (type : NotificationType ): Notification = when (type) {
41
51
NotificationType .ALERT -> object : Notification () {
42
52
override fun show (message : String ) {
0 commit comments