需要全部源碼或運行有問題請點贊關注收藏后評論區(qū)留言~~~
一、通知渠道NtoificationChannel
為了分清消息通知的輕重緩急,Android8.0新增了通知渠道,并且必須指定通知渠道才能正常推送消息,一個應用允許擁有多個通知渠道,每個渠道的重要性各不相同,有的渠道消息在通知欄被折疊成小行,有的渠道消息在通知欄展示完整的大行,有的會發(fā)出鈴聲甚至震動等等
效果如下 可以輸入標題和內(nèi)容,然后下拉框里面選擇消息的重要級別,根據(jù)重要級別的不同消息會有不同的通知方式(這里要連接真機才能具體展示 讀者可自行連接 或者參考我之前的博客連接步驟)
?
?
?代碼如下
Java類
package com.example.chapter11;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.chapter11.util.NotifyUtil;
import com.example.chapter11.util.ViewUtil;
public class NotifyChannelActivity extends AppCompatActivity implements View.OnClickListener {
private EditText et_title;
private EditText et_message;
private String mChannelId = "0"; // 通知渠道的編號
private String mChannelName; // 通知渠道的名稱
private int mImportance; // 通知渠道的級別
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notify_channel);
et_title = findViewById(R.id.et_title);
et_message = findViewById(R.id.et_message);
findViewById(R.id.btn_send_channel).setOnClickListener(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
initImportanceSpinner(); // 初始化渠道級別的下拉框
}
}
// 初始化渠道級別的下拉框
private void initImportanceSpinner() {
findViewById(R.id.ll_channel).setVisibility(View.VISIBLE);
ArrayAdapter<String> importanceAdapter = new ArrayAdapter<String>(this,
R.layout.item_select, importanceDescArray);
Spinner sp_importance = findViewById(R.id.sp_importance);
sp_importance.setPrompt("請選擇渠道級別");
sp_importance.setAdapter(importanceAdapter);
sp_importance.setSelection(3);
sp_importance.setOnItemSelectedListener(new TypeSelectedListener());
}
private int[] importanceTypeArray = {NotificationManager.IMPORTANCE_NONE,
NotificationManager.IMPORTANCE_MIN,
NotificationManager.IMPORTANCE_LOW,
NotificationManager.IMPORTANCE_DEFAULT,
NotificationManager.IMPORTANCE_HIGH,
NotificationManager.IMPORTANCE_MAX};
private String[] importanceDescArray = {"不重要", // 無通知
"最小級別", // 通知欄折疊,無提示聲音,無鎖屏通知
"有點重要", // 通知欄展開,無提示聲音,有鎖屏通知
"一般重要", // 通知欄展開,有提示聲音,有鎖屏通知
"非常重要", // 通知欄展開,有提示聲音,有鎖屏通知,在屏幕頂部短暫懸?。ㄓ械氖謾C需要在設置頁面開啟橫幅)
"最高級別" // 通知欄展開,有提示聲音,有鎖屏通知,在屏幕頂部短暫懸?。ㄓ械氖謾C需要在設置頁面開啟橫幅)
};
class TypeSelectedListener implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
mImportance = importanceTypeArray[arg2];
mChannelId = "" + arg2;
mChannelName = importanceDescArray[arg2];
}
public void onNothingSelected(AdapterView<?> arg0) {
}
}
// 發(fā)送指定渠道的通知消息(包括消息標題和消息內(nèi)容)
private void sendChannelNotify(String title, String message) {
// 創(chuàng)建一個跳轉(zhuǎn)到活動頁面的意圖
Intent clickIntent = new Intent(this, MainActivity.class);
// 創(chuàng)建一個用于頁面跳轉(zhuǎn)的延遲意圖
PendingIntent contentIntent = PendingIntent.getActivity(this,
R.string.app_name, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// 創(chuàng)建一個通知消息的建造器
Notification.Builder builder = new Notification.Builder(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Android 8.0開始必須給每個通知分配對應的渠道
builder = new Notification.Builder(this, mChannelId);
}
builder.setContentIntent(contentIntent) // 設置內(nèi)容的點擊意圖
.setAutoCancel(true) // 點擊通知欄后是否自動清除該通知
.setSmallIcon(R.mipmap.ic_launcher) // 設置應用名稱左邊的小圖標
.setContentTitle(title) // 設置通知欄里面的標題文本
.setContentText(message); // 設置通知欄里面的內(nèi)容文本
Notification notify = builder.build(); // 根據(jù)通知建造器構(gòu)建一個通知對象
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotifyUtil.createNotifyChannel(this, mChannelId, mChannelName, mImportance);
}
// 從系統(tǒng)服務中獲取通知管理器
NotificationManager notifyMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// 使用通知管理器推送通知,然后在手機的通知欄就會看到該消息,多條通知需要指定不同的通知編號
notifyMgr.notify(Integer.parseInt(mChannelId), notify);
if (mImportance != NotificationManager.IMPORTANCE_NONE) {
Toast.makeText(this, "已發(fā)送渠道消息", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_send_channel) {
ViewUtil.hideOneInputMethod(this, et_message); // 隱藏輸入法軟鍵盤
if (TextUtils.isEmpty(et_title.getText())) {
Toast.makeText(this, "請?zhí)顚懴祟}", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(et_message.getText())) {
Toast.makeText(this, "請?zhí)顚懴?nèi)容", Toast.LENGTH_SHORT).show();
return;
}
// 發(fā)送指定渠道的通知消息(包括消息標題和消息內(nèi)容)
sendChannelNotify(et_title.getText().toString(), et_message.getText().toString());
}
}
}
XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="消息標題:"
android:textColor="@color/black"
android:textSize="17sp" />
<EditText
android:id="@+id/et_title"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="5dp"
android:background="@drawable/editext_selector"
android:hint="請?zhí)顚懴祟}"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="消息內(nèi)容:"
android:textColor="@color/black"
android:textSize="17sp" />
<EditText
android:id="@+id/et_message"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="top"
android:layout_margin="5dp"
android:background="@drawable/editext_selector"
android:hint="請?zhí)顚懴?nèi)容"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_channel"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="渠道級別:"
android:textColor="@color/black"
android:textSize="17sp" />
<Spinner
android:id="@+id/sp_importance"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="5dp"
android:gravity="center"
android:spinnerMode="dialog" />
</LinearLayout>
<Button
android:id="@+id/btn_send_channel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="發(fā)送渠道消息"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
二、給桌面應用添加信息角標(以華為和小米為例)
每個應用都可以給用戶發(fā)送很多消息,通知欄顯然有時候不夠容納如此之多的消息,于是各應用希望向用戶展現(xiàn)未讀消息的數(shù)量,好讓用戶知曉有沒有未讀消息或者是有幾條未讀消息。例如微信的99+,幾個小紅點好有申請之類。
因為各手機廠商對消息角標的實現(xiàn)方案各不相同,因此只能給它們的手機分別適配處理,
下面依次介紹華為和小米系手機的適配編碼
華為的消息角標不依賴通知推送,允許單獨設置紅點的展示情況
小米的消息角標方案則依賴于通知推送,必須在發(fā)送通知之時一起傳送消息數(shù)量參數(shù)
效果如下 可自行設置
代碼如下文章來源:http://www.zghlxwxcb.cn/news/detail-600752.html
Java類
package com.example.chapter11;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.chapter11.util.NotifyUtil;
import com.example.chapter11.util.ViewUtil;
public class NotifyMarkerActivity extends AppCompatActivity implements View.OnClickListener {
private EditText et_title;
private EditText et_message;
private EditText et_count;
private static String mChannelId = "3"; // 通知渠道的編號
private static String mChannelName = "一般重要"; // 通知渠道的名稱
private static int mImportance = NotificationManager.IMPORTANCE_DEFAULT; // 通知渠道的級別
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notify_marker);
et_title = findViewById(R.id.et_title);
et_message = findViewById(R.id.et_message);
et_count = findViewById(R.id.et_count);
findViewById(R.id.btn_show_marker).setOnClickListener(this);
findViewById(R.id.btn_clear_marker).setOnClickListener(this);
}
@Override
public void onClick(View v) {
ViewUtil.hideOneInputMethod(this, et_message); // 隱藏輸入法軟鍵盤
if (TextUtils.isEmpty(et_title.getText())) {
Toast.makeText(this, "請?zhí)顚懴祟}", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(et_message.getText())) {
Toast.makeText(this, "請?zhí)顚懴?nèi)容", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(et_count.getText())) {
Toast.makeText(this, "請?zhí)顚懴?shù)量", Toast.LENGTH_SHORT).show();
return;
}
String title = et_title.getText().toString();
String message = et_message.getText().toString();
int count = Integer.parseInt(et_count.getText().toString());
if (v.getId() == R.id.btn_show_marker) {
sendChannelNotify(title, message, count); // 發(fā)送指定渠道的通知消息
Toast.makeText(this, "已顯示消息角標,請回到桌面查看", Toast.LENGTH_SHORT).show();
} else if (v.getId() == R.id.btn_clear_marker) {
sendChannelNotify(title, message, 0); // 發(fā)送指定渠道的通知消息
Toast.makeText(this, "已清除消息角標,請回到桌面查看", Toast.LENGTH_SHORT).show();
}
}
// 發(fā)送指定渠道的通知消息(包括消息標題和消息內(nèi)容)
private void sendChannelNotify(String title, String message, int count) {
// 創(chuàng)建一個跳轉(zhuǎn)到活動頁面的意圖
Intent clickIntent = new Intent(this, MainActivity.class);
// 創(chuàng)建一個用于頁面跳轉(zhuǎn)的延遲意圖
PendingIntent contentIntent = PendingIntent.getActivity(this,
R.string.app_name, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// 創(chuàng)建一個通知消息的建造器
Notification.Builder builder = new Notification.Builder(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Android 8.0開始必須給每個通知分配對應的渠道
builder = new Notification.Builder(this, mChannelId);
}
builder.setContentIntent(contentIntent) // 設置內(nèi)容的點擊意圖
.setAutoCancel(true) // 點擊通知欄后是否自動清除該通知
.setSmallIcon(R.mipmap.ic_launcher) // 設置應用名稱左邊的小圖標
.setContentTitle(title) // 設置通知欄里面的標題文本
.setContentText(message); // 設置通知欄里面的內(nèi)容文本
Notification notify = builder.build(); // 根據(jù)通知建造器構(gòu)建一個通知對象
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotifyUtil.createNotifyChannel(this, mChannelId, mChannelName, mImportance);
}
NotifyUtil.showMarkerCount(this, count, notify); // 在桌面的應用圖標右上方顯示指定數(shù)字的消息角標
// 從系統(tǒng)服務中獲取通知管理器
NotificationManager notifyMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// 使用通知管理器推送通知,然后在手機的通知欄就會看到該消息,多條通知需要指定不同的通知編號
notifyMgr.notify(Integer.parseInt(mChannelId), notify);
}
}
?XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="本頁面的消息角標僅支持華為與小米手機"
android:textColor="@color/black"
android:textSize="17sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="消息標題:"
android:textColor="@color/black"
android:textSize="17sp" />
<EditText
android:id="@+id/et_title"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="5dp"
android:background="@drawable/editext_selector"
android:hint="請?zhí)顚懴祟}"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="消息內(nèi)容:"
android:textColor="@color/black"
android:textSize="17sp" />
<EditText
android:id="@+id/et_message"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="top"
android:layout_margin="5dp"
android:background="@drawable/editext_selector"
android:hint="請?zhí)顚懴?nèi)容"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="消息數(shù)量:"
android:textColor="@color/black"
android:textSize="17sp" />
<EditText
android:id="@+id/et_count"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="@drawable/editext_selector"
android:gravity="left|center"
android:inputType="number"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
<Button
android:id="@+id/btn_show_marker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="發(fā)送消息同時顯示桌面角標"
android:textColor="@color/black"
android:textSize="17sp" />
<Button
android:id="@+id/btn_clear_marker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="清除應用的消息角標"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
創(chuàng)作不易 覺得有幫助請點贊關注收藏文章來源地址http://www.zghlxwxcb.cn/news/detail-600752.html
到了這里,關于Android Studio App開發(fā)之通知渠道NotificationChannel及給華為、小米手機桌面應用添加消息數(shù)量角標實戰(zhàn)(包括消息重要級別的設置 附源碼)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!