parent
ec71899630
commit
c5ba4da06c
@ -0,0 +1,84 @@ |
|||||||
|
package net.pokeranalytics.android.util |
||||||
|
|
||||||
|
import android.app.Notification |
||||||
|
import android.app.NotificationChannel |
||||||
|
import android.app.NotificationManager |
||||||
|
import android.content.Context |
||||||
|
import android.graphics.Color |
||||||
|
import android.media.RingtoneManager |
||||||
|
import android.os.Build |
||||||
|
import androidx.core.app.NotificationCompat |
||||||
|
import androidx.core.app.NotificationManagerCompat |
||||||
|
import androidx.work.Worker |
||||||
|
import androidx.work.WorkerParameters |
||||||
|
import net.pokeranalytics.android.R |
||||||
|
import timber.log.Timber |
||||||
|
import java.util.* |
||||||
|
|
||||||
|
class NotificationSchedule(var context: Context, var params: WorkerParameters) : Worker(context, params) { |
||||||
|
|
||||||
|
enum class ParamKeys(val value: String) { |
||||||
|
TITLE("title"), |
||||||
|
BODY("body") |
||||||
|
} |
||||||
|
|
||||||
|
override fun doWork(): Result { |
||||||
|
val data = params.inputData |
||||||
|
val title = data.getString(ParamKeys.TITLE.value) |
||||||
|
val body = data.getString(ParamKeys.BODY.value) |
||||||
|
|
||||||
|
if (title != null && body != null) { |
||||||
|
TriggerNotification(context, title, body) |
||||||
|
} else { |
||||||
|
Timber.d("Missing title and or title for notification") |
||||||
|
} |
||||||
|
|
||||||
|
return Result.success() |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
class TriggerNotification(context: Context, title: String, body: String) { |
||||||
|
|
||||||
|
init { |
||||||
|
sendNotification(context, title, body) |
||||||
|
} |
||||||
|
|
||||||
|
private fun createNotificationChannel(context: Context, name: String, description: String): String { |
||||||
|
// Create the NotificationChannel, but only on API 26+ because |
||||||
|
// the NotificationChannel class is new and not in the support library |
||||||
|
val channelId = UUID.randomUUID().toString() |
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
||||||
|
val importance = NotificationManager.IMPORTANCE_HIGH |
||||||
|
val channel = NotificationChannel(channelId, name, importance) |
||||||
|
channel.enableLights(true) |
||||||
|
channel.enableVibration(true) |
||||||
|
channel.description = description |
||||||
|
channel.lightColor = Color.GREEN |
||||||
|
channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC |
||||||
|
|
||||||
|
val notificationManager = context.getSystemService(NotificationManager::class.java) |
||||||
|
notificationManager?.createNotificationChannel(channel) |
||||||
|
} |
||||||
|
|
||||||
|
return channelId |
||||||
|
} |
||||||
|
|
||||||
|
private fun sendNotification(context: Context, title: String, body: String) { |
||||||
|
|
||||||
|
val notificationManager = NotificationManagerCompat.from(context) |
||||||
|
val mBuilder = NotificationCompat.Builder(context, createNotificationChannel(context, title, body)) |
||||||
|
val notificationId = (System.currentTimeMillis() and 0xfffffff).toInt() |
||||||
|
|
||||||
|
mBuilder.setDefaults(Notification.DEFAULT_ALL) |
||||||
|
.setContentTitle(title) |
||||||
|
.setContentText(body) |
||||||
|
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) |
||||||
|
.setPriority(NotificationCompat.PRIORITY_HIGH) |
||||||
|
.setSmallIcon(R.drawable.release_note_icon) |
||||||
|
// .setContentInfo("Content Info") |
||||||
|
.setAutoCancel(true) |
||||||
|
|
||||||
|
notificationManager.notify(notificationId, mBuilder.build()) |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue