mirror of
https://github.com/Matte23/circolapp.git
synced 2025-12-06 07:29:10 +00:00
Sent all the data required to create a notification within the message
This commit is contained in:
@@ -1,16 +1,46 @@
|
||||
/*
|
||||
* Circolapp
|
||||
* Copyright (C) 2019-2020 Matteo Schiff
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.underdesk.circolapp.push
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import com.google.firebase.messaging.FirebaseMessagingService
|
||||
import com.google.firebase.messaging.RemoteMessage
|
||||
import net.underdesk.circolapp.works.PollWork
|
||||
import net.underdesk.circolapp.shared.data.Circular
|
||||
|
||||
// We don't need to get an Instance Token for topic notifications
|
||||
@SuppressLint("MissingFirebaseInstanceTokenRefresh")
|
||||
class CircolappFirebaseMessagingService : FirebaseMessagingService() {
|
||||
override fun onMessageReceived(remoteMessage: RemoteMessage) {
|
||||
if (remoteMessage.data.isNotEmpty()) {
|
||||
PollWork.runWork(applicationContext)
|
||||
if (remoteMessage.data["newCircular"] == true.toString()) {
|
||||
val id = remoteMessage.data["id"]?.toLong() ?: -1
|
||||
val name = remoteMessage.data["name"] ?: ""
|
||||
val url = remoteMessage.data["url"] ?: ""
|
||||
|
||||
val circular = Circular(
|
||||
id, -1, name, url, "",
|
||||
favourite = false,
|
||||
reminder = false,
|
||||
attachmentsNames = mutableListOf(),
|
||||
attachmentsUrls = mutableListOf()
|
||||
)
|
||||
|
||||
NotificationsUtils.createNotificationsForCirculars(listOf(circular), applicationContext)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,21 @@
|
||||
/*
|
||||
* Circolapp
|
||||
* Copyright (C) 2019-2020 Matteo Schiff
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.underdesk.circolapp.push
|
||||
|
||||
import android.content.Context
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* Circolapp
|
||||
* Copyright (C) 2019-2020 Matteo Schiff
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.underdesk.circolapp.push
|
||||
|
||||
import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.app.PendingIntent
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import androidx.core.app.TaskStackBuilder
|
||||
import net.underdesk.circolapp.MainActivity
|
||||
import net.underdesk.circolapp.R
|
||||
import net.underdesk.circolapp.shared.data.Circular
|
||||
import net.underdesk.circolapp.works.PollWork
|
||||
|
||||
object NotificationsUtils {
|
||||
fun createNotificationsForCirculars(circulars: List<Circular>, context: Context) {
|
||||
createNotificationChannel(context)
|
||||
|
||||
val summaryStyle = NotificationCompat.InboxStyle()
|
||||
.setBigContentTitle(context.getString(R.string.notification_summary_title))
|
||||
.setSummaryText(context.getString(R.string.notification_summary))
|
||||
|
||||
for (circular in circulars) {
|
||||
createNotification(circular, context)
|
||||
summaryStyle.addLine(circular.name)
|
||||
}
|
||||
|
||||
val summaryNotification =
|
||||
NotificationCompat.Builder(context, PollWork.CHANNEL_ID)
|
||||
.setContentTitle(context.getString(R.string.notification_summary_title))
|
||||
.setContentText(
|
||||
context.resources.getQuantityString(
|
||||
R.plurals.notification_summary_text,
|
||||
circulars.size,
|
||||
circulars.size
|
||||
)
|
||||
)
|
||||
.setSmallIcon(R.drawable.ic_notification)
|
||||
.setStyle(summaryStyle)
|
||||
.setGroup(PollWork.CHANNEL_ID)
|
||||
.setGroupSummary(true)
|
||||
.build()
|
||||
|
||||
with(NotificationManagerCompat.from(context)) {
|
||||
notify(-1, summaryNotification)
|
||||
}
|
||||
}
|
||||
|
||||
private fun createNotification(circular: Circular, context: Context) {
|
||||
val mainIntent = Intent(context, MainActivity::class.java)
|
||||
val viewIntent = Intent(Intent.ACTION_VIEW)
|
||||
|
||||
if (circular.url.endsWith(".pdf")) {
|
||||
viewIntent.setDataAndType(Uri.parse(circular.url), "application/pdf")
|
||||
} else {
|
||||
viewIntent.data = Uri.parse(circular.url)
|
||||
}
|
||||
|
||||
viewIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
|
||||
|
||||
val taskStackBuilder = TaskStackBuilder.create(context)
|
||||
taskStackBuilder.addParentStack(MainActivity::class.java)
|
||||
taskStackBuilder.addNextIntent(mainIntent)
|
||||
taskStackBuilder.addNextIntent(viewIntent)
|
||||
|
||||
val pendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
|
||||
val builder = NotificationCompat.Builder(context, PollWork.CHANNEL_ID)
|
||||
.setSmallIcon(R.drawable.ic_notification)
|
||||
.setContentTitle(context.getString(R.string.notification_title, circular.id))
|
||||
.setContentText(circular.name)
|
||||
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||
.setContentIntent(pendingIntent)
|
||||
.setAutoCancel(true)
|
||||
.setGroup(PollWork.CHANNEL_ID)
|
||||
.setStyle(
|
||||
NotificationCompat.BigTextStyle()
|
||||
.bigText(circular.name)
|
||||
)
|
||||
|
||||
with(NotificationManagerCompat.from(context)) {
|
||||
notify(circular.id.toInt(), builder.build())
|
||||
}
|
||||
}
|
||||
|
||||
private fun createNotificationChannel(context: Context) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
val name = context.getString(R.string.channel_name_new)
|
||||
val descriptionText = context.getString(R.string.channel_description_new)
|
||||
val importance = NotificationManager.IMPORTANCE_DEFAULT
|
||||
val channel = NotificationChannel(PollWork.CHANNEL_ID, name, importance).apply {
|
||||
description = descriptionText
|
||||
}
|
||||
|
||||
val notificationManager: NotificationManager =
|
||||
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
notificationManager.createNotificationChannel(channel)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,23 +18,12 @@
|
||||
|
||||
package net.underdesk.circolapp.works
|
||||
|
||||
import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.app.PendingIntent
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import androidx.core.app.TaskStackBuilder
|
||||
import androidx.preference.PreferenceManager
|
||||
import androidx.work.*
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import net.underdesk.circolapp.MainActivity
|
||||
import net.underdesk.circolapp.R
|
||||
import net.underdesk.circolapp.data.AndroidCircularRepository
|
||||
import net.underdesk.circolapp.shared.data.Circular
|
||||
import net.underdesk.circolapp.push.NotificationsUtils
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
class PollWork(appContext: Context, workerParams: WorkerParameters) :
|
||||
@@ -100,90 +89,9 @@ class PollWork(appContext: Context, workerParams: WorkerParameters) :
|
||||
val newCirculars = result.first
|
||||
|
||||
if (newCirculars.isNotEmpty()) {
|
||||
createNotificationChannel()
|
||||
|
||||
val summaryStyle = NotificationCompat.InboxStyle()
|
||||
.setBigContentTitle(applicationContext.getString(R.string.notification_summary_title))
|
||||
.setSummaryText(applicationContext.getString(R.string.notification_summary))
|
||||
|
||||
for (circular in newCirculars) {
|
||||
createNotification(circular)
|
||||
summaryStyle.addLine(circular.name)
|
||||
}
|
||||
|
||||
val summaryNotification =
|
||||
NotificationCompat.Builder(applicationContext, CHANNEL_ID)
|
||||
.setContentTitle(applicationContext.getString(R.string.notification_summary_title))
|
||||
.setContentText(
|
||||
applicationContext.resources.getQuantityString(
|
||||
R.plurals.notification_summary_text,
|
||||
newCirculars.size,
|
||||
newCirculars.size
|
||||
)
|
||||
)
|
||||
.setSmallIcon(R.drawable.ic_notification)
|
||||
.setStyle(summaryStyle)
|
||||
.setGroup(CHANNEL_ID)
|
||||
.setGroupSummary(true)
|
||||
.build()
|
||||
|
||||
with(NotificationManagerCompat.from(applicationContext)) {
|
||||
notify(-1, summaryNotification)
|
||||
}
|
||||
NotificationsUtils.createNotificationsForCirculars(newCirculars, applicationContext)
|
||||
}
|
||||
|
||||
Result.success()
|
||||
}
|
||||
|
||||
private fun createNotification(circular: Circular) {
|
||||
val mainIntent = Intent(applicationContext, MainActivity::class.java)
|
||||
val viewIntent = Intent(Intent.ACTION_VIEW)
|
||||
|
||||
if (circular.url.endsWith(".pdf")) {
|
||||
viewIntent.setDataAndType(Uri.parse(circular.url), "application/pdf")
|
||||
} else {
|
||||
viewIntent.data = Uri.parse(circular.url)
|
||||
}
|
||||
|
||||
viewIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
|
||||
|
||||
val taskStackBuilder = TaskStackBuilder.create(applicationContext)
|
||||
taskStackBuilder.addParentStack(MainActivity::class.java)
|
||||
taskStackBuilder.addNextIntent(mainIntent)
|
||||
taskStackBuilder.addNextIntent(viewIntent)
|
||||
|
||||
val pendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
|
||||
val builder = NotificationCompat.Builder(applicationContext, CHANNEL_ID)
|
||||
.setSmallIcon(R.drawable.ic_notification)
|
||||
.setContentTitle(applicationContext.getString(R.string.notification_title, circular.id))
|
||||
.setContentText(circular.name)
|
||||
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||
.setContentIntent(pendingIntent)
|
||||
.setAutoCancel(true)
|
||||
.setGroup(CHANNEL_ID)
|
||||
.setStyle(
|
||||
NotificationCompat.BigTextStyle()
|
||||
.bigText(circular.name)
|
||||
)
|
||||
|
||||
with(NotificationManagerCompat.from(applicationContext)) {
|
||||
notify(circular.id.toInt(), builder.build())
|
||||
}
|
||||
}
|
||||
|
||||
private fun createNotificationChannel() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
val name = applicationContext.getString(R.string.channel_name_new)
|
||||
val descriptionText = applicationContext.getString(R.string.channel_description_new)
|
||||
val importance = NotificationManager.IMPORTANCE_DEFAULT
|
||||
val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
|
||||
description = descriptionText
|
||||
}
|
||||
|
||||
val notificationManager: NotificationManager =
|
||||
applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
notificationManager.createNotificationChannel(channel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user