I am new to android development and I want to send a notification from the IntentService when the device enters in any specific geofence which is setted by the user. I had written the below code for sending a notification.
class GeofenceIntentService : IntentService("GeofenceIntentService") {
var notId: Int = 15452
override fun onHandleIntent(intent: Intent?) {
var geofencingEvent = GeofencingEvent.fromIntent(intent)
if(geofencingEvent.hasError())
{
Log.e("JK-->>","geofencingError -->> "+geofencingEvent.hasError())
return
}
var geofenceTransition = geofencingEvent.geofenceTransition
if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER)
{
var pendingIntent = PendingIntent.getService(applicationContext,0,Intent(applicationContext,MainActivity::class.java),PendingIntent.FLAG_UPDATE_CURRENT)
var notification = NotificationCompat.Builder(applicationContext).setAutoCancel(true).setContentTitle("Entered In Geofence!").setContentText("Click here for return to app!!").setContentIntent(pendingIntent).build()
var notiManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notId = notId + 1
notiManager.notify(notId,notification)
Log.e("JK-->>", "Entered in geofence")
}
else if(geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT)
{
var notiBuilder = NotificationCompat.Builder(this).setAutoCancel(true).setContentTitle("Entered In Geofence").setContentText("Click Here for return to app!").build()
var notiManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
var intent = Intent(this,MainActivity::class.java)
var pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT)
notiBuilder.contentIntent = pendingIntent
notId = notId + 1
notiManager.notify(notId,notiBuilder)
Log.e("JK-->>", "Entered in geofence")
Log.e("JK-->>", "Exit from geofence")
}
}}
I had also checked that my service is called when the device enters or exit from geofence by putting debug points. No Object gets null value and everything goes fine but the notification does not receive. How can I do??