TIME_TICK не срабатывает при закрытом приложении
TIME_TICK - установка для android вызывать каждую минуту Broadcast
Но вот у меня при открытом приложении все работает, но при закрытом, оно не срабатывает, не понимаю почему вроде бы везде пишут что там
<receiver
android:name=".broadcast.NotificationListener"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.TIME_TICK" />
</intent-filter>
</receiver>
Код из Activity которая запускается первой
override fun onResume() {
super.onResume()
registerReceiver(notificationBroadCast, IntentFilter(Intent.ACTION_TIME_TICK))
}
класс Broadcast
class NotificationListener : BroadcastReceiver() {
@Inject
lateinit var mAuth: FirebaseAuth
@Inject
lateinit var base: DatabaseReference
override fun onReceive(context: Context, intent: Intent) {
Log.i(TAG!!,"Lived Broad")
App.appComponent.inject(this)
Log.i(TAG,"Lived Load")
val list = mutableListOf<Message>()
base.child(NOTIFICATION).child(mAuth.currentUser!!.uid).addListenerForSingleValueEvent(object : ValueEventListener{
override fun onDataChange(snapshot: DataSnapshot) {
for (userMessages in snapshot.children){
for (messageSnapshot in userMessages.children){
list.add(messageSnapshot.getValue(Message::class.java)!!)
//messageSnapshot.ref.removeValue()
}
}
if (list.isNotEmpty()) {
Log.i("Notification","Broad died ${list.size}")
context.startService(MessageService.newInstance(context, list))
}
Log.i(TAG,"Broad died ${list.size}")
}
override fun onCancelled(error: DatabaseError) {
Log.i(TAG,"Broad error")
}
})
Log.i(TAG,"Lived Finish")
}
класс IntentService
class MessageService : IntentService("MessageService") {
companion object{
private const val LIST_MESSAGE = "list_message"
fun newInstance(context: Context, list: MutableList<Message>): Intent{
val intent = Intent(context, MessageService::class.java)
intent.putExtra(LIST_MESSAGE, list as Serializable)
return intent
}
}
override fun onHandleIntent(intent: Intent?) {
Log.i("Notification", "onHandle")
val list = intent?.getSerializableExtra(LIST_MESSAGE) as MutableList<Message>
Log.i(TAG!!, "onHandle ${list.size}")
Log.i("Notification", "onHandle ${list.size}")
createNotification(list)
}
private fun createNotification(list: MutableList<Message>){
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
for (message in list) {
val pendingIntent = PendingIntent.getActivity(
this,
NOTIFICATION_ID,
DialogWithUserFragment.newInstance(baseContext, message),
PendingIntent.FLAG_UPDATE_CURRENT
)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationChannel = NotificationChannel(
CHANEL_ID,
"My Notifications",
NotificationManager.IMPORTANCE_LOW
)
notificationChannel.setSound(null, null)
manager.createNotificationChannel(notificationChannel)
}
val builder = NotificationCompat.Builder(this, CHANEL_ID)
builder.setSmallIcon(R.mipmap.icon)
.setContentTitle(message.userId)
.setContentText(message.text)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setAutoCancel(true)
Log.i(TAG!!, "Notification create")
manager.notify(NOTIFICATION_ID, builder.build())
}
}
Очень важно, что бы уведомление могло прийти при закрытом приложении