Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions android/app/src/main/kotlin/com/zulip/flutter/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
package com.zulip.flutter

import android.content.Intent
import com.zulip.flutter.notifications.NotificationTapEventListener
import com.zulip.flutter.notifications.NotificationTapEventsStreamHandler
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine

class MainActivity : FlutterActivity() {
private var androidIntentEventListener: AndroidIntentEventListener? = null
private var notificationTapEventListener: NotificationTapEventListener? = null

override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)

androidIntentEventListener = AndroidIntentEventListener()
AndroidIntentEventsStreamHandler.register(
flutterEngine.dartExecutor.binaryMessenger,
androidIntentEventListener!!
flutterEngine.dartExecutor.binaryMessenger, androidIntentEventListener!!
)
notificationTapEventListener = NotificationTapEventListener()
NotificationTapEventsStreamHandler.register(
flutterEngine.dartExecutor.binaryMessenger, notificationTapEventListener!!
)

maybeHandleIntent(intent)
}

Expand All @@ -35,6 +42,17 @@ class MainActivity : FlutterActivity() {
return true
}

Intent.ACTION_VIEW -> {
if (notificationTapEventListener!!.maybeHandleViewNotif(intent)) {
// Notification tapped
return true
}

// Let Flutter handle other intents, in particular the web-auth intents
// have ACTION_VIEW, scheme "zulip", and authority "login".
return false
}

// For other intents, let Flutter handle it.
else -> return false
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.zulip.flutter.notifications

import android.content.Intent
import android.net.Uri

class NotificationTapEventListener : NotificationTapEventsStreamHandler() {
private var eventSink: PigeonEventSink<NotificationTapEvent>? = null
private val buffer = mutableListOf<NotificationTapEvent>()

override fun onListen(p0: Any?, sink: PigeonEventSink<NotificationTapEvent>) {
eventSink = sink
if (buffer.isNotEmpty()) {
buffer.forEach { sink.success(it) }
buffer.clear()
}
}

private fun onNotificationTapEvent(dataUrl: Uri) {
val event = AndroidNotificationTapEvent(dataUrl.toString())
if (eventSink != null) {
eventSink!!.success(event)
} else {
buffer.add(event)
}
}

/**
* Recognize if the ACTION_VIEW intent came from tapping a notification; handle it if so
*
* If the intent is recognized, sends a notification tap event via
* the Pigeon event stream to the Dart layer and returns true.
* Else does nothing and returns false.
*
* Do not call if `intent.action` is not ACTION_VIEW.
*/
fun maybeHandleViewNotif(intent: Intent): Boolean {
assert(intent.action == Intent.ACTION_VIEW)

val url = intent.data
if (url?.scheme == "zulip" && url.authority == "notification") {
onNotificationTapEvent(url)
return true
}

return false
}
}
Loading