Add warning notification when disconnected

This commit is contained in:
S1m 2021-11-23 09:15:28 +01:00
parent 391382dd51
commit 77f7901024
5 changed files with 120 additions and 58 deletions

View File

@ -31,6 +31,7 @@ import org.unifiedpush.distributor.nextpush.account.ssoAccount
import org.unifiedpush.distributor.nextpush.api.ApiUtils
import org.unifiedpush.distributor.nextpush.distributor.sendUnregistered
import org.unifiedpush.distributor.nextpush.distributor.getDb
import org.unifiedpush.distributor.nextpush.services.startListener
import java.lang.String.format
private const val TAG = "NextPush-MainActivity"
@ -100,7 +101,7 @@ class MainActivity : AppCompatActivity() {
format(getString(R.string.main_account_desc), ssoAccount.name)
showLogout = true
invalidateOptionsMenu()
startListener()
startListener(this)
}
private fun showStart() {
@ -144,7 +145,7 @@ class MainActivity : AppCompatActivity() {
Log.d(TAG, "Restarting the Listener")
val serviceIntent = Intent(this, StartService::class.java)
this.stopService(serviceIntent)
startListener()
startListener(this)
}
private fun logout() {
@ -168,16 +169,6 @@ class MainActivity : AppCompatActivity() {
alert.show()
}
private fun startListener(){
Log.d(TAG, "Starting the Listener")
val serviceIntent = Intent(this, StartService::class.java)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
this.startForegroundService(serviceIntent)
}else{
this.startService(serviceIntent)
}
}
private fun setListView(){
listView = findViewById<ListView>(R.id.applications_list)
val db = getDb(this)

View File

@ -0,0 +1,98 @@
package org.unifiedpush.distributor.nextpush.services
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import androidx.core.app.NotificationManagerCompat
import org.unifiedpush.distributor.nextpush.R
import android.app.PendingIntent
import android.content.Intent
import org.unifiedpush.distributor.nextpush.activities.MainActivity
const val NOTIF_ID_FOREGROUND = 51115
const val NOTIF_ID_WARNING = 51215
fun createForegroundNotification(context: Context): Notification {
val appName = context.getString(R.string.app_name)
val notificationChannelId = "$appName.Listener"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager;
val channel = NotificationChannel(
notificationChannelId,
appName,
NotificationManager.IMPORTANCE_LOW
).let {
it.description = context.getString(R.string.listening_notif_description)
it
}
notificationManager.createNotificationChannel(channel)
}
val builder: Notification.Builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) Notification.Builder(
context,
notificationChannelId
) else Notification.Builder(context)
return builder
.setContentTitle(context.getString(R.string.app_name))
.setContentText(context.getString(R.string.listening_notif_description))
.setSmallIcon(R.drawable.ic_launcher_notification)
.setTicker(context.getString(R.string.listening_notif_ticker))
.setPriority(Notification.PRIORITY_LOW) // for under android 26 compatibility
.build()
}
fun createWarningNotification(context: Context) {
val appName = context.getString(R.string.app_name)
val notificationChannelId = "$appName.Warning"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager;
val channel = NotificationChannel(
notificationChannelId,
appName,
NotificationManager.IMPORTANCE_HIGH
).let {
it.description = context.getString(R.string.warning_notif_description)
it
}
notificationManager.createNotificationChannel(channel)
}
val notificationIntent: Intent = Intent(context, MainActivity::class.java)
notificationIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
val intent = PendingIntent.getActivity(
context, 0,
notificationIntent, 0
)
val builder: Notification.Builder = (
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) Notification.Builder(
context,
notificationChannelId
)
else Notification.Builder(context)
).setContentTitle(context.getString(R.string.app_name))
.setContentText(context.getString(R.string.warning_notif_description))
.setSmallIcon(R.drawable.ic_launcher_notification)
.setTicker(context.getString(R.string.warning_notif_ticker))
.setPriority(Notification.PRIORITY_HIGH) // for under android 26 compatibility
.setContentIntent(intent)
with(NotificationManagerCompat.from(context)) {
notify(NOTIF_ID_WARNING, builder.build())
}
}
fun deleteWarningNotification(context: Context) {
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager;
notificationManager.cancel(NOTIF_ID_WARNING)
}

