From ebe3ab4505f6d585148b5a42f7ab9f543675df59 Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Mon, 25 Oct 2021 20:25:54 -0400 Subject: [PATCH] Working notification --- .../main/java/io/heckel/ntfy/data/NtfyApi.kt | 6 +- .../io/heckel/ntfy/list/TopicsListActivity.kt | 55 +++++++++++++++--- app/src/main/res/drawable/ntfy.png | Bin 0 -> 1260 bytes app/src/main/res/values/strings.xml | 2 + 4 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 app/src/main/res/drawable/ntfy.png diff --git a/app/src/main/java/io/heckel/ntfy/data/NtfyApi.kt b/app/src/main/java/io/heckel/ntfy/data/NtfyApi.kt index a0f9108..36568c6 100644 --- a/app/src/main/java/io/heckel/ntfy/data/NtfyApi.kt +++ b/app/src/main/java/io/heckel/ntfy/data/NtfyApi.kt @@ -26,11 +26,15 @@ class NtfyApi(context: Context) { fun getEventsFlow(): Flow = flow { coroutineScope { - val conn = getStreamConnection("https://ntfy.sh/_phil") + println("111111111111") + + val conn = getStreamConnection("https://ntfy.sh/_phil/sse") + println("2222222222222") val input = conn.inputStream.bufferedReader() try { conn.connect() var event = Event() + println("CCCCCCCCCCCCCCc") while (isActive) { val line = input.readLine() println("PHIL: " + line) diff --git a/app/src/main/java/io/heckel/ntfy/list/TopicsListActivity.kt b/app/src/main/java/io/heckel/ntfy/list/TopicsListActivity.kt index 3234bfe..9113c56 100644 --- a/app/src/main/java/io/heckel/ntfy/list/TopicsListActivity.kt +++ b/app/src/main/java/io/heckel/ntfy/list/TopicsListActivity.kt @@ -17,12 +17,17 @@ package io.heckel.ntfy.list import android.app.Activity +import android.app.NotificationChannel +import android.app.NotificationManager +import android.content.Context import android.content.Intent +import android.os.Build import android.os.Bundle import android.view.View import androidx.activity.viewModels import androidx.appcompat.app.AppCompatActivity -import androidx.lifecycle.Observer +import androidx.core.app.NotificationCompat +import androidx.core.app.NotificationManagerCompat import androidx.lifecycle.asLiveData import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.RecyclerView @@ -35,6 +40,7 @@ import io.heckel.ntfy.detail.TopicDetailActivity import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +import kotlin.random.Random const val TOPIC_ID = "topic id" @@ -64,18 +70,32 @@ class TopicsListActivity : AppCompatActivity() { fabOnClick() } - val self = this - api.getEventsFlow().asLiveData(Dispatchers.IO).observe(this, Observer { event -> - // Get the Activity's lifecycleScope and launch + createNotificationChannel() + + api.getEventsFlow().asLiveData(Dispatchers.IO).observe(this) { event -> this.lifecycleScope.launch(Dispatchers.Main) { - // run the code again in IO context withContext(Dispatchers.IO) { - println(event.data) - //Toast.makeText(self, event.data, Toast.LENGTH_SHORT) + handleEvent(event) } } } - ) + } + + private fun handleEvent(event: NtfyApi.Event) { + if (event.data.isJsonNull || !event.data.has("message")) { + return + } + println("PHIL EVENT: " + event.data) + val channelId = getString(R.string.notification_channel_id) + val notification = NotificationCompat.Builder(this, channelId) + .setSmallIcon(R.drawable.ntfy) + .setContentTitle("ntfy") + .setContentText(event.data.get("message").asString) + .setPriority(NotificationCompat.PRIORITY_DEFAULT) + .build() + with(NotificationManagerCompat.from(this)) { + notify(Random.nextInt(), notification) + } } /* Opens TopicDetailActivity when RecyclerView item is clicked. */ @@ -102,4 +122,23 @@ class TopicsListActivity : AppCompatActivity() { } } } + + private fun createNotificationChannel() { + // Create the NotificationChannel, but only on API 26+ because + // the NotificationChannel class is new and not in the support library + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val channelId = getString(R.string.notification_channel_id) + val name = getString(R.string.notification_channel_name) + val descriptionText = getString(R.string.notification_channel_name) + val importance = NotificationManager.IMPORTANCE_DEFAULT + val channel = NotificationChannel(channelId, name, importance).apply { + description = descriptionText + } + // Register the channel with the system + val notificationManager: NotificationManager = + getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + notificationManager.createNotificationChannel(channel) + } + } + } diff --git a/app/src/main/res/drawable/ntfy.png b/app/src/main/res/drawable/ntfy.png new file mode 100644 index 0000000000000000000000000000000000000000..93982c6f1098797152e5aeef37eafaf978dd9ab5 GIT binary patch literal 1260 zcmVWFU8GbZ8()Nlj2>E@cM*00dA;L_t(|+U?wLOdDk! z2k`H;g$W4ht&2-6o1)EJ}VjhdkG#a!Z_7mLRE=gmw^l*D(# zsEIGOII}kzjV55`VnjM4CAcAfI!A@J8#JX{DZT66`D#K%<9h8{uKj)AwU>K-&wcVd z&-I?BWxVK9Kv5kX5fC5&5+DIm2#^2?kN_zJNPq-LfD{5OVhmfogH=WY_V{+9qpJn3 zRwtbI*l}B}1!j(gEGt+`rm+}~V&&j;}sGk24>9jpaqYvZiq3HXGhKa}=Ki z-bHW!!JP2`z-G7N>DL~?7r_BMe)JH)R0v>CzYnJ`zlZK8y#;B{!kO^OhtJ^TxtG9N zs>^E)wBJ>WW2bun0MXevcJA1X2mAKll3EWux)%fA9mlcmlMqs3d5u7W!GOp54&O4o z8)@6;!TaC73P33zK)qfMpU;PFJDQ79Z_oX$c>VKdVKC~o52(}W(An9E`uh5!*6r>+ zfT!L*07G?QF1=)HU|n5ZN$MJnM)-R6VJVWpHv_-WlZcwQ-EMfi&1mUz5}?{q=x{i& z|Hv*FYpMuPO`J|A*qUl|9Cnx2vv4MpK{y=7+}s@E@i0L z-gO!p8lWf&c6Z;2pU+J}5@hWIMx#-Tj*cRkOl~^H^E`N-$IQ$OJRT3)+uNbntN%E{ zvMe}`1JCeqb=Wce(}K3A@a*g?hKGm$6)$V|gJCc=HHERUvAkipjn$P@K>&@WDs2Hul7vJe z@%QWC#VEuzDd%>CbfzHv(N!_p0{rKl_i7-j?$J*y44_`G(-xp8X0LVE|467C$)Z%4 z4ypX+Zc6}485yzp6;Vd2fCNZ@1V|x30wh2Jq!1th5+DIm2#^2?kN_zJSe!70`v1VL=i1QPwO!S@ z;prI8p7=5EK0N&LUbydVDrz5S1kw~PlPOFikV1e2NPq-LAwU8oKmw!?AOR9!W#lhP WZetnY50iZW0000fab Unsubscribe + Ntfy + ntfy