小白大学生第一次发布文章,希望大佬批评指正qwq.
在自己的包里创建 NotificationHelper.java 文件,将以下代码复制到该文件。注意:代码的第一行改成自己的包
//实现一个通知的功能
//返回值:通知id(notificationId)
//使用方法:
// 在Activity内使用NotificationHelper notificationHelper = new NotificationHelper(this)实例化此类
//需要this参数
//必要权限:<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
//示例 1、 自定义渠道: NotificationHelper notificationHelper2 = new NotificationHelper(this,"渠道2","描述2","channel_2");
// 默认渠道: NotificationHelper notificationHelperDefault = new NotificationHelper(this);
// 或使用 //notificationHelper.setIntent(intent);
// 2、 发送通知:(后两个可选参数) notificationHelper.sendNotification("标题2","通知10",-1,intent);
package com.example.notfication;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import androidx.core.app.NotificationCompat;
public class NotificationHelper {
private static String CHANNEL_ID = "channelId1";
private final NotificationManager notificationManager;
private Context context;
private PendingIntent m_PendingIntent;
private Intent m_intent;
private static int defaultNotificationId = Integer.MIN_VALUE;
//设置intent来确认需要跳转的页面
public void setIntent(Intent intent) {
this.m_intent = intent;
m_PendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
}
//如不创建渠道则使用默认渠道
public NotificationHelper(Context context){
this(context, "默认渠道", "这里是默认的通知渠道", "default_channel_id");
}
public NotificationHelper(Context context, String channelNameIn, String channelDescriptionIn, String channelIdIn) {
CHANNEL_ID=channelIdIn;
this.context = context;
this.notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
createNotificationChannel(channelNameIn,channelDescriptionIn);
}
//若不指定id则使用自增id
public void sendNotification(String title, String text){
sendNotification(title, text,defaultNotificationId,null);
defaultNotificationId++;
};
//指定id则使用以下方法
public void sendNotification(String title, String text,int notficationId,Intent intent) {
if(notficationId<0){
notficationId=defaultNotificationId;
if(defaultNotificationId>=-1){
defaultNotificationId = Integer.MIN_VALUE;
}else{
defaultNotificationId++;
}
}
PendingIntent l_pendingIntent = null;
if(intent != null){
l_pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Notification.Builder builder = null;
builder = new Notification.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(title)
.setContentText(text);
//当pendingIntent不会空值时才设置
if (m_PendingIntent != null) {
builder.setContentIntent(m_PendingIntent);
}
if (l_pendingIntent != null) {
builder.setContentIntent(l_pendingIntent);
}
builder.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notficationId, builder.build());//如出现报错则位声明权限
}else{ //若小于Android8.0则执行以下代码
NotificationCompat.Builder builder = null;
builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(title)
.setContentText(text);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notficationId, builder.build());//如出现报错则位声明权限
}
}
//创建通知渠道
private void createNotificationChannel(String channelNameIn,String channelDescriptionIn) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence channelName = channelNameIn;
String channelDescription = channelDescriptionIn;
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, channelName, importance);
channel.setDescription(channelDescription);
channel.enableLights(true);
channel.setLightColor(Color.RED);
notificationManager.createNotificationChannel(channel);
}
}
}
使用方法:
在 MianActivity 类的 onCreate 方法中直接实例化此类,使用:
NotificationHelper notificationHelper = new NotificationHelper(this)
下一步在代码中使用notificationHelper类的方法使用
notificationText.sendNotification("通知标题","通知文本",1,intent);
发送通知,sendNotification的四个参数为
参数名称 | 参数类型 | 是否可选 |
通知标题 | String | 必填 |
通知文本 | String | 必填 |
通知id | int | 可选 |
intent | Intent | 可选 |
其中,intent为点击通知后跳转的Activity。