Kotlin Service實現(xiàn)消息推送通知過程
四大組件,就剩下最后一個Service ,他比較重要,相當(dāng)于后臺服務(wù),基本上大部分的app,都會有一兩個這樣的服務(wù)Service 。
Service用處非常的多,可以根據(jù)后臺的特性來決定Service的用法。
Service 的使用也非常的簡單,簡單的建立和綁定,就能完成Service的動作。
建立Service
這里我們創(chuàng)建一個Service,用它來發(fā)送消息服務(wù),這里從服務(wù)的建立和用Binder 來綁定服務(wù),這樣可以建立起Service 和Activity之間的通訊問題。
建立一個
internal class MyBinder(private val service: NotificationService) : Binder() {
fun getService() : NotificationService{
return service
}
}MyBinder 是我們的中間人,我們需要通過它來找到真正的Service。
NotificationService 如下:
class NotificationService : Service() {
private lateinit var mNotification: Notification
private val mNotificationId: Int = 1000
private var mBinder = MyBinder(this@NotificationService)
companion object {
const val CHANNEL_ID = "com.kotlin.kotlin_start_ch18.CHANNEL_ID"
const val CHANNEL_NAME = "Sample Notification"
}
override fun onBind(intent: Intent): IBinder {
return mBinder
}這里NotificationService 是一個空的,什么任務(wù)也沒有,為他加一個簡單的任務(wù),就是消息推送通知。
@SuppressLint("NewApi")
private fun createChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
val context = this.applicationContext
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val importance = NotificationManager.IMPORTANCE_HIGH
val notificationChannel = NotificationChannel(CHANNEL_ID, CHANNEL_NAME, importance)
notificationChannel.enableVibration(true)
notificationChannel.setShowBadge(true)
notificationChannel.enableLights(true)
notificationChannel.lightColor = Color.parseColor("#e8334a")
notificationChannel.description = getString(R.string.notification_channel_description)
notificationChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
notificationManager.createNotificationChannel(notificationChannel)
}
}通過上面的代碼,NotificationService 就有了自己的事情做了,可以通過notifyMessage()
public fun notifyMessage(){
//Create Channel
createChannel()
val context = this.applicationContext
var notificationManager: NotificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notifyIntent = Intent(this, ResultActivity::class.java)
val title = "Sample Notification"
val message =
"You have received a sample notification. This notification will take you to the details page."
notifyIntent.putExtra("title", title)
notifyIntent.putExtra("message", message)
notifyIntent.putExtra("notification", true)
notifyIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
val pendingIntent =
PendingIntent.getActivity(context, 0, notifyIntent, PendingIntent.FLAG_IMMUTABLE)
val res = this.resources
val uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mNotification = Notification.Builder(this, CHANNEL_ID)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_stat_name)
.setLargeIcon(BitmapFactory.decodeResource(res, R.mipmap.ic_launcher))
.setAutoCancel(true)
.setContentTitle(title)
.setStyle(
Notification.BigTextStyle()
.bigText(message)
)
.setContentText(message).build()
} else {
mNotification = Notification.Builder(this)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_stat_name)
.setLargeIcon(BitmapFactory.decodeResource(res, R.mipmap.ic_launcher))
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_MAX)
.setContentTitle(title)
.setStyle(
Notification.BigTextStyle()
.bigText(message)
)
.setSound(uri)
.setContentText(message).build()
}
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// mNotificationId is a unique int for each notification that you must define
notificationManager.notify(mNotificationId, mNotification)
}當(dāng)我們發(fā)送的通知消息被點(diǎn)擊以后,會回到我們app的ResultActivity 中,只要在程序中把ResultActivity 實現(xiàn)為自己的邏輯,就能調(diào)整到ResultActivity 頁面中。
綁定服務(wù)
啟動服務(wù)可以有兩種方法,一種是直接啟動,一種還要進(jìn)行相應(yīng)的綁定。
val service = Intent(this@MainActivity, NotificationService::class.java)
service.putExtra("reason", intent.getStringExtra("reason"))
service.putExtra("timestamp", intent.getLongExtra("timestamp", 0))
service.data = Uri.parse("custom://" + System.currentTimeMillis())
startService(service)我們需要和Service 進(jìn)行通訊,所以我們采用綁定的方式。
private fun bindService() {
connection = object : ServiceConnection {
override fun onServiceConnected(name: ComponentName, service: IBinder) {
binder = service as NotificationService.MyBinder
}
override fun onServiceDisconnected(name: ComponentName) {}
}
val intent = Intent(this, NotificationService::class.java)
startService(intent)
bindService(intent, connection as ServiceConnection, Context.BIND_AUTO_CREATE)
}
如上,我們可以通過服務(wù),發(fā)送通知消息了。

小結(jié)
四大組件,我們已經(jīng)一個一個的進(jìn)行了簡單的介紹,你會慢慢的了解到安卓開發(fā)中主要的組件形式和使用的方法,后面還會慢慢的安卓的其他的特性進(jìn)行介紹。這四大組件非常的重要,可以在其他的demo中注意這四個組件的用法,對開發(fā)程序會有很大的幫助。
到此這篇關(guān)于Kotlin Service實現(xiàn)消息推送通知過程的文章就介紹到這了,更多相關(guān)Kotlin Service消息推送內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android自定義View的使用及其原理知識點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是關(guān)于Android自定義View的使用及其原理知識點(diǎn)總結(jié)內(nèi)容,需要的朋友們可以學(xué)習(xí)下。2019-08-08
Android實現(xiàn)瘋狂連連看游戲之加載界面圖片和實現(xiàn)游戲Activity(四)
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)瘋狂連連看游戲之加載界面圖片和實現(xiàn)游戲Activity,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03
Android開發(fā)實現(xiàn)多進(jìn)程彈窗效果
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)實現(xiàn)多進(jìn)程彈窗效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10
Android仿Iphone屏幕底部彈出半透明PopupWindow效果
這篇文章主要為大家詳細(xì)介紹了Android仿Iphone屏幕底部彈出半透明PopupWindow效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
詳細(xì)講解Android中使用LoaderManager加載數(shù)據(jù)的方法
這篇文章主要介紹了Android中使用LoaderManager加載數(shù)據(jù)的方法,講到了LoaderManager的異步加載與聲明周期的管理等相關(guān)用法,需要的朋友可以參考下2016-04-04

