Android Scheduled Notification

Papon Ahasan - Sep 12 - - Dev Community
data class NotificationModel(
    var id: String,
    var titleEng: String,
    var titleBng: String?,
    var bodyEng: String,
    var bodyBng: String?,
    var link: String?,
    var button: String?,
    var image: String?,
)
Enter fullscreen mode Exit fullscreen mode
val listOfAlarm = listOf(
    NotificationModel(
        id = "1",
        titleEng = "পরিবারের সকল সদস্যের নামে ঔষধ বুকমার্ক করুন।\n",
        titleBng = "পরিবারের সকল সদস্যের নামে ঔষধ বুকমার্ক করুন।\n",
        bodyEng = "পেশেন্ট এইড দিচ্ছে মাল্টি (একাধিক) প্রোফাইল তৈরি করার সুবিধা। পরিবারের সদস্যদের ঔষধ বুকমার্ক করে রাখুন তাদের নিজেদের নামে। সুবিধাটি রয়েছে শুধু প্রিমিয়াম ব্যবহার কারিদের জন্য। তাই দেরি না করে এখনি কিনে নিন।",
        bodyBng = "পেশেন্ট এইড দিচ্ছে মাল্টি (একাধিক) প্রোফাইল তৈরি করার সুবিধা। পরিবারের সদস্যদের ঔষধ বুকমার্ক করে রাখুন তাদের নিজেদের নামে। সুবিধাটি রয়েছে শুধু প্রিমিয়াম ব্যবহার কারিদের জন্য। তাই দেরি না করে এখনি কিনে নিন।",
        link = "",
        button = "",
        image = "",
    )
}
Enter fullscreen mode Exit fullscreen mode
    private fun periodicScheduleNotificationWorker() {
        val workRequest =
            PeriodicWorkRequestBuilder<ScheduledNotificationManager>(15, TimeUnit.MINUTES)
                // Additional configuration
                .addTag("SN2")
                .build()

        WorkManager.getInstance(this)
            .enqueueUniquePeriodicWork(
                ScheduledNotificationManager.TAG,
                ExistingPeriodicWorkPolicy.KEEP,
                workRequest
            )
        Log.d("tag", "Periodic Scheduled Notification Worker Launched2")
    }
Enter fullscreen mode Exit fullscreen mode
class UserActivityTracker(context: Context) {
    fun saveLastMessageIndex(index: Int) {
        sharedPreferences.edit().putInt("last_message_index", index).apply()
    }

    fun getLastMessageIndex(): Int {
        return sharedPreferences.getInt("last_message_index", 0)
    }

    fun setLastActivity() {
        val currentTime = System.currentTimeMillis()
        sharedPreferences.edit().putLong("last_activity_timestamp", currentTime).apply()
    }

    private fun getLastActivityTime(): Long {
        return sharedPreferences.getLong("last_activity_timestamp", 0L)
    }

    fun isInActiveInLastFiveDays(): Boolean {
        val currentTime = System.currentTimeMillis()
        val fiveDaysInMillis = Constants.INTERVAL_5_DAYS // 5 days in milliseconds 
        val lastActivityTimestamp = getLastActivityTime()
        val isActiveInLastFiveDays = currentTime - lastActivityTimestamp >= fiveDaysInMillis
        return isActiveInLastFiveDays
    }
}
Enter fullscreen mode Exit fullscreen mode
class ScheduledNotificationManager(
    appContext: Context, params: WorkerParameters,
) : CoroutineWorker(appContext, params) {

    companion object {
        const val TAG = "ScheduledNotificationManager"
    }

    override suspend fun doWork(): Result {
        try {
            val tracker = UserActivityTracker(applicationContext)
            val lastMessageIndex = tracker.getLastMessageIndex()

            if (tracker.isInActiveInLastFiveDays()) {
                // User has not been active in the last 5 days
                Log.d(TAG, "User is not active in the last 5 days.")
                val notifyUtil = PeriodicNotificationUtil()
                val notifyManager =
                    applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
                notifyUtil.sendNotification(applicationContext, listOfAlarm[lastMessageIndex], notifyManager)

                if (lastMessageIndex == listOfAlarm.size - 1) {
                    tracker.saveLastMessageIndex(0)
                } else {
                    tracker.saveLastMessageIndex(lastMessageIndex + 1)
                }
            } else {
                // User has been active in the last 5 days
                Log.d(TAG, "UserStatus: User is active in the last 5 days.")
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }


        return Result.success()
    }
}
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . .
Terabox Video Player