Android Notification(通知消息)

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;//通知消息

    /********************************************************************************
     * 获取视图对象ID
     ********************************************************************************/
    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;
        }
    }

    /********************************************************************************
     * 切换Activity/
     * srcContext : 源Activity/Service
     * dstContext : 目标Activity/Service
     ********************************************************************************/
    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文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <!--xSuper Demo5标题 工具栏 -->
    <androidx.appcompat.widget.Toolbar
        android:id="@+id/tb_ToolbarDemo5"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#2196F3"
        app:navigationIcon="@drawable/ic_baseline_arrow_back_24">
        <!--工具栏标题文本-->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="xSuper Demo5标题"
            android:gravity="center"
            android:layout_gravity="center" />
    </androidx.appcompat.widget.Toolbar>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="50dp">

        <!--发送通知按钮-->
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50dp"
            android:text="发送通知"
            android:onClick="vSendNotificationButton"/>

        <!--取消通知按钮-->
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50dp"
            android:text="取消通知"
            android:onClick="vCancelNotificationButton"/>
    </LinearLayout>

</LinearLayout>


在这里插入图片描述

三、单击通知跳转的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文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="28sp"
        android:textColor="#0000FF"
        android:layout_marginTop="100dp"
        android:layout_marginLeft="30dp"
        android:text="单击通知跳转至此处......"/>

</LinearLayout>

在这里插入图片描述

五、最终效果

在这里插入图片描述在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值