mirror of https://github.com/Xe/xesite_android
add notifications to android app
Signed-off-by: Christine Dodrill <me@christine.website>
This commit is contained in:
parent
5ea97dac68
commit
ea7c18723f
|
@ -16,9 +16,37 @@
|
||||||
package website.christine.xesite
|
package website.christine.xesite
|
||||||
|
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
|
import android.app.NotificationChannel
|
||||||
|
import android.app.NotificationManager
|
||||||
|
import android.content.Context
|
||||||
|
import android.os.Build
|
||||||
|
|
||||||
class Application : Application() {
|
class Application : Application() {
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
|
|
||||||
|
val ctx = this.applicationContext
|
||||||
|
createNotificationChannel(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun createNotificationChannel(ctx: Context) {
|
||||||
|
// 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 = NEW_POST_CHANNEL
|
||||||
|
val descriptionText = NEW_POST_CHANNEL_DESC
|
||||||
|
val importance = NotificationManager.IMPORTANCE_LOW
|
||||||
|
val channel = NotificationChannel(NEW_POST_CHANNEL, name, importance).apply {
|
||||||
|
description = descriptionText
|
||||||
|
}
|
||||||
|
// Register the channel with the system
|
||||||
|
val notificationManager: NotificationManager =
|
||||||
|
ctx.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||||
|
notificationManager.createNotificationChannel(channel)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal val NEW_POST_CHANNEL = "New Posts";
|
||||||
|
internal val NEW_POST_CHANNEL_DESC = "New posts on christine.website";
|
||||||
|
internal val notificationId = 1;
|
|
@ -15,6 +15,7 @@
|
||||||
*/
|
*/
|
||||||
package website.christine.xesite
|
package website.christine.xesite
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import com.google.androidbrowserhelper.trusted.LauncherActivity
|
import com.google.androidbrowserhelper.trusted.LauncherActivity
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,15 @@
|
||||||
package website.christine.xesite
|
package website.christine.xesite
|
||||||
|
|
||||||
|
import android.app.NotificationChannel
|
||||||
|
import android.app.NotificationManager
|
||||||
import android.appwidget.AppWidgetManager
|
import android.appwidget.AppWidgetManager
|
||||||
import android.appwidget.AppWidgetProvider
|
import android.appwidget.AppWidgetProvider
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.os.Build
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.widget.RemoteViews
|
import android.widget.RemoteViews
|
||||||
|
import androidx.core.app.NotificationCompat
|
||||||
|
import androidx.core.app.NotificationManagerCompat
|
||||||
import com.android.volley.RequestQueue
|
import com.android.volley.RequestQueue
|
||||||
import com.android.volley.Response
|
import com.android.volley.Response
|
||||||
import com.android.volley.toolbox.BasicNetwork
|
import com.android.volley.toolbox.BasicNetwork
|
||||||
|
@ -23,6 +28,18 @@ class NewPostWidget : AppWidgetProvider() {
|
||||||
return ctx.packageName.plus("/").plus(pkgInfo.versionName)
|
return ctx.packageName.plus("/").plus(pkgInfo.versionName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun notify(ctx: Context, newPost: NewPost) {
|
||||||
|
var builder = NotificationCompat.Builder(ctx, NEW_POST_CHANNEL)
|
||||||
|
.setSmallIcon(R.drawable.splash)
|
||||||
|
.setContentTitle(newPost.title)
|
||||||
|
.setContentText(newPost.summary)
|
||||||
|
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||||
|
|
||||||
|
NotificationManagerCompat.from(ctx).apply {
|
||||||
|
notify(notificationId, builder.build())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun onUpdate(
|
override fun onUpdate(
|
||||||
ctx: Context,
|
ctx: Context,
|
||||||
appWidgetManager: AppWidgetManager,
|
appWidgetManager: AppWidgetManager,
|
||||||
|
@ -38,6 +55,13 @@ class NewPostWidget : AppWidgetProvider() {
|
||||||
NewPost::class.java,
|
NewPost::class.java,
|
||||||
headers,
|
headers,
|
||||||
Response.Listener<NewPost> { response ->
|
Response.Listener<NewPost> { response ->
|
||||||
|
val oldURL = loadPref(ctx, "old_url", response.link)
|
||||||
|
if (response.link != oldURL) {
|
||||||
|
// make notification?
|
||||||
|
this.notify(ctx, response)
|
||||||
|
savePref(ctx, "old_url", response.link)
|
||||||
|
}
|
||||||
|
|
||||||
Log.println(Log.INFO, "new_post", response.toString())
|
Log.println(Log.INFO, "new_post", response.toString())
|
||||||
// There may be multiple widgets active, so update all of them
|
// There may be multiple widgets active, so update all of them
|
||||||
for (appWidgetId in appWidgetIds) {
|
for (appWidgetId in appWidgetIds) {
|
||||||
|
@ -75,6 +99,8 @@ class NewPostWidget : AppWidgetProvider() {
|
||||||
|
|
||||||
override fun onEnabled(ctx: Context) {
|
override fun onEnabled(ctx: Context) {
|
||||||
this.makeQueue(ctx)
|
this.makeQueue(ctx)
|
||||||
|
//this.createNotificationChannel(ctx)
|
||||||
|
savePref(ctx, "old_url", "http://google.com")
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDisabled(context: Context) {
|
override fun onDisabled(context: Context) {
|
||||||
|
@ -103,3 +129,18 @@ class NewPostWidget : AppWidgetProvider() {
|
||||||
appWidgetManager.updateAppWidget(appWidgetId, views)
|
appWidgetManager.updateAppWidget(appWidgetId, views)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private const val PREFS_NAME = "website.christine.xesite.NewPostWidget"
|
||||||
|
private const val PREF_PREFIX_KEY = "appwidget_"
|
||||||
|
|
||||||
|
internal fun savePref(ctx: Context, key: String, value: String) {
|
||||||
|
val prefs = ctx.getSharedPreferences(PREFS_NAME, 0).edit()
|
||||||
|
prefs.putString(PREF_PREFIX_KEY + key, value)
|
||||||
|
prefs.apply()
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun loadPref(ctx: Context, key: String, default: String): String {
|
||||||
|
val prefs = ctx.getSharedPreferences(PREFS_NAME, 0)
|
||||||
|
val titleValue = prefs.getString(PREF_PREFIX_KEY + key, default)
|
||||||
|
return titleValue ?: default
|
||||||
|
}
|
Loading…
Reference in New Issue