欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Kotlin Service實(shí)現(xiàn)消息推送通知過程

 更新時間:2022年12月05日 10:27:16   作者:go2coding  
這幾天分析了一下的啟動過程,于是乎,今天寫一下Service使用; 給我的感覺是它并不復(fù)雜,千萬不要被一坨一坨的代碼嚇住了,雖然彎彎繞繞不少,重載函數(shù)一個接著一個,就向走迷宮一樣,但只要抓住主線閱讀,很快就能找到出口

四大組件,就剩下最后一個Service ,他比較重要,相當(dāng)于后臺服務(wù),基本上大部分的app,都會有一兩個這樣的服務(wù)Service 。

Service用處非常的多,可以根據(jù)后臺的特性來決定Service的用法。

Service 的使用也非常的簡單,簡單的建立和綁定,就能完成Service的動作。

建立Service

這里我們創(chuàng)建一個Service,用它來發(fā)送消息服務(wù),這里從服務(wù)的建立和用Binder 來綁定服務(wù),這樣可以建立起ServiceActivity之間的通訊問題。

建立一個

    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)擊以后,會回到我們appResultActivity 中,只要在程序中把ResultActivity 實(shí)現(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實(shí)現(xiàn)消息推送通知過程的文章就介紹到這了,更多相關(guān)Kotlin Service消息推送內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論