0

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

Jaydip Kalkani
  • 2,493
  • 6
  • 24
  • 56

1 Answers1

3

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)
                   .setAutoCancel(true)
                   .setContentTitle("Entered In Geofence!")
                   .setContentText("Click here for return to app!!")
                   .setSmallIcon(R.drawable.notificationDra‌​wableResource)
                   // add ^^^^^^^
                   .setContentIntent(pendingIntent).build()

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

Community
  • 1
  • 1
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • Ohk. Thanks but now i receive notification but when i click on that then nothing happens. It should start my MainActivity. – Jaydip Kalkani Oct 01 '17 at 17:04
  • Probably because you create a pending intent for a service and not for an activity. – Christopher Oct 01 '17 at 17:08
  • @JaydipKalkani i guess that is due using same id for `getActivity` and `getService` with `FLAG_UPDATE_CURRENT` to effect changes in already displayed notification (which starts service) so try changing id `PendingIntent.getActivity(this,10,intent,PendingIntent.FLAG_UPDATE_CURRENT)` – Pavneet_Singh Oct 01 '17 at 17:09
  • 1
    Nice Explaination @Pavneet_Singh +1 – UltimateDevil Oct 01 '17 at 17:11
  • should i generate new id every time when i create PendingIntent Object or i can use same id every time for entering in geofence and another id at time of exiting from geofence? – Jaydip Kalkani Oct 01 '17 at 17:12
  • since you want two actions so either you can have two different notification with different ids or you can try using [FLAG_CANCEL_CURRENT](https://developer.android.com/reference/android/app/PendingIntent.html#FLAG_ONE_SHOT) and @VikasTiwari thanks for appreciation – Pavneet_Singh Oct 01 '17 at 17:18
  • i tried both solutions but it's not working.@pavneet_singh – Jaydip Kalkani Oct 01 '17 at 17:51
  • is your `MainActivity` is service as well as activity? because in both cases you are using `MainActivity` for service and activity both , i guess they should be different – Pavneet_Singh Oct 01 '17 at 18:21
  • Its my pleasure @Pavneet_Singh we beginner get to learn from you :) – UltimateDevil Oct 01 '17 at 18:32
  • can't get it. My MainActivity is Activity and GeofenceIntentService is service and i don't have any service named MainActivity in my whole project. – Jaydip Kalkani Oct 01 '17 at 18:35
  • can you explain me in brief please? @pavneet_singh – Jaydip Kalkani Oct 01 '17 at 18:35
  • @JaydipKalkani then change `PendingIntent.getService(applicationContext,0,Intent` to `PendingIntent.getActivity(applicationContext,0,Intent` – Pavneet_Singh Oct 01 '17 at 18:38
  • yes!! I just got it!! It Works! Thank you very much! @Pavneet_singh – Jaydip Kalkani Oct 01 '17 at 18:40
  • i am glad that i could help , happy coding and @VikasTiwari that's what SO community is all about , we learn from each other :) – Pavneet_Singh Oct 01 '17 at 18:43