Android如何使用Notification进行通知
16lz
2022-04-05
有两张图片素材会放在末尾
activity代码,和XML布局
package com.example.myapplication;import androidx.appcompat.app.AppCompatActivity;import androidx.core.app.NotificationCompat;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.BitmapFactory;import android.graphics.Color;import android.os.Bundle;import android.view.View;import android.widget.ImageView;public class NotificationActivity extends AppCompatActivity { public static NotificationManager notificationManager; public static String CHANNEL_1 = "channel1"; Notification notification; ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notification); iv = findViewById(R.id.iv); iv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { iv.setImageResource(R.drawable.aoligei); notificationManager = (NotificationManager) NotificationActivity.this.getSystemService(Context.NOTIFICATION_SERVICE); //android 8.0有通知渠道,如果不设置的话,会报错 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { 配置通知渠道id,渠道名称(用户可以看到),渠道优先级 NotificationChannel channel1 = new NotificationChannel(CHANNEL_1, "通知渠道1", NotificationManager.IMPORTANCE_HIGH); channel1.setDescription("通知渠道的描述1"); ///配置通知出现时的闪灯(如果 android 设备支持的话) channel1.enableLights(true); channel1.setLightColor(Color.WHITE); //配置通知出现时的震动(如果 android 设备支持的话) channel1.enableVibration(true); channel1.setVibrationPattern(new long[]{100, 200, 100, 200}); //channel.setBypassDnd(true); //设置绕过免打扰模式 //channel.canBypassDnd(); //检测是否绕过免打扰模式 //channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);//设置在锁屏界面上显示这条通知 notificationManager.createNotificationChannel(channel1);// NotificationChannel channel = new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_HIGH);// channel.setShowBadge(true);// NotificationManager notificationManager = (NotificationManager) getSystemService(// NOTIFICATION_SERVICE);// notificationManager.createNotificationChannel(channel); } //普通的通知// sendChatMsg(); //可以点击的通知 preIntent(); } }); } public void sendChatMsg() { NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification notification = new NotificationCompat.Builder(this, CHANNEL_1) .setContentTitle("收到一条聊天消息") .setContentText("老八问候你吃了吗") .setWhen(System.currentTimeMillis()) .setSmallIcon(R.drawable.ic_launcher_background) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background)) .setAutoCancel(true) .setNumber(2) .build();// for (int i = 0; i < 3; i++) { manager.notify(1, notification);// } } public void preIntent(){ NotificationManager manager1 = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //创建一个可以点击的通知 //通知的需要跳转,创建Intent Intent intent = new Intent(NotificationActivity.this, NotificationActivity.class); //需要创建PendingIntent进行 PendingIntent pendingIntent = PendingIntent.getActivity(NotificationActivity.this, 0, intent, 0); //创建notification notification = new Notification.Builder(NotificationActivity.this,CHANNEL_1).//第二个参数需要保持一致 setContentTitle("这是一个通知") .setContentText("快来和老八一起共进晚餐") . setWhen(System.currentTimeMillis()) . setSmallIcon(R.mipmap.ic_launcher) . setLargeIcon(BitmapFactory.decodeResource( getResources(), R.mipmap.ic_launcher)) . setContentIntent(pendingIntent). build(); //。build就是创建的意思 //这个id是随便起的名字,只要id不同可以多个显示,如果id相同,会显示玩当前id的通知才会显示下一个id manager1.notify(0x11, notification); }}
XML布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".NotificationActivity"><ImageView android:id="@+id/iv" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/haha" /><!-- android:adjustViewBounds="true"--><!-- android:scaleType="fitXY"--></LinearLayout>
更多相关文章
- android 创建 删除桌面快捷方式
- 用代码创建Android视图
- android 打开通知栏
- android中全屏显示
- Android创建和删除桌面快捷方式
- Android(安卓)Wear 进阶 5 - Creating Watch Faces 创建手表的界
- Android(安卓)封装http请求的工具类
- Android(安卓)在桌面创建一个快捷方式
- android之Notification的使用