Boa noite, eu criei um serviço de notificação para o meu APP, ele deveria exibir as notificações no momento definido, no caso 6h da manhã, porém ele exibe várias vezes durante períodos aleatórios do dia. Alguém pode me ajudar a descobrir o problema? Código:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tk.thallyssonklein.kapp" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".AlarmBroadcastReceiver" android:process=":remote">
</receiver>
<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:enabled="true" android:name=".NotificationService" android:label="Notification Service"/>
</application>
</manifest>
BootReceiver.kt
package tk.thallyssonklein.kapp
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
/**
* Created by thall on 22/01/2018.
*/
class BootReceiver : BroadcastReceiver(){
override fun onReceive(p0: Context?, p1: Intent?) {
val intent = Intent(p0, NotificationService::class.java)
if (p0 != null) {
p0.startService(intent)
}
}
}
NotificationService.kt
package tk.thallyssonklein.kapp
import android.app.AlarmManager
import android.app.PendingIntent
import android.app.Service
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.IBinder
import android.support.v7.app.AppCompatActivity
import java.util.*
/**
* Created by thall on 21/01/2018.
*/
class NotificationService : Service(){
lateinit var preferences: SharedPreferences
override fun onBind(p0: Intent?): IBinder {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onCreate(){
preferences = getSharedPreferences("tk.thallyssonklein.kapp.notifications", AppCompatActivity.MODE_PRIVATE)
val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
var id = 0
for(mov in preferences.all){
alarmManager.set(
AlarmManager.RTC_WAKEUP,
Calendar.getInstance().apply {
set(Calendar.HOUR_OF_DAY, 6)
set(Calendar.MINUTE, 0)
set(Calendar.SECOND, 0)
}.timeInMillis,
PendingIntent.getBroadcast(
applicationContext,
id,
Intent(applicationContext, AlarmBroadcastReceiver::class.java).apply {
putExtra("notificationId", id)
putExtra("ticker", "A morning exercise")
putExtra("title", mov.key)
putExtra("text", "Do this exercise now!")
},
PendingIntent.FLAG_CANCEL_CURRENT
)
)
++id
}
}
}
AlarmBroadcastReceiver.kt
package tk.thallyssonklein.kapp
/**
* Created by thall on 14/01/2018.
*/
import android.app.Notification
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
class AlarmBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
(context?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).notify(
intent!!.getIntExtra("notificationId", 0),
Notification.Builder(context).apply {
setSmallIcon(tk.thallyssonklein.kapp.R.drawable.ic_stretching_exercises)
setContentTitle(intent.getStringExtra("title"))
setContentText(intent.getCharSequenceExtra("text"))
setWhen(System.currentTimeMillis())
setTicker(intent.getCharSequenceExtra("ticker"))
setPriority(Notification.PRIORITY_DEFAULT)
setAutoCancel(true)
setDefaults(Notification.DEFAULT_SOUND)
setContentIntent(PendingIntent.getActivity(context, 0, Intent(context, NotificationService::class.java), 0))
}.build()
)
}
}
Eu já tentei de tudo mas não descobri nenhum erro nesse código.