如何使用Kotlin从Android中的IntentService发送通知

如何使用Kotlin从Android中的IntentService发送通知,android,notifications,kotlin,intentservice,Android,Notifications,Kotlin,Intentservice,我是android开发新手,我想在设备进入用户设置的任何特定地理围栏时,从IntentService发送通知。我已经为发送通知编写了以下代码 class GeofenceIntentService : IntentService("GeofenceIntentService") { var notId: Int = 15452 override fun onHandleIntent(intent: Intent?) { var geofencingEvent = GeofencingE

我是android开发新手,我想在设备进入用户设置的任何特定地理围栏时,从IntentService发送通知。我已经为发送通知编写了以下代码

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")
    }
}}
我还通过设置调试点检查了设备进入或退出geofence时是否调用了我的服务。没有对象获得空值,一切正常,但通知未收到。
如何操作???

您需要在通知中添加图标,否则您的通知将被触发,但不会显示在状态栏上

但是为什么呢?因为它是安卓操作系统的最低要求之一,在以下情况下显示通知:

通知对象必须包含以下内容:

由setSmallIcon()设置的小图标

由setContentTitle()设置的标题

详细信息文本,由setContentText()设置

在Android 8.0(API级别26)及更高版本上,由setChannelId()设置的或在创建通道时在NotificationCompat.Builder构造函数中提供的有效通知通道ID

注意:根据棒棒糖标准,您的通知图标应该是透明的,您可以使用
setColor
设置背景色,但根据android操作系统,您必须勾选调用
setColor


您需要在通知中添加图标,否则您的通知将被触发,但不会显示在状态栏上

但是为什么呢?因为它是安卓操作系统的最低要求之一,在以下情况下显示通知:

通知对象必须包含以下内容:

由setSmallIcon()设置的小图标

由setContentTitle()设置的标题

详细信息文本,由setContentText()设置

在Android 8.0(API级别26)及更高版本上,由setChannelId()设置的或在创建通道时在NotificationCompat.Builder构造函数中提供的有效通知通道ID

注意:根据棒棒糖标准,您的通知图标应该是透明的,您可以使用
setColor
设置背景色,但根据android操作系统,您必须勾选调用
setColor


尝试添加
.setAutoCancel(true)。setSmallIcon(R.drawable.notificationdrawable资源)
已添加setAutoCancel(true)。让我尝试使用setSmallIcon()。我用它作为提示:PYes!你的暗示很管用,兄弟!谢谢但是你能给我解释一下为什么它不使用默认图标作为通知,为什么需要显式地设置它@pavneet Singhtry添加
.setAutoCancel(true).setMallicon(R.drawable.notificationDrawableResource)
setAutoCancel(true)已添加。让我尝试使用setSmallIcon()。我用它作为提示:PYes!你的暗示很管用,兄弟!谢谢但是你能给我解释一下为什么它不使用默认图标作为通知,为什么需要显式地设置它@pavneet SinghOhk。谢谢,但现在我收到了通知,但当我点击它时,什么也没有发生。它应该启动我的MainActivity。可能是因为您为服务而不是活动创建了一个挂起的意图。@JaydipKalkani我想这是由于对
getActivity
getService
使用了
FLAG\u UPDATE\u CURRENT
的相同id,以对已经显示的通知(启动服务)进行更改因此,请尝试更改id
PendingIntent.getActivity(this,10,intent,PendingIntent.FLAG_UPDATE_CURRENT)
Nice explainion@Pavneet_Singh+1我应该在每次创建PendingIntent对象时生成新id,还是可以在每次进入地理围栏时使用相同的id,在退出地理围栏时使用另一个id?Ohk。谢谢,但现在我收到了通知,但当我点击它时,什么也没有发生。它应该启动我的MainActivity。可能是因为您为服务而不是活动创建了一个挂起的意图。@JaydipKalkani我想这是由于对
getActivity
getService
使用了
FLAG\u UPDATE\u CURRENT
的相同id,以对已经显示的通知(启动服务)进行更改因此,请尝试更改id
PendingIntent.getActivity(this,10,intent,PendingIntent.FLAG\u UPDATE\u CURRENT)
Nice explainion@Pavneet\u Singh+1我应该在每次创建PendingIntent对象时生成新id,还是可以在每次进入geofence时使用相同的id,在退出geofence时使用另一个id?
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()