Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.joshualsmn.mademovie.util.notification
- import android.app.AlarmManager
- import android.app.NotificationChannel
- import android.app.NotificationManager
- import android.app.PendingIntent
- import android.content.BroadcastReceiver
- import android.content.Context
- import android.content.Intent
- import android.media.RingtoneManager
- import android.os.Build
- import android.util.Log
- import android.widget.Toast
- import androidx.core.app.NotificationCompat
- import androidx.core.content.ContextCompat
- import com.joshualsmn.mademovie.BuildConfig
- import com.joshualsmn.mademovie.R
- import com.joshualsmn.mademovie.model.MovieItems
- import com.joshualsmn.mademovie.service.BaseService
- import com.joshualsmn.mademovie.service.DiscoverService
- import com.joshualsmn.mademovie.service.response.MovieResponse
- import com.joshualsmn.mademovie.util.CommonConstant
- import retrofit2.Call
- import retrofit2.Response
- import java.text.SimpleDateFormat
- import java.util.*
- import kotlin.collections.ArrayList
- /**
- * Created by Dihardja Software on 2019-12-09.
- */
- class AlarmReceiver : BroadcastReceiver() {
- private var movieItems: ArrayList<MovieItems>? = null
- override fun onReceive(context: Context, intent: Intent) {
- val type = intent.getStringExtra(CommonConstant.PARAM_TYPE)
- val notificationId = if (type.equals(
- CommonConstant.TYPE_DAILY,
- ignoreCase = true
- )
- ) CommonConstant.NOTIFICATION_ID_DAILY else CommonConstant.NOTIFICATION_ID_RELEASE
- showAlarmNotification(context, notificationId)
- }
- fun setDailyAlarm(context: Context) {
- val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
- val intent = Intent(context, AlarmReceiver::class.java)
- intent.putExtra(
- CommonConstant.PARAM_TYPE,
- CommonConstant.TYPE_DAILY
- )
- val calendar = Calendar.getInstance()
- calendar.set(Calendar.HOUR_OF_DAY, 6)
- calendar.set(Calendar.MINUTE, 0)
- calendar.set(Calendar.SECOND, 0)
- val pendingIntent =
- PendingIntent.getBroadcast(
- context,
- CommonConstant.NOTIFICATION_ID_DAILY,
- intent,
- 0
- )
- alarmManager.setInexactRepeating(
- AlarmManager.RTC_WAKEUP,
- calendar.timeInMillis,
- AlarmManager.INTERVAL_DAY,
- pendingIntent
- )
- Toast.makeText(context, context.getString(R.string.notification_add_daily), Toast.LENGTH_SHORT).show()
- }
- fun setReleaseAlarm(context: Context) {
- val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
- val intent = Intent(context, AlarmReceiver::class.java)
- intent.putExtra(
- CommonConstant.PARAM_TYPE,
- CommonConstant.TYPE_RELEASE
- )
- val calendar = Calendar.getInstance()
- calendar.set(Calendar.HOUR_OF_DAY, 7)
- calendar.set(Calendar.MINUTE, 0)
- calendar.set(Calendar.SECOND, 0)
- val pendingIntent =
- PendingIntent.getBroadcast(
- context,
- CommonConstant.NOTIFICATION_ID_RELEASE,
- intent,
- 0
- )
- alarmManager.setInexactRepeating(
- AlarmManager.RTC_WAKEUP,
- calendar.timeInMillis,
- AlarmManager.INTERVAL_DAY,
- pendingIntent
- )
- Toast.makeText(context, context.getString(R.string.notification_add_release), Toast.LENGTH_SHORT).show()
- }
- fun cancelAlarm(context: Context, type: String) {
- when(type == CommonConstant.TYPE_DAILY){
- true->{
- val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
- val intent = Intent(context, AlarmReceiver::class.java)
- val requestCode = CommonConstant.NOTIFICATION_ID_DAILY
- val pendingIntent = PendingIntent.getBroadcast(context, requestCode, intent, 0)
- pendingIntent.cancel()
- alarmManager.cancel(pendingIntent)
- Toast.makeText(context, context.getString(R.string.notification_remove_daily), Toast.LENGTH_SHORT).show()
- }
- false->{
- val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
- val intent = Intent(context, AlarmReceiver::class.java)
- val requestCode = CommonConstant.NOTIFICATION_ID_RELEASE
- val pendingIntent = PendingIntent.getBroadcast(context, requestCode, intent, 0)
- pendingIntent.cancel()
- alarmManager.cancel(pendingIntent)
- Toast.makeText(context, context.getString(R.string.notification_remove_release), Toast.LENGTH_SHORT).show()
- }
- }
- }
- private fun showAlarmNotification(
- context: Context,
- notificationId: Int
- ) {
- when(notificationId == CommonConstant.NOTIFICATION_ID_DAILY){
- true -> {
- val channelId = "DAILY_CHANNEL"
- val channelName = "Daily channel"
- val notificationManagerCompat =
- context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
- val alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
- val builder = NotificationCompat.Builder(context, channelId)
- .setSmallIcon(R.drawable.ic_notifications)
- .setContentTitle(context.getString(R.string.daily_reminder))
- .setContentText(context.getString(R.string.daily_reminder_desc))
- .setColor(ContextCompat.getColor(context, android.R.color.transparent))
- .setVibrate(longArrayOf(1000, 1000, 1000, 1000, 1000))
- .setSound(alarmSound)
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- val channel = NotificationChannel(
- channelId,
- channelName,
- NotificationManager.IMPORTANCE_DEFAULT
- )
- channel.enableVibration(true)
- channel.vibrationPattern = longArrayOf(1000, 1000, 1000, 1000, 1000)
- builder.setChannelId(channelId)
- notificationManagerCompat.createNotificationChannel(channel)
- }
- val notification = builder.build()
- notificationManagerCompat.notify(notificationId, notification)
- }
- false -> {
- getTodayMovie()
- when(movieItems?.isNotEmpty()){
- true -> {
- val channelId = "RELEASE_CHANNEL"
- val channelName = "Release channel"
- val notificationManagerCompat =
- context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
- val alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
- val builder = NotificationCompat.Builder(context, channelId)
- .setSmallIcon(R.drawable.ic_notifications)
- .setContentTitle(movieItems!![0].movieName)
- .setContentText(movieItems!![0].movieDesc)
- .setColor(ContextCompat.getColor(context, android.R.color.transparent))
- .setVibrate(longArrayOf(1000, 1000, 1000, 1000, 1000))
- .setSound(alarmSound)
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- val channel = NotificationChannel(
- channelId,
- channelName,
- NotificationManager.IMPORTANCE_DEFAULT
- )
- channel.enableVibration(true)
- channel.vibrationPattern = longArrayOf(1000, 1000, 1000, 1000, 1000)
- builder.setChannelId(channelId)
- notificationManagerCompat.createNotificationChannel(channel)
- }
- val notification = builder.build()
- notificationManagerCompat.notify(notificationId, notification)
- }
- }
- }
- }
- }
- private fun getTodayMovie() {
- val language = when (Locale.getDefault().language == Locale.ENGLISH.language) {
- true -> {
- "en-US"
- }
- false -> {
- "id-ID"
- }
- }
- val date = getCurrentDateTime()
- val dateInString = date.toString("yyyy-MM-dd")
- val service: DiscoverService = BaseService.create(DiscoverService::class.java)
- service.getTodayMovie(BuildConfig.API_KEY, language, dateInString, dateInString)
- .enqueue(object :
- retrofit2.Callback<MovieResponse.Result> {
- override fun onFailure(call: Call<MovieResponse.Result>, t: Throwable) {
- Log.d("Exception", t.message.toString())
- }
- override fun onResponse(
- call: Call<MovieResponse.Result>,
- response: Response<MovieResponse.Result>
- ) {
- when (response.isSuccessful && response.body() != null) {
- true -> {
- if (response.body()!!.results.isNotEmpty()) {
- movieItems = response.body()!!.results
- }
- }
- false -> {
- Log.d("Exception", response.message())
- }
- }
- }
- })
- }
- private fun Date.toString(format: String, locale: Locale = Locale.getDefault()): String {
- val formatter = SimpleDateFormat(format, locale)
- return formatter.format(this)
- }
- private fun getCurrentDateTime(): Date {
- return Calendar.getInstance().time
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement