Kotlin Service實(shí)現(xiàn)消息推送通知過程
四大組件,就剩下最后一個(gè)Service ,他比較重要,相當(dāng)于后臺(tái)服務(wù),基本上大部分的app,都會(huì)有一兩個(gè)這樣的服務(wù)Service 。
Service用處非常的多,可以根據(jù)后臺(tái)的特性來決定Service的用法。
Service 的使用也非常的簡單,簡單的建立和綁定,就能完成Service的動(dòng)作。
建立Service
這里我們創(chuàng)建一個(gè)Service,用它來發(fā)送消息服務(wù),這里從服務(wù)的建立和用Binder 來綁定服務(wù),這樣可以建立起Service 和Activity之間的通訊問題。
建立一個(gè)
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 是一個(gè)空的,什么任務(wù)也沒有,為他加一個(gè)簡單的任務(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)擊以后,會(huì)回到我們app的ResultActivity 中,只要在程序中把ResultActivity 實(shí)現(xiàn)為自己的邏輯,就能調(diào)整到ResultActivity 頁面中。
綁定服務(wù)
啟動(dòng)服務(wù)可以有兩種方法,一種是直接啟動(dòng),一種還要進(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)一個(gè)一個(gè)的進(jìn)行了簡單的介紹,你會(huì)慢慢的了解到安卓開發(fā)中主要的組件形式和使用的方法,后面還會(huì)慢慢的安卓的其他的特性進(jìn)行介紹。這四大組件非常的重要,可以在其他的demo中注意這四個(gè)組件的用法,對開發(fā)程序會(huì)有很大的幫助。
到此這篇關(guān)于Kotlin Service實(shí)現(xiàn)消息推送通知過程的文章就介紹到這了,更多相關(guān)Kotlin Service消息推送內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android app會(huì)crash的原因及解決方法
這篇文章主要介紹了Android app會(huì)crash的原因及解決方法,幫助大家更好的進(jìn)行Android開發(fā),感興趣的朋友可以了解下2020-12-12
解決Android從相冊中獲取圖片出錯(cuò)圖片卻無法裁剪問題的方法
這篇文章主要介紹了解決Android從相冊中獲取圖片出錯(cuò)圖片卻無法裁剪問題的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-01-01
Android編程實(shí)現(xiàn)wifi掃描及連接的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)wifi掃描及連接的方法,涉及Android網(wǎng)絡(luò)操作掃描、查找、連接、線程等相關(guān)操作技巧,需要的朋友可以參考下2018-02-02
kotlin協(xié)程之coroutineScope函數(shù)使用詳解
這篇文章主要為大家介紹了kotlin協(xié)程之coroutineScope函數(shù)使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Android編程實(shí)現(xiàn)自動(dòng)檢測版本及自動(dòng)升級的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)自動(dòng)檢測版本及自動(dòng)升級的方法,涉及Android版本檢測,匹配,下載及自動(dòng)安裝等技巧,需要的朋友可以參考下2016-01-01
兩分鐘讓你徹底明白Android Activity生命周期的詳解(圖文介紹)
本篇文章是對Android的生命周期進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android樣式的開發(fā):layer-list實(shí)例詳解
本文主要介紹Android樣式開發(fā)layer-list,這里整理了詳細(xì)的資料,及簡單示例代碼有興趣的小伙伴可以參考下2016-09-09

