Android 通知欄使用

不同版本通知欄的創(chuàng)建方式不盡相同,當(dāng)前官方推薦使用 NotificationCompat 相關(guān)的API,兼容到Android 4.0,但是部分新功能,比如內(nèi)嵌回復(fù)操作,舊版本是無法支持的。

一、設(shè)置通知內(nèi)容

    //CHANNEL_ID,渠道ID,Android 8.0及更高版本必須要設(shè)置
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
            //設(shè)置小圖標(biāo)
            .setSmallIcon(R.drawable.notification_icon)
            //設(shè)置標(biāo)題
            .setContentTitle(textTitle)
            //設(shè)置內(nèi)容
            .setContentText(textContent)
            //設(shè)置等級
            .setPriority(NotificationCompat.PRIORITY_DEFAULT);

二、創(chuàng)建渠道

在 Android 8.0 及更高版本上提供通知,需要在系統(tǒng)中注冊應(yīng)用的通知渠道。

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = getString(R.string.channel_name);
            String description = getString(R.string.channel_description);
            //不同的重要程度會影響通知顯示的方式
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
            channel.setDescription(description);

            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }

上述代碼應(yīng)該在應(yīng)用啟動時立即執(zhí)行,可以放在 Application 中進(jìn)行初始化。

三、設(shè)置通知欄的點擊操作

一般點擊通知欄會打開對應(yīng)的 Activity 界面,具體代碼如下:

    //點擊時想要打開的界面
    Intent intent = new Intent(this, AlertDetails.class);
    //一般點擊通知都是打開獨立的界面,為了避免添加到現(xiàn)有的activity棧中,可以設(shè)置下面的啟動方式
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    //創(chuàng)建activity類型的pendingIntent,還可以創(chuàng)建廣播等其他組件
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.drawable.notification_icon)
            .setContentTitle("My notification")
            .setContentText("Hello World!")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            //設(shè)置pendingIntent
            .setContentIntent(pendingIntent)
            //設(shè)置點擊后是否自動消失
            .setAutoCancel(true);    

四、顯示通知

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    //notificationId 相當(dāng)于通知的唯一標(biāo)識,用于更新或者移除通知
    notificationManager.notify(notificationId, builder.build());

還有很多特殊功能,可以直接查看官網(wǎng)教程進(jìn)行設(shè)置。

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

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

  • 由于歷史原因,Android在發(fā)布之初對通知欄Notification的設(shè)計相當(dāng)簡單,而如今面對各式各樣的通知欄玩...
    Linhaojian閱讀 5,707評論 0 43
  • 最近做直播,要求向關(guān)注者發(fā)通知,顯示在通知欄, 記錄下簡單的使用。 并發(fā)現(xiàn)一個在魅族手機(jī)上奇葩的坑。。。 直接上代...
    itkluo88閱讀 7,500評論 0 2
  • 原創(chuàng)微信公眾號郭霖 WeChat ID: guolin_blog 對于通知欄的使用,Android各個版本其實都有...
    木木00閱讀 949評論 1 11
  • 1.動態(tài)注冊廣播無法觸發(fā)點擊事件 場景:通知欄的點擊事件通常會采用PendingIntent.getBroadca...
    JohnsonZZZ閱讀 11,184評論 2 32
  • 推薦指數(shù): 6.0 書籍主旨關(guān)鍵詞:特權(quán)、焦點、注意力、語言聯(lián)想、情景聯(lián)想 觀點: 1.統(tǒng)計學(xué)現(xiàn)在叫數(shù)據(jù)分析,社會...
    Jenaral閱讀 5,967評論 0 5

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