-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from urFate/notifications
Merge notifications branch
- Loading branch information
Showing
23 changed files
with
798 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
app/src/main/java/live/shirabox/shirabox/NotificationService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package live.shirabox.shirabox | ||
|
||
import android.app.Notification | ||
import android.app.NotificationChannel | ||
import android.app.NotificationManager | ||
import android.app.PendingIntent | ||
import android.app.TaskStackBuilder | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.os.Build | ||
import android.util.Log | ||
import androidx.annotation.RequiresApi | ||
import com.google.firebase.messaging.FirebaseMessagingService | ||
import com.google.firebase.messaging.RemoteMessage | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.SupervisorJob | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
import live.shirabox.core.entity.NotificationEntity | ||
import live.shirabox.core.model.ContentType | ||
import live.shirabox.core.util.Util | ||
import live.shirabox.shirabox.db.AppDatabase | ||
import live.shirabox.shirabox.ui.activity.resource.ResourceActivity | ||
import java.net.URL | ||
|
||
class NotificationService : FirebaseMessagingService() { | ||
companion object { | ||
private const val TAG = "ShiraBoxService" | ||
private const val MAIN_CHANNEL_ID = "SB_NOTIFICATIONS" | ||
lateinit var db: AppDatabase | ||
} | ||
|
||
private val job = SupervisorJob() | ||
private val scope = CoroutineScope(Dispatchers.IO + job) | ||
|
||
private data class MessageData( | ||
val title: String, | ||
val body: String, | ||
val thumbnailUrl: String?, | ||
val shikimoriId: Int | ||
) | ||
|
||
override fun onNewToken(token: String) { | ||
super.onNewToken(token) | ||
Log.d(TAG, "New token observed: $token") | ||
} | ||
|
||
override fun onMessageReceived(remoteMessage: RemoteMessage) { | ||
super.onMessageReceived(remoteMessage) | ||
remoteMessage.data.ifEmpty { return } | ||
|
||
Log.d(TAG, "Message data payload: ${remoteMessage.data}") | ||
|
||
try { | ||
db = AppDatabase.getAppDataBase(this)!! | ||
|
||
val data = remoteMessage.data | ||
|
||
val messageData = MessageData( | ||
title = data["title"] ?: "Undefined title", | ||
body = data["body"] ?: "Undefined notification body", | ||
shikimoriId = data["shikimori_id"]!!.toInt(), | ||
thumbnailUrl = data["thumbnail"] | ||
) | ||
|
||
scope.launch { | ||
launch { sendNotification(messageData) } | ||
launch { saveNotification(messageData) } | ||
} | ||
|
||
} catch (ex: Exception) { | ||
ex.printStackTrace() | ||
} | ||
} | ||
|
||
private suspend fun sendNotification(messageData: MessageData) { | ||
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | ||
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
manager.createNotificationChannel(notificationChannel(this)) | ||
} | ||
|
||
val thumbnailBitmap = messageData.thumbnailUrl?.let { | ||
withContext(Dispatchers.IO) { | ||
async { Util.getBitmapFromURL(URL(it)) } | ||
}.await() | ||
} | ||
|
||
val activityIntent = Intent(this, ResourceActivity::class.java).apply { | ||
putExtra("id", messageData.shikimoriId) | ||
putExtra("type", ContentType.ANIME.toString()) | ||
} | ||
val activityPendingIntent: PendingIntent? = TaskStackBuilder.create(this).run { | ||
addNextIntentWithParentStack(activityIntent) | ||
getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE) | ||
} | ||
|
||
val notification: Notification = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
Notification.Builder(this@NotificationService, MAIN_CHANNEL_ID).apply { | ||
setContentTitle(messageData.title) | ||
setContentText(messageData.body) | ||
setLargeIcon(thumbnailBitmap) | ||
setSmallIcon(R.drawable.ic_stat_shirabox_notification) | ||
setContentIntent(activityPendingIntent) | ||
setAutoCancel(true) | ||
}.build() | ||
} else { | ||
Notification.Builder(this@NotificationService).apply { | ||
setContentTitle(messageData.title) | ||
setContentText(messageData.body) | ||
setLargeIcon(thumbnailBitmap) | ||
setSmallIcon(R.drawable.ic_stat_shirabox_notification) | ||
setContentIntent(activityPendingIntent) | ||
setAutoCancel(true) | ||
}.build() | ||
} | ||
|
||
manager.notify(System.nanoTime().toInt(), notification) | ||
} | ||
|
||
private suspend fun saveNotification(messageData: MessageData) { | ||
withContext(Dispatchers.IO) { | ||
db.notificationDao().insertNotification( | ||
NotificationEntity( | ||
contentShikimoriId = messageData.shikimoriId, | ||
receiveTimestamp = System.currentTimeMillis(), | ||
title = messageData.title, | ||
body = messageData.body, | ||
thumbnailUrl = messageData.thumbnailUrl ?: "" | ||
) | ||
) | ||
} | ||
} | ||
|
||
@RequiresApi(Build.VERSION_CODES.O) | ||
private fun notificationChannel(context: Context) = NotificationChannel( | ||
MAIN_CHANNEL_ID, | ||
context.getString(R.string.notificaion_channel), | ||
NotificationManager.IMPORTANCE_DEFAULT | ||
).apply { description = context.getString(R.string.notificaion_channel_description) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
app/src/main/java/live/shirabox/shirabox/db/dao/NotificationDao.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package live.shirabox.shirabox.db.dao | ||
|
||
import androidx.room.Dao | ||
import androidx.room.Delete | ||
import androidx.room.Insert | ||
import androidx.room.OnConflictStrategy | ||
import androidx.room.Query | ||
import androidx.room.Transaction | ||
import androidx.room.Update | ||
import kotlinx.coroutines.flow.Flow | ||
import live.shirabox.core.entity.NotificationEntity | ||
import live.shirabox.core.entity.relation.NotificationAndContent | ||
|
||
@Dao | ||
interface NotificationDao { | ||
@Query("SELECT * FROM notification") | ||
fun all(): Flow<List<NotificationEntity>> | ||
|
||
@Transaction | ||
@Query("SELECT * FROM notification") | ||
fun allNotificationsWithContent(): Flow<List<NotificationAndContent>> | ||
|
||
@Transaction | ||
@Query("SELECT * FROM notification WHERE content_shikimori_id IS :shikimoriId") | ||
fun notificationsFromParent(shikimoriId: Int): Flow<List<NotificationEntity>> | ||
|
||
@Insert(onConflict = OnConflictStrategy.ABORT) | ||
fun insertNotification(vararg notificationEntity: NotificationEntity) | ||
|
||
@Update | ||
fun updateNotification(vararg notificationEntity: NotificationEntity) | ||
|
||
@Delete | ||
fun deleteNotification(vararg notificationEntity: NotificationEntity) | ||
|
||
@Query("DELETE FROM notification") | ||
fun deleteAll() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.