View File

@ -14,11 +14,8 @@ import org.unifiedpush.distributor.nextpush.distributor.sendMessage
private const val TAG = "SSEListener"
class SSEListener (val context: Context) : EventSourceListener() {
private var pingtime = 0.toLong()
private val networkConnected = false
override fun onOpen(eventSource: EventSource, response: Response) {
pingtime = System.currentTimeMillis()
deleteWarningNotification(context)
try {
Log.d(TAG, "onOpen: " + response.code)
} catch (e: Exception) {
@ -28,7 +25,6 @@ class SSEListener (val context: Context) : EventSourceListener() {
override fun onEvent(eventSource: EventSource, id: String?, eventType: String?, data: String) {
Log.d(TAG, "New SSE message event=$eventType message=$data")
pingtime = System.currentTimeMillis()
if (eventType == "warning") {
Log.d(TAG, "Warning event received.")
// Notification warning
@ -49,18 +45,17 @@ class SSEListener (val context: Context) : EventSourceListener() {
override fun onClosed(eventSource: EventSource) {
Log.d(TAG, "onClosed: $eventSource")
if (!networkConnected) return
startListener(context)
}
override fun onFailure(eventSource: EventSource, t: Throwable?, response: Response?) {
createWarningNotification(context)
t?.let {
Log.d(TAG, "An error occurred: $t")
return
}
response?.let {
Log.d(TAG, "onFailure: ${it.code}")
if (!networkConnected) return
return
}
}
}

View File

@ -1,8 +1,5 @@
package org.unifiedpush.distributor.nextpush.services
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Context
import android.content.Intent
@ -16,12 +13,21 @@ import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException
import com.nextcloud.android.sso.helper.SingleAccountHelper
import com.nextcloud.android.sso.ui.UiExceptionManager
import org.unifiedpush.distributor.nextpush.R
import org.unifiedpush.distributor.nextpush.api.ApiUtils
import org.unifiedpush.distributor.nextpush.account.ssoAccount
private const val TAG = "StartService"
fun startListener(context: Context){
Log.d(TAG, "Starting the Listener")
val serviceIntent = Intent(context, StartService::class.java)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(serviceIntent)
}else{
context.startService(serviceIntent)
}
}
class StartService: Service(){
private var isServiceStarted = false
@ -35,8 +41,8 @@ class StartService: Service(){
override fun onCreate(){
super.onCreate()
Log.i(TAG,"Starting")
val notification = createNotification()
startForeground(51115, notification)
val notification = createForegroundNotification(this)
startForeground(NOTIF_ID_FOREGROUND, notification)
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
@ -50,37 +56,6 @@ class StartService: Service(){
super.onDestroy()
}
private fun createNotification(): Notification {
val appName = getString(R.string.app_name)
val notificationChannelId = "$appName.Listener"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager;
val channel = NotificationChannel(
notificationChannelId,
appName,
NotificationManager.IMPORTANCE_LOW
).let {
it.description = getString(R.string.listening_notif_description)
it
}
notificationManager.createNotificationChannel(channel)
}
val builder: Notification.Builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) Notification.Builder(
this,
notificationChannelId
) else Notification.Builder(this)
return builder
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.listening_notif_description))
.setSmallIcon(R.drawable.ic_launcher_notification)
.setTicker("Listening")
.setPriority(Notification.PRIORITY_LOW) // for under android 26 compatibility
.build()
}
private fun startService() {
if (isServiceStarted) return
isServiceStarted = true

View File

@ -5,7 +5,7 @@
<string name="main_applications_title">Registered applications</string>
<string name="help"></string>
<string name="listening_notif_description">Listening incoming notifications</string>
<string name="listening_notif_description">Listening for incoming notifications</string>
<string name="connection_button">Login</string>
<string name="connection_description">You are not connected to Nextcloud yet</string>
<string name="action_logout">Logout</string>
@ -16,4 +16,7 @@
<string name="main_account_title">Account</string>
<string name="main_account_desc">You are connected as: %s</string>
<string name="action_restart">Restart Service</string>
<string name="warning_notif_description">NextPush is disconnected</string>
<string name="warning_notif_ticker">Warning</string>
<string name="listening_notif_ticker">Listening</string>
</resources>