Android通知 Notification的簡(jiǎn)單使用

在Android應(yīng)用的開發(fā)中,必然會(huì)遇上通知的開發(fā)需求,本文主要講一下Android中的通知 Notification的簡(jiǎn)單基本使用,主要包含創(chuàng)建通知渠道、初始化通知、顯示通知、顯示圖片通知、通知點(diǎn)擊、以及配合WorkManager發(fā)送延遲通知。

Demo下載

創(chuàng)建通知渠道

首先,創(chuàng)建幾個(gè)常量和變量,其中渠道名是會(huì)顯示在手機(jī)設(shè)置-通知里app對(duì)應(yīng)展示的通知渠道名稱,一般基于通知作用取名。

    companion object {
        //渠道Id
        private const val CHANNEL_ID = "渠道Id"

        //渠道名
        private const val CHANNEL_NAME = "渠道名-簡(jiǎn)單通知"

        //渠道重要級(jí)
        private const val CHANNEL_IMPORTANCE = NotificationManager.IMPORTANCE_DEFAULT
    }

    private lateinit var context: Context

    //Notification的ID
    private var notifyId = 100
    private lateinit var manager: NotificationManager
    private lateinit var builder: NotificationCompat.Builder

然后獲取系統(tǒng)通知服務(wù),創(chuàng)建通知渠道,其中因?yàn)橥ㄖ朗茿ndroid8.0才有的,所以增加一個(gè)版本判斷:

        //獲取系統(tǒng)通知服務(wù)
        manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        //創(chuàng)建通知渠道,Android8.0及以上需要
        createChannel()
    private fun createChannel() {
        //創(chuàng)建通知渠道,Android8.0及以上需要
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            return
        }
        val notificationChannel = NotificationChannel(
            CHANNEL_ID,
            CHANNEL_NAME,
            CHANNEL_IMPORTANCE
        )
        manager.createNotificationChannel(notificationChannel)
    }

初始化通知

先生成NotificationCompat.Builder,然后初始化通知Builder的通用配置:

        builder = NotificationCompat.Builder(context.applicationContext, CHANNEL_ID)
        initNotificationBuilder()
    /**
     * 初始化通知Builder的通用配置
     */
    private fun initNotificationBuilder() {
        builder
            .setAutoCancel(true) //設(shè)置這個(gè)標(biāo)志當(dāng)用戶單擊面板就可以讓通知自動(dòng)取消
            .setSmallIcon(R.drawable.ic_reminder) //通知的圖標(biāo)
            .setWhen(System.currentTimeMillis()) //通知產(chǎn)生的時(shí)間,會(huì)在通知信息里顯示
            .setDefaults(Notification.DEFAULT_ALL)
    }

此外builder還有setVibrate、setSound、setStyle等方法,按需配置即可。

顯示通知

給builder設(shè)置需要通知需要顯示的title和content,然后通過builder.build()生成生成通知Notification,manager.notify()方法將通知發(fā)送出去。

    fun configNotificationAndSend(title: String, content: String){
        builder.setContentTitle(title)
            .setContentText(content)
        val notification = builder.build()
        //發(fā)送通知
        manager.notify(notifyId, notification)
        //id自增
        notifyId++
    }

最簡(jiǎn)單的通知顯示至此上面三步就完成了。
效果如下圖:


image.png

顯示圖片通知

當(dāng)通知內(nèi)容過多一行展示不下時(shí),可以通過設(shè)置

builder.setStyle(NotificationCompat.BigTextStyle().bigText(content)) //設(shè)置可以顯示多行文本

這樣通知就能收縮和展開,顯示多行文本。
另外setStyle還可以設(shè)置圖片形式的通知:

setStyle(NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(resources,R.drawable.logo)))//設(shè)置圖片樣式

效果如下圖:


image.png

通知點(diǎn)擊

目前為止的通知還只是顯示,因?yàn)樵O(shè)置了builder.setAutoCancel(true),點(diǎn)擊通知之后通知會(huì)自動(dòng)消失,除此之外還沒有其他操作。
給builder設(shè)置setContentIntent(PendingIntent)就能有通知點(diǎn)擊之后的其他操作了。PendingIntent可以看作是對(duì)Intent的一個(gè)封裝,但它不是立刻執(zhí)行某個(gè)行為,而是滿足某些條件或觸發(fā)某些事件后才執(zhí)行指定的行為。PendingIntent獲取有三種方式:Activity、Service和BroadcastReceiver獲取。通過對(duì)應(yīng)方法PendingIntent.getActivity、PendingIntent.getBroadcast、PendingIntent.getService就能獲取。
這里就示例一下PendingIntent.getBroadcast和PendingIntent.getActivity

PendingIntent.getBroadcast

首先創(chuàng)建一個(gè)BroadcastReceiver:

class NotificationHandleReceiver : BroadcastReceiver() {
    companion object {
        const val NOTIFICATION_HANDLE_ACTION = "notification_handle_action"
        const val NOTIFICATION_LINK = "notificationLink"
        const val TAG = "NotificationReceiver"
    }

    override fun onReceive(context: Context, intent: Intent?) {
        if (intent?.action == NOTIFICATION_HANDLE_ACTION) {
            val link = intent.getStringExtra(NOTIFICATION_LINK)
        }
    }
}

別忘了在清單文件中還需要靜態(tài)注冊(cè)BroadcastReceiver:

    <receiver
        android:name=".NotificationHandleReceiver"
        android:exported="false">
        <intent-filter>
            <action android:name="notification_handle_action" />
        </intent-filter>
    </receiver>

然后創(chuàng)建一個(gè)上面BroadcastReceiver的Intent,在intent.putExtra傳入相應(yīng)的點(diǎn)擊通知之后需要識(shí)別的操作:

   fun generateDefaultBroadcastPendingIntent(linkParams: (() -> String)?): PendingIntent {
        val intent = Intent(NotificationHandleReceiver.NOTIFICATION_HANDLE_ACTION)
        intent.setPackage(context.packageName)
        linkParams?.let {
            val params = it.invoke()
            intent.putExtra(NotificationHandleReceiver.NOTIFICATION_LINK, params)
        }
        return PendingIntent.getBroadcast(
            context,
            notifyId,
            intent,
            PendingIntent.FLAG_IMMUTABLE
        )
    }

這樣生成的PendingIntent再builder.setContentIntent(pendingIntent),在我們點(diǎn)擊通知之后,NotificationHandleReceiver的onReceive里就會(huì)收到信息了,根據(jù)信息處理后續(xù)操作即可。

PendingIntent. getActivity

Activity的PendingIntent用于跳轉(zhuǎn)到指定activity,創(chuàng)建一個(gè)跳轉(zhuǎn)activity的Intent(同普通的頁面跳轉(zhuǎn)的Intent),也是同上面在intent.putExtra傳入相應(yīng)的點(diǎn)擊通知之后需要識(shí)別的操作:

        val intent = Intent(this, XXXX::class.java).apply {
            putExtra("title", title).putExtra("content", content)
        }
        return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)

也是這樣生成的PendingIntent再builder.setContentIntent(pendingIntent),在我們點(diǎn)擊通知之后,就會(huì)跳轉(zhuǎn)到對(duì)應(yīng)的activity頁面,然后intent里就會(huì)收到信息了,根據(jù)信息處理后續(xù)操作即可。

Android12之PendingIntent特性

行為變更:以 Android 12 為目標(biāo)平臺(tái)的應(yīng)用
查看上面關(guān)于Android12的特性
在Android12平臺(tái)上有關(guān)于PendingIntent的兩點(diǎn)特性:

  • 一是待處理 intent 可變性,必須為應(yīng)用創(chuàng)建的每個(gè) PendingIntent 對(duì)象指定可變性,這也是上面創(chuàng)建PendingIntent時(shí)需要設(shè)置flag為PendingIntent.FLAG_IMMUTABLE。
  • 二是通知 trampoline 限制,以 Android 12 或更高版本為目標(biāo)平臺(tái)的應(yīng)用無法從用作通知 trampoline 的服務(wù)廣播接收器中啟動(dòng) activity。換言之,當(dāng)用戶點(diǎn)按通知或通知中的操作按鈕時(shí),您的應(yīng)用無法在服務(wù)或廣播接收器內(nèi)調(diào)用 startActivity()。所以當(dāng)需要點(diǎn)擊通知實(shí)現(xiàn)activity跳轉(zhuǎn)時(shí),需要使用PendingIntent. getActivity,而不是使用PendingIntent.getBroadcast,然后在BroadcastReceiver里實(shí)現(xiàn)activity跳轉(zhuǎn),后者方式在Android 12 或更高版本為目標(biāo)平臺(tái)的應(yīng)用中將被限制。

配合WorkManager發(fā)送延遲通知

配合上WorkManager,就能實(shí)現(xiàn)發(fā)送延遲通知,主要是通過OneTimeWorkRequest的延遲特性。
創(chuàng)建一個(gè)延遲的OneTimeWorkRequest,加入WorkManager隊(duì)列中:

    fun sendWorkRequest(
        context: Context,
        reminderId: Int,
        title: String,
        content: String,
        link: String,
        triggerTime: Long
    ): OneTimeWorkRequest {
        val duration = triggerTime - System.currentTimeMillis()
        val data =
            Data.Builder().putInt(REMINDER_WORKER_DATA_ID, reminderId).putString(REMINDER_WORKER_DATA_TITLE, title)
                .putString(REMINDER_WORKER_DATA_CONTENT, content).putString(REMINDER_WORKER_DATA_LINK, link)
                .build()
        val uniqueWorkName =
            "reminderData_${reminderId}"
        val request = OneTimeWorkRequest.Builder(ReminderWorker::class.java)
            .setInitialDelay(duration, TimeUnit.MILLISECONDS)
            .setInputData(data)
            .build()
        WorkManager.getInstance(context)
            .enqueueUniqueWork(uniqueWorkName, ExistingWorkPolicy.REPLACE, request)
        return request
    }

然后在doWork方法中拿到數(shù)據(jù)進(jìn)行我們上面的通知發(fā)送顯示即可。具體關(guān)于OneTimeWorkRequest的使用在本文中就不詳細(xì)說明了。當(dāng)需要發(fā)送延遲通知時(shí),知道可以通過配合WorkManager實(shí)現(xiàn)。

Android13 通知權(quán)限

在目前最新的Android 13(API 級(jí)別 33)上對(duì)于通知增加了權(quán)限限制,具體可看官方描述:
通知運(yùn)行時(shí)權(quán)限

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容