Skip to content

Commit cab2167

Browse files
committed
android [nfc]: Move notif intent-handling function into notifications/
1 parent 5fc958b commit cab2167

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

android/app/src/main/java/com/zulipmobile/MainActivity.kt

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ import android.os.Bundle
66
import android.webkit.WebView
77
import com.facebook.react.ReactActivity
88
import com.facebook.react.ReactActivityDelegate
9-
import com.zulipmobile.notifications.EXTRA_NOTIFICATION_DATA
10-
import com.zulipmobile.notifications.NOTIFICATION_URL_AUTHORITY
11-
import com.zulipmobile.notifications.logNotificationData
12-
import com.zulipmobile.notifications.notifyReact
9+
import com.zulipmobile.notifications.maybeHandleViewNotif
1310
import com.zulipmobile.sharing.handleSend
1411
import expo.modules.ReactActivityDelegateWrapper
1512

@@ -32,26 +29,22 @@ open class MainActivity : ReactActivity() {
3229
/* Returns true just if we did handle the intent. */
3330
private fun maybeHandleIntent(intent: Intent?): Boolean {
3431
intent ?: return false
35-
val url = intent.data
3632
when (intent.action) {
3733
// Share-to-Zulip
3834
Intent.ACTION_SEND, Intent.ACTION_SEND_MULTIPLE -> {
3935
handleSend(intent, reactApplication.tryGetReactContext(), contentResolver)
4036
return true
4137
}
4238

43-
Intent.ACTION_VIEW -> when {
44-
// Launch MainActivity on tapping a notification
45-
url?.scheme == "zulip" && url.authority == NOTIFICATION_URL_AUTHORITY -> {
46-
val data = intent.getBundleExtra(EXTRA_NOTIFICATION_DATA) ?: return false
47-
logNotificationData("notif opened", data)
48-
notifyReact(reactApplication.tryGetReactContext(), data)
39+
Intent.ACTION_VIEW -> {
40+
if (maybeHandleViewNotif(intent, reactApplication.tryGetReactContext())) {
41+
// Notification tapped
4942
return true
5043
}
5144

5245
// Let RN handle other intents. In particular web-auth intents (parsed in
5346
// src/start/webAuth.js) have ACTION_VIEW, scheme "zulip", and authority "login".
54-
else -> return false
47+
return false
5548
}
5649

5750
// For other intents, let RN handle it.

android/app/src/main/java/com/zulipmobile/notifications/NotifyReact.kt

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
package com.zulipmobile.notifications
44

5+
import android.content.Intent
56
import android.os.Bundle
67
import android.util.Log
78
import com.facebook.react.bridge.Arguments
89
import com.facebook.react.bridge.ReactContext
9-
import com.zulipmobile.ReactAppStatus
10-
import com.zulipmobile.appStatus
11-
import com.zulipmobile.emitEvent
10+
import com.zulipmobile.*
1211

1312
/**
1413
* Methods for telling React about a notification.
@@ -17,6 +16,31 @@ import com.zulipmobile.emitEvent
1716
* TODO: Replace this with a fresh implementation based on RN upstream docs.
1817
*/
1918

19+
/**
20+
* Recognize if an ACTION_VIEW intent came from tapping a notification; handle it if so
21+
*
22+
* Just if the intent is recognized,
23+
* sends a 'notificationOpened' event to the JavaScript layer
24+
* and returns true.
25+
* Else does nothing and returns false.
26+
*
27+
* Do not call if `intent.action` is not ACTION_VIEW.
28+
*/
29+
internal fun maybeHandleViewNotif(intent: Intent, maybeReactContext: ReactContext?): Boolean {
30+
assert(intent.action == Intent.ACTION_VIEW)
31+
32+
val url = intent.data
33+
// Launch MainActivity on tapping a notification
34+
if (url?.scheme == "zulip" && url.authority == NOTIFICATION_URL_AUTHORITY) {
35+
val data = intent.getBundleExtra(EXTRA_NOTIFICATION_DATA) ?: return false
36+
logNotificationData("notif opened", data)
37+
notifyReact(maybeReactContext, data)
38+
return true
39+
}
40+
41+
return false
42+
}
43+
2044
internal fun notifyReact(reactContext: ReactContext?, data: Bundle) {
2145
// TODO deduplicate this with handleSend in SharingHelper.kt; see
2246
// https://github.com/zulip/zulip-mobile/pull/5146#discussion_r764437055

0 commit comments

Comments
 (0)