android Notification通知管理
用于提醒用户有消息通知
使用notification一般需要四步
1.获取通知管理器
2.创建通知 8.0以后需要自建通知通道
3.绑定对应的Activity
4.发送通知
获取通知管理器
//1.获取通知管理器
NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
2.创建通知 8.0以后需要自建通知通道
//2.创建通知 8.0以后需要自建通知通道
Notification notification=null;
//创建通道
String id="mchannel";//通道id
String name="通道1";//通道名称
//判断安卓版本 如果大于8.0则使用通道
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel= new NotificationChannel(id,name,NotificationManager.IMPORTANCE_LOW);
//创建通道
manager.createNotificationChannel(channel);
//创建通知
notification=new Notification.Builder(getApplicationContext(),id)
//setLargeIcon 设置大图标
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.wolf))
//setSmallIcon 设置小图标
.setSmallIcon(R.drawable.ic_event_note_black_24dp)
//setContentText 设置内容
.setContentText("下周放假")
//setStyle 设置样式
.setStyle(new Notification.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.wolf)))
//setContentTitle 设置标题
.setContentTitle("好消息")
//setAutoCancel点击过后取消显示
.setAutoCancel(true)
//帮对应的Activity
.setContentIntent(PendingIntent.getActivity(getApplicationContext(),1,new Intent(MainActivity.this, NotificationResult.class), PendingIntent.FLAG_CANCEL_CURRENT)
).build();
}else{
//安卓4.0-8.0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notification =new Notification.Builder(getApplicationContext()
)//setSmallIcon 设置小图标
.setSmallIcon(R.drawable.ic_event_note_black_24dp)
//setContentText 设置内容
.setContentText("下周放假")
//setContentTitle 设置标题
.setContentTitle("好消息")
//setAutoCancel点击过后取消显示
.setAutoCancel(true)
.setContentIntent(
PendingIntent.getActivity(
getApplicationContext(),1,new
Intent(
MainActivity.this,
NotificationResult.class),
PendingIntent.FLAG_CANCEL_CURRENT
)
).build();
}
}
3.绑定对应的Activity
步骤2创建的时候已经使用Intent绑定了
4.发出通知
//4.发出通知
manager.notify(0,notification);
完整代码如下:
复制过去报错的都是资源问题 需要自己创建资源
package com.example.notificationdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//1.获取通知管理器
NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//2.创建通知 8.0以后需要自建通知通道
Notification notification=null;
//创建通道
String id="mchannel";//通道id
String name="通道1";//通道名称
//判断安卓版本 如果大于8.0则使用通道
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel= new NotificationChannel(id,name,NotificationManager.IMPORTANCE_LOW);
//创建通道
manager.createNotificationChannel(channel);
//创建通知
notification=new Notification.Builder(getApplicationContext(),id)
//setLargeIcon 设置大图标
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.wolf))
//setSmallIcon 设置小图标
.setSmallIcon(R.drawable.ic_event_note_black_24dp)
//setContentText 设置内容
.setContentText("下周放假")
//setStyle 设置样式
.setStyle(new Notification.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.wolf)))
//setContentTitle 设置标题
.setContentTitle("好消息")
//setAutoCancel点击过后取消显示
.setAutoCancel(true)
//帮对应的Activity
.setContentIntent(PendingIntent.getActivity(getApplicationContext(),1,new Intent(MainActivity.this, NotificationResult.class), PendingIntent.FLAG_CANCEL_CURRENT)
).build();
}else{
//安卓4.0-8.0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notification =new Notification.Builder(getApplicationContext()
)//setSmallIcon 设置小图标
.setSmallIcon(R.drawable.ic_event_note_black_24dp)
//setContentText 设置内容
.setContentText("下周放假")
//setContentTitle 设置标题
.setContentTitle("好消息")
//setAutoCancel点击过后取消显示
.setAutoCancel(true)
.setContentIntent(
PendingIntent.getActivity(
getApplicationContext(),1,new
Intent(
MainActivity.this,
NotificationResult.class),
PendingIntent.FLAG_CANCEL_CURRENT
)
).build();
}
}
//3.绑定对应的Activity
//4.发出通知
manager.notify(0,notification);
System.out.println("发送成功");
}
});
}
}
谢谢观看 0v0