Jaydip Kalkani Jaydip Kalkani - 3 years ago 323
Android Question

How to send notification from IntentService in Android using Kotlin

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)
Log.e("JK-->>","geofencingError -->> "+geofencingEvent.hasError())

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

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

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??

Answer Source

You need add icon in your notification otherwise your notification will be triggered but won't be displayed on the status bar.

But why? Because it is one of the minimum requirement by android OS to display notification under Required notification contents

A Notification object must contain the following:

A small icon, set by setSmallIcon().

A title, set by setContentTitle().

Detail text, set by setContentText().

On Android 8.0 (API level 26) and higher, a valid notification channel ID, set by setChannelId() or provided in the NotificationCompat.Builder constructor when creating a channel.

var notification = NotificationCompat.Builder(applicationContext)
                   .setContentTitle("Entered In Geofence!")
                   .setContentText("Click here for return to app!!")
                   // add ^^^^^^^

Note : As per lollipop standard , your notification icon should be transparent and you can use setColor to set background color but you have to put a check to call setColor as per android OS

Notification bar icon turns white in Android 5 Lollipop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download