通知与服务——消息通知——通知渠道NotificationChannel
一个应用允许拥有多个通知渠道,每个渠道的重要性各不相同,从Android8开始必须指定通知渠道才能正常推送消息。
引入通知渠道之后,推送消息的过程分为下列几个步骤:
(1)创建默认的通知渠道(2)使用通知渠道创建通知建造器(3)利用通知建造器设置消息参数,并生成通知对象(4)通过通知管理器推送消息
=========================================================================================
布局:
<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="请填写消息标题"
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="消息内容:"
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="请填写消息内容"
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="发送渠道消息"
android:textColor="@color/black"
android:textSize="17sp" />
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="跳转下一个页面"
android:textColor="@color/black"
android:textSize="17sp" />
<Button
android:id="@+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="跳转下一个页面————消息推送"
android:textColor="@color/black"
android:textSize="17sp" />
<Button
android:id="@+id/btn4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="跳转下一个页面————消息推送——-计算"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
代码:
package com.example.myapplication;
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;
public class MainActivity 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_main);
et_title = findViewById(R.id.et_title);
et_message = findViewById(R.id.et_message);
findViewById(R.id.btn_send_channel).setOnClickListener(this);
findViewById(R.id.btn2).setOnClickListener(this);
findViewById(R.id.btn3).setOnClickListener(this);
findViewById(R.id.btn4).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 = {"不重要", // 无通知
"最小级别", // 通知栏折叠,无提示声音,无锁屏通知
"有点重要", // 通知栏展开,无提示声音,有锁屏通知
"一般重要", // 通知栏展开,有提示声音,有锁屏通知
"非常重要", // 通知栏展开,有提示声音,有锁屏通知,在屏幕顶部短暂悬浮(有的手机需要在设置页面开启横幅)
"最高级别" // 通知栏展开,有提示声音,有锁屏通知,在屏幕顶部短暂悬浮(有的手机需要在设置页面开启横幅)
};
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)
{
}
}
// 发送指定渠道的通知消息(包括消息标题和消息内容)
private void sendChannelNotify(String title, String message)
{
// 创建一个跳转到活动页面的意图
Intent clickIntent = new Intent(this, MainActivity2.class); // MainActivity。class ---用于跳转页面
// 创建一个用于页面跳转的延迟意图
PendingIntent contentIntent = PendingIntent.getActivity(this, R.string.app_name, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// 创建一个通知消息的建造器
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) // 设置内容的点击意图
.setAutoCancel(true) // 点击通知栏后是否自动清除该通知
.setSmallIcon(R.mipmap.ic_launcher) // 设置应用名称左边的小图标
.setContentTitle(title) // 设置通知栏里面的标题文本
.setContentText(message); // 设置通知栏里面的内容文本
Notification notify = builder.build(); // 根据通知建造器构建一个通知对象
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
NotifyUtil.createNotifyChannel(this, mChannelId, mChannelName, mImportance);
}
// 从系统服务中获取通知管理器
NotificationManager notifyMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// 使用通知管理器推送通知,然后在手机的通知栏就会看到该消息,多条通知需要指定不同的通知编号
notifyMgr.notify(Integer.parseInt(mChannelId), notify);
if (mImportance != NotificationManager.IMPORTANCE_NONE)
{
Toast.makeText(this, "已发送渠道消息", 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, "请填写消息标题", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(et_message.getText()))
{
Toast.makeText(this, "请填写消息内容", Toast.LENGTH_SHORT).show();
return;
}
// 发送指定渠道的通知消息(包括消息标题和消息内容)
sendChannelNotify(et_title.getText().toString(), et_message.getText().toString());
}
if (v.getId() == R.id.btn2)
{
startActivity(new Intent(this,MainActivity2.class));
}
if (v.getId() == R.id.btn3)
{
startActivity(new Intent(this,MainActivity3.class));
}
if (v.getId() == R.id.btn4)
{
startActivity(new Intent(this,MainActivity4.class));
}
}
}
ViewUtil
package com.example.myapplication;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
public class ViewUtil
{
public static void hideAllInputMethod(Activity act)
{
// 从系统服务中获取输入法管理器
InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) // 软键盘如果已经打开则关闭之
{
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
}
public static void hideOneInputMethod(Activity act, View v)
{
// 从系统服务中获取输入法管理器
InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
// 关闭屏幕上的输入法软键盘
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
NotifyUtil
package com.example.myapplication;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class NotifyUtil
{
private final static String TAG = "NotifyUtil";
@TargetApi(Build.VERSION_CODES.O)
// 创建通知渠道。Android 8.0开始必须给每个通知分配对应的渠道
public static void createNotifyChannel(Context ctx, String channelId, String channelName, int importance)
{
// 从系统服务中获取通知管理器
NotificationManager notifyMgr = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
if (notifyMgr.getNotificationChannel(channelId) == null) // 已经存在指定编号的通知渠道
{
// 创建指定编号、指定名称、指定级别的通知渠道
NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
channel.setSound(null, null); // 设置推送通知之时的铃声。null表示静音推送
channel.enableLights(true); // 通知渠道是否让呼吸灯闪烁
channel.enableVibration(true); // 通知渠道是否让手机震动
channel.setShowBadge(true); // 通知渠道是否在应用图标的右上角展示小红点
// VISIBILITY_PUBLIC显示所有通知信息,VISIBILITY_PRIVATE只显示通知标题不显示通知内容,VISIBILITY_SECRET不显示任何通知信息
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); // 设置锁屏时候的可见性
channel.setImportance(importance); // 设置通知渠道的重要性级别
notifyMgr.createNotificationChannel(channel); // 创建指定的通知渠道
}
}
// 在桌面上的应用图标右上角显示数字角标
public static void showMarkerCount(Context ctx, int count, Notification notify)
{
showBadgeOfEMUI(ctx, count); // 华为手机EMUI系统的消息角标
// 小米手机还要进入设置里面的应用管理,开启当前App的“显示桌面图标角标”
showBadgeOfMIUI(count, notify); // 小米手机MIUI系统的消息角标
}
// 华为的消息角标需要事先声明两个权限:android.permission.INTERNET、com.huawei.android.launcher.permission.CHANGE_BADGE
private static void showBadgeOfEMUI(Context ctx, int count)
{
try
{
Bundle extra = new Bundle(); // 创建一个包裹对象
extra.putString("package", ctx.getPackageName()); // 应用的包名
// 应用的首屏页面路径
extra.putString("class", ctx.getPackageName()+".MainActivity");
extra.putInt("badgenumber", count); // 应用的消息数量
Uri uri = Uri.parse("content://com.huawei.android.launcher.settings/badge/");
// 通过内容解析器调用华为内核的消息角标服务
ctx.getContentResolver().call(uri, "change_badge", null, extra);
} catch (Exception e)
{
e.printStackTrace();
}
}
// 小米的消息角标需要在发送通知的时候一块调用
private static void showBadgeOfMIUI(int count, Notification notify)
{
try
{
// 利用反射技术获得额外的新增字段extraNotification
Field field = notify.getClass().getDeclaredField("extraNotification");
Log.d(TAG, "field.getName="+field.getName());
// 该字段为Notification类型,下面获取它的实例对象
Object extra = field.get(notify);
Log.d(TAG, "extraNotification.toString="+extra.toString());
// 利用反射技术获得额外的新增方法setMessageCount
Method method = extra.getClass().getDeclaredMethod("setMessageCount", int.class);
Log.d(TAG, "method.getName="+method.getName());
// 利用反射技术调用实例对象的setMessageCount方法,设置消息角标的数量
method.invoke(extra, count);
Log.d(TAG, "invoke count="+count);
} catch (Exception e)
{
e.printStackTrace();
}
}
}
============================================================================================
=============================================================================================================
还没有评论,来说两句吧...