-
Notifications
You must be signed in to change notification settings - Fork 125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Native Notifications [WIP] #462
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>CFBundleDevelopmentRegion</key> | ||
<string>$(DEVELOPMENT_LANGUAGE)</string> | ||
<key>CFBundleExecutable</key> | ||
<string>$(EXECUTABLE_NAME)</string> | ||
<key>CFBundleIdentifier</key> | ||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> | ||
<key>CFBundleInfoDictionaryVersion</key> | ||
<string>6.0</string> | ||
<key>CFBundleName</key> | ||
<string>$(PRODUCT_NAME)</string> | ||
<key>CFBundlePackageType</key> | ||
<string>APPL</string> | ||
<key>CFBundleShortVersionString</key> | ||
<string>1.0</string> | ||
<key>CFBundleVersion</key> | ||
<string>1</string> | ||
<key>UILaunchStoryboardName</key> | ||
<string></string> | ||
<key>NSHighResolutionCapable</key> | ||
<string>True</string> | ||
</dict> | ||
</plist> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,27 @@ | ||
package org.jetbrains.skiko.sample | ||
|
||
import kotlinx.coroutines.* | ||
import platform.AppKit.* | ||
|
||
import org.jetbrains.skiko.* | ||
import platform.Foundation.NSMakeRect | ||
import platform.Foundation.NSSelectorFromString | ||
import platform.darwin.NSObject | ||
import org.jetbrains.skiko.notifications.Notification | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if notifications deserve their own package. |
||
import platform.Foundation.* | ||
import platform.darwin.* | ||
|
||
fun makeApp(skiaLayer: SkiaLayer) = Clocks(skiaLayer) | ||
fun makeApp(skiaLayer: SkiaLayer) = object : Clocks(skiaLayer) { | ||
override fun onKeyboardEvent(event: SkikoKeyboardEvent) { | ||
super.onKeyboardEvent(event) | ||
if (event.kind == SkikoKeyboardEventKind.DOWN) when (event.key) { | ||
SkikoKey.KEY_N -> runBlocking { | ||
Notification( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, why |
||
title = "Hello", | ||
body = "It works!" | ||
).send() | ||
} | ||
else -> {} | ||
} | ||
} | ||
} | ||
|
||
fun main() { | ||
val app = NSApplication.sharedApplication() | ||
|
@@ -20,7 +34,7 @@ fun main() { | |
appMenuItem.setSubmenu(appMenu) | ||
appMenu.addItemWithTitle("About $appName", NSSelectorFromString("orderFrontStandardAboutPanel:"), "a") | ||
appMenu.addItemWithTitle("Quit $appName", NSSelectorFromString("terminate:"), "q") | ||
|
||
app.delegate = object: NSObject(), NSApplicationDelegateProtocol { | ||
override fun applicationShouldTerminateAfterLastWindowClosed(sender: NSApplication): Boolean { | ||
return true | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.jetbrains.skiko.notifications | ||
|
||
internal actual suspend fun sendNotification(notification: Notification) { | ||
TODO("Not implemented yet") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package org.jetbrains.skiko.notifications | ||
|
||
sealed class NotificationError(override val message: String) : Throwable(message) | ||
|
||
class PermissionNotGrantedError : NotificationError("Permission not granted for notification") | ||
class NotificationsNotSupportedError : NotificationError("Notifications are not supported for this platform") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package org.jetbrains.skiko.notifications | ||
|
||
internal expect suspend fun sendNotification(notification: Notification) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.jetbrains.skiko.notifications | ||
|
||
class Notification(var title: String, var body: String) { | ||
var iconPath: String? = null | ||
|
||
suspend fun send() = sendNotification(this) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.jetbrains.skiko.notifications | ||
|
||
import platform.Foundation.NSError | ||
import platform.Foundation.NSUUID | ||
import platform.UserNotifications.* | ||
import platform.darwin.dispatch_async | ||
import platform.darwin.dispatch_block_t | ||
import platform.darwin.dispatch_get_main_queue | ||
import kotlin.native.concurrent.freeze | ||
|
||
internal actual suspend fun sendNotification(notification: Notification) { | ||
val nc = UNUserNotificationCenter.currentNotificationCenter().freeze() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to freeze here, this is Objective-C object. |
||
val sendNotification: dispatch_block_t = { | ||
val callback = { granted: Boolean, err: NSError? -> | ||
if (granted) { | ||
val content = UNMutableNotificationContent().apply { | ||
setBody(notification.body) | ||
setTitle(notification.title) | ||
} | ||
val id = NSUUID().UUIDString | ||
val request = UNNotificationRequest.requestWithIdentifier(id, content, null) | ||
val callback = { _: NSError? -> } | ||
nc.addNotificationRequest(request, callback.freeze()) | ||
} | ||
} | ||
val options = UNAuthorizationOptionAlert or UNAuthorizationOptionSound or UNAuthorizationOptionBadge | ||
nc.requestAuthorizationWithOptions(options, callback.freeze()) | ||
} | ||
dispatch_async(dispatch_get_main_queue(), sendNotification.freeze()) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.jetbrains.skiko.notifications | ||
|
||
import kotlinx.browser.window | ||
import kotlinx.coroutines.await | ||
import org.w3c.notifications.DENIED | ||
import org.w3c.notifications.GRANTED | ||
import org.w3c.notifications.NotificationOptions | ||
import org.w3c.notifications.NotificationPermission | ||
import org.w3c.notifications.Notification as WebNotification | ||
|
||
internal actual suspend fun sendNotification(notification: Notification) { | ||
if (window.asDynamic()["Notification"] == undefined) { | ||
throw NotificationsNotSupportedError() | ||
} | ||
|
||
val permission = when (WebNotification.permission) { | ||
NotificationPermission.GRANTED -> NotificationPermission.GRANTED | ||
NotificationPermission.DENIED -> NotificationPermission.DENIED | ||
else -> WebNotification.requestPermission().await() | ||
} | ||
|
||
if (permission == NotificationPermission.GRANTED) { | ||
WebNotification(notification.title, NotificationOptions( | ||
body = notification.body | ||
)) | ||
} else { | ||
throw PermissionNotGrantedError() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.jetbrains.skiko.notifications | ||
|
||
internal actual suspend fun sendNotification(notification: Notification) { | ||
TODO("Not implemented yet") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Temporarily opened to add custom key binding for macOS