Skip to content

Commit ccf4aa2

Browse files
authored
Add Pusher beams and code that enables push notifications handling (#68)
1 parent 706c3fb commit ccf4aa2

9 files changed

Lines changed: 55 additions & 6 deletions

File tree

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,7 @@ lint/tmp/
8181
# lint/reports/
8282

8383
# Detekt Reports
84-
reports/
84+
reports/
85+
86+
# Firebase config
87+
app/google-services.json

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ This is the official repository for the [dev.to](https://dev.to/)'s Android app.
99

1010
## Design ethos
1111

12-
DEV Android is an [WebView](https://developer.android.com/guide/webapps/webview) based application. This application is inspired by [Basecamp's approach](https://m.signalvnoise.com/basecamp-3-for-ios-hybrid-architecture-afc071589c25). We will grow to include more native code over time.
12+
DEV Android is an [WebView](https://developer.android.com/guide/webapps/webview) based application. This application is inspired by [Basecamp's approach](https://m.signalvnoise.com/basecamp-3-for-ios-hybrid-architecture-afc071589c25). We will grow to include more native code over time.
1313

14-
By leveraging webviews as much as possible, we can smoothly sync up with our web dev work. And where it makes sense, we can re-implement certain things fully native, or build entirely native features. Life's a journey, not a destination.
14+
By leveraging webviews as much as possible, we can smoothly sync up with our web dev work. And where it makes sense, we can re-implement certain things fully native, or build entirely native features. Life's a journey, not a destination.
1515

1616
## Contributions
1717

@@ -43,6 +43,11 @@ To see more detail about a task, run gradlew help --task <task>
4343

4444
```
4545

46+
### Push Notifications
47+
48+
For Push Notification delivery we use [Pusher Beams](https://pusher.com/beams). In order to get the app running locally you'll need a `google-services.json` configuration file from Firebase, otherwise you'll encounter the following error: `File google-services.json is missing. The Google Services Plugin cannot function without it.`
49+
50+
You can [sign up or sign in on Firebase](https://firebase.google.com/) account for free in order to get the app working locally. Steps 1-4 under **Firebase for Android Push Notifications** in our [official docs](https://docs.dev.to/backend/pusher/#mobile-push-notifications) show how to set this up in more detail. Drop the resulting `google-services.json` file in the `app` folder and you'll be good to go.
4651

4752
### How to contribute
4853

app/build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ dependencies {
3030
implementation(Libs.kotlin_stdlib_jdk8)
3131
implementation(Libs.browser)
3232

33+
implementation("com.google.firebase:firebase-messaging:18.0.0")
34+
implementation("com.pusher:push-notifications-android:1.6.2")
35+
3336
testImplementation(Libs.junit)
3437
androidTestImplementation(Libs.androidx_test_runner)
3538
androidTestImplementation(Libs.espresso_core)
3639
}
40+
41+
apply(plugin = "com.google.gms.google-services")

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
android:roundIcon="@mipmap/ic_launcher_round"
1414
android:supportsRtl="true"
1515
android:theme="@style/AppTheme"
16-
android:usesCleartextTraffic="true"
17-
tools:ignore="GoogleAppIndexingWarning,UnusedAttribute">
16+
tools:ignore="GoogleAppIndexingWarning,UnusedAttribute"
17+
tools:replace="android:fullBackupContent"
18+
>
1819
<activity
1920
android:name=".view.main.view.MainActivity"
2021
android:screenOrientation="portrait">

app/src/main/java/to/dev/dev_android/view/main/view/CustomWebViewClient.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import android.webkit.CookieManager
99
import android.webkit.WebView
1010
import android.webkit.WebViewClient
1111
import androidx.browser.customtabs.CustomTabsIntent
12+
import com.pusher.pushnotifications.PushNotifications
13+
import java.lang.Exception
1214

1315
class CustomWebViewClient(
1416
private val context: Context,
@@ -23,12 +25,32 @@ class CustomWebViewClient(
2325
"github.com/sessions/"
2426
)
2527

28+
private var registeredUserNotifications = false
29+
2630
override fun onPageFinished(view: WebView, url: String?) {
2731
onPageFinish()
2832
view.visibility = View.VISIBLE
2933
super.onPageFinished(view, url)
3034
}
3135

36+
override fun doUpdateVisitedHistory(view: WebView?, url: String?, isReload: Boolean) {
37+
val javascript = "JSON.parse(document.getElementsByTagName('body')[0].getAttribute('data-user')).id"
38+
view?.evaluateJavascript(javascript) { result ->
39+
if (result != "null" && !registeredUserNotifications) {
40+
try {
41+
val userId = result.toString().toInt()
42+
PushNotifications.addDeviceInterest("user-notifications-$userId")
43+
registeredUserNotifications = true
44+
}
45+
catch (e: Exception) {
46+
println(e)
47+
}
48+
}
49+
}
50+
51+
super.doUpdateVisitedHistory(view, url, isReload)
52+
}
53+
3254
override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
3355
if (view.originalUrl == "https://dev.to/signout_confirm" && url == "https://dev.to/") {
3456
view.clearCache(true)

app/src/main/java/to/dev/dev_android/view/main/view/MainActivity.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import android.view.View
1010
import android.webkit.ValueCallback
1111
import android.webkit.WebView
1212
import to.dev.dev_android.R
13+
import com.pusher.pushnotifications.PushNotifications
1314
import to.dev.dev_android.base.BuildConfig
1415
import to.dev.dev_android.base.activity.BaseActivity
1516
import to.dev.dev_android.databinding.ActivityMainBinding
@@ -29,6 +30,15 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), CustomWebChromeClient.
2930
setWebViewSettings()
3031
savedInstanceState?.let { restoreState(it) } ?: navigateToHome()
3132
handleIntent(intent)
33+
PushNotifications.start(getApplicationContext(), BuildConfig.pusherInstanceId);
34+
PushNotifications.addDeviceInterest("broadcast");
35+
}
36+
37+
override fun onResume() {
38+
if (intent.extras != null && intent.extras["url"] != null) {
39+
binding.webView.loadUrl(intent.extras["url"].toString())
40+
}
41+
super.onResume()
3242
}
3343

3444
override fun onSaveInstanceState(outState: Bundle) {

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ buildscript {
1010
val agpVersion = findProperty("version.com.android.tools.build..gradle") as String
1111
classpath("com.android.tools.build:gradle:$agpVersion")
1212
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
13-
13+
classpath("com.google.gms:google-services:4.2.0")
1414
// NOTE: Do not place your application dependencies here; they belong
1515
// in the individual module build.gradle files
1616
}

buildSrc/src/main/kotlin/Android.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ fun Project.configureBuildConfig(android: BaseExtension) {
4040
android.buildTypes.all {
4141
resValue("string", "baseUrl", extra.string("chrome.baseUrl"))
4242
buildConfigString("baseUrl", extra.string("chrome.baseUrl"))
43+
resValue("string", "pusherInstanceId", extra.string("pusher.instanceId"))
44+
buildConfigString("pusherInstanceId", extra.string("pusher.instanceId"))
4345
resValue("string", "userAgent", extra.string("chrome.userAgent"))
4446
buildConfigString("userAgent", extra.string("chrome.userAgent"))
4547
}

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## dev.to configurations
22
chrome.baseUrl=https://dev.to/
33
chrome.userAgent=DEV-Native-android
4+
pusher.instanceId=cdaf9857-fad0-4bfb-b360-64c1b2693ef3
45

56
# Android release configurations
67
android.targetSdkVersion=28

0 commit comments

Comments
 (0)