如何使用 Kotlin 在 Android 中从 IntentService 发送通知
How to send notification from IntentService in Android using Kotlin
我是 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")
}
}}
我还通过放置调试点检查了当设备进入或退出地理围栏时我的服务被调用。没有对象获得空值,一切正常,但没有收到通知。
我该怎么办??
您需要在您的通知中添加图标,否则您的通知将被触发但不会显示在状态栏上。
但是为什么呢?因为在 Required notification contents
下显示通知是 android OS 的最低要求之一
通知对象必须包含以下内容:
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.notificationDrawableResource)
// add ^^^^^^^
.setContentIntent(pendingIntent).build()
注意:根据棒棒糖标准,您的通知图标应该是透明的,您可以使用 setColor
设置背景颜色,但您必须按照 [=31] 勾选调用 setColor
=] OS
我是 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")
}
}}
我还通过放置调试点检查了当设备进入或退出地理围栏时我的服务被调用。没有对象获得空值,一切正常,但没有收到通知。 我该怎么办??
您需要在您的通知中添加图标,否则您的通知将被触发但不会显示在状态栏上。
但是为什么呢?因为在 Required notification contents
通知对象必须包含以下内容:
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.notificationDrawableResource)
// add ^^^^^^^
.setContentIntent(pendingIntent).build()
注意:根据棒棒糖标准,您的通知图标应该是透明的,您可以使用 setColor
设置背景颜色,但您必须按照 [=31] 勾选调用 setColor
=] OS