- Android Notification(通知消息)样式
- 一、核心文件源码
- 二、主界面页面
- 三、单击通知跳转的Activity
- 四、单击通知跳转的页面
- 五、最终效果
Android Notification(通知消息)样式
① 为小图标
② 为App名称
③ 为标题
④ 为内容
⑤ 为大图标
一、核心文件源码
Demo5_Practice_Notification.java文件
package com.xsuper.demoapplication;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.NotificationCompat;
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class Demo5_Practice_Notification extends AppCompatActivity implements View.OnClickListener {
private Toolbar toolbarDemo5; //工具栏
private NotificationManager notificationManager;//通知消息管理
private Notification notification;//通知消息
private void vGetViewId(){
this.toolbarDemo5 = (Toolbar) findViewById(R.id.tb_ToolbarDemo5);
}
private void vRegisterView_OnClick_Listener(){
this.toolbarDemo5.setNavigationOnClickListener(this::onClick);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo5_practice_notification);
vGetViewId();
vRegisterView_OnClick_Listener();
//Android 8.0以后引入通知渠道,为要显示的美中通知类型创建自定义渠道
//Builder()中的channelId为渠道id,需通过NotificationChannel()来设置
//在NotificationChannel()和Builder()中的渠道id必须相同
//importance通知的重要程度,通过NotificationManager来设置
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);//获取系统通知服务
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
//Android 8.0以上SDK
//创建通知渠道
NotificationChannel notificationChannel = new NotificationChannel("CHX_ID", "测试通知", NotificationManager.importANCE_HIGH);
notificationManager.createNotificationChannel(notificationChannel);
}
//点击通知跳转意图
Intent intent = new Intent(this, NotificationJumpActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
notification = new Notification.Builder(this, "CHX_ID")
.setContentTitle("官方通知")//设置标题,必须项
.setContentText("世界那么大,想带你去看看(*^▽^*)")//设置内容,必须项
.setSmallIcon(R.drawable.ic_baseline_people_alt_24)//设置小图标,必须项,alphe图层
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.button_pink_record))//设置大图标
.setColor(Color.parseColor("#FF0000"))//设置小图标颜色
.setContentIntent(pendingIntent)//设置点击通知跳转意图
.setAutoCancel(true)//设置单击通知后清除通知
.setWhen(System.currentTimeMillis())//设置通知时间
.build();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case -1 :
vMainSwitchActivity(Demo5_Practice_Notification.this, MainActivity.class);//切换到首页
break;
default : break;
}
}
private void vMainSwitchActivity(Context srcContext, Class> dstContext){
//setClass:跳转到与该工程下(同一个Application中的)activity或service
//setClassName:跳转到不同Applicaiton的activity或service
Intent intent = new Intent();
intent.setClass(srcContext, dstContext);
startActivity(intent);
}
//发送通知
public void vSendNotificationButton(View view) {
notificationManager.notify(1, notification);
}
//取消通知
public void vCancelNotificationButton(View view) {
//id与发送通知id一致
notificationManager.cancel(1);
//notificationManager.cancelAll();
}
}
二、主界面页面
activity_demo5_practice_notification.xml文件
三、单击通知跳转的Activity
NotificationJumpActivity .java文件
package com.xsuper.demoapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class NotificationJumpActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification_jump);
}
}
四、单击通知跳转的页面
activity_notification_jump.xml文件
五、最终效果



