Start playing with notifications

This commit is contained in:
Matthieu 2021-07-01 16:35:02 +02:00
parent 8ad3e4e546
commit 4b6dc0074b
3 changed files with 93 additions and 0 deletions

View File

@ -41,6 +41,7 @@ import org.pixeldroid.app.searchDiscover.SearchDiscoverFragment
import org.pixeldroid.app.settings.SettingsActivity
import org.pixeldroid.app.utils.BaseActivity
import org.pixeldroid.app.utils.db.addUser
import org.pixeldroid.app.utils.notificationsWorker.enablePullNotifications
import org.pixeldroid.app.utils.db.entities.HomeStatusDatabaseEntity
import org.pixeldroid.app.utils.db.entities.PublicFeedStatusDatabaseEntity
import org.pixeldroid.app.utils.db.entities.UserDatabaseEntity
@ -96,6 +97,7 @@ class MainActivity : BaseActivity() {
}
)
setupTabs(tabs)
enablePullNotifications(this)
}
}

View File

@ -0,0 +1,20 @@
package org.pixeldroid.app.utils.notificationsWorker
import android.content.Context
import androidx.work.*
import java.util.concurrent.TimeUnit
fun enablePullNotifications(context: Context) {
val workManager = WorkManager.getInstance(context)
workManager.cancelAllWorkByTag("NOTIFICATION_PULL_TAG")
val workRequest: WorkRequest = PeriodicWorkRequestBuilder<NotificationWorker>(
PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS, TimeUnit.MILLISECONDS,
PeriodicWorkRequest.MIN_PERIODIC_FLEX_MILLIS, TimeUnit.MILLISECONDS
)
.addTag("NOTIFICATION_PULL_TAG")
.setConstraints(
Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build()
)
.build()
workManager.enqueue(workRequest)
}

View File

@ -0,0 +1,71 @@
package org.pixeldroid.app.utils.notificationsWorker
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.work.Worker
import androidx.work.WorkerParameters
import org.pixeldroid.app.R
import org.pixeldroid.app.settings.AboutActivity
class NotificationWorker(
context: Context,
params: WorkerParameters
) : Worker(context, params) {
override fun doWork(): Result {
Log.e("worker", "is working")
//TODO fetch notifications and create it
createNotificationChannel()
// Create an explicit intent for an Activity in your app
val intent = Intent(applicationContext, AboutActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
val pendingIntent: PendingIntent = PendingIntent.getActivity(applicationContext, 0, intent, 0)
val builder = NotificationCompat.Builder(applicationContext, "TestNotification")
.setSmallIcon(R.drawable.explore_24dp)
.setContentTitle("My notification")
.setContentText("Much longer text that cannot fit one line...")
.setStyle(NotificationCompat.BigTextStyle()
.bigText("Much longer text that cannot fit one line..."))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setAutoCancel(true)
with(NotificationManagerCompat.from(applicationContext)) {
// notificationId is a unique int for each notification that you must define
notify(420, builder.build())
}
return Result.success()
}
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 name = "test name"
val descriptionText = "test description"
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel("TestNotification", name, importance).apply {
description = descriptionText
}
// Register the channel with the system
val notificationManager: NotificationManager =
applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
}
}