1.获取NotificationManager对象
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
2. 安卓8.0以上版本的通知需要创建NotificationChannel
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel channel = new NotificationChannel("june","通知",NotificationManager.IMPORTANCE_HIGH);
manager.createNotificationChannel(channel);
}
3.使用Builder构造器创建Notification对象,通知设置
setContentTitle
设置通知标题
setContentText
设置通知内容
setSmallIcon
设置通知小图标
setLargeIcon
设置通知大图标
setColor
设置小图标颜色
setContentIntent
设置点击后跳转意图
setAutoCancel
点击通知后自动清除通知
notification = new NotificationCompat.Builder(this,"june")
.setContentTitle("官方通知")
.setContentText("相由心生,道法自然")
.setSmallIcon(R.drawable.ic_android_black_24dp)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.user))
.setColor(Color.parseColor("#ff0000"))
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.build();
4.设置跳转意图
Intent intent = new Intent(this, NotificationActivity.class);
@SuppressLint("UnspecifiedImmutableFlag") PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationActivity.java
package com.weijun901.app;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import androidx.annotation.Nullable;
public class NotificationActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("june", "onCreate: 进入NotificationActivity");
}
}
notification.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Notification">
<LinearLayout
android:orientation="vertical"
android:layout_width="409dp"
android:layout_height="729dp" tools:layout_editor_absoluteY="1dp" tools:layout_editor_absoluteX="1dp">
<Button
android:text="发送通知"
android:onClick="send"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/button"/>
<Button
android:text="取消通知"
android:onClick="cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/button2"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Notification.java
package com.weijun901.app;
import android.annotation.SuppressLint;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.core.app.NotificationCompat;
public class Notification extends AppCompatActivity {
private NotificationManager manager;
private android.app.Notification notification;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification);
//1.获取NotificationManager对象
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//2.安卓8.0以上版本的通知需要创建NotificationChannel
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel channel = new NotificationChannel("june","通知",NotificationManager.IMPORTANCE_HIGH);
manager.createNotificationChannel(channel);
}
//4.设置跳转意图
Intent intent = new Intent(this, NotificationActivity.class);
@SuppressLint("UnspecifiedImmutableFlag") PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
//3.使用Builder构造器创建Notification对象,通知设置
notification = new NotificationCompat.Builder(this,"june")
.setContentTitle("官方通知")
.setContentText("相由心生,道法自然")
.setSmallIcon(R.drawable.ic_android_black_24dp)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.user))
.setColor(Color.parseColor("#ff0000"))
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.build();
}
//5.发送通知
public void send(View view) {
manager.notify(1,notification);
}
//6.取消通知
public void cancel(View view) {
manager.cancel(1);
}
}
5.发送通知
public void send(View view) {
manager.notify(1,notification);
}
6.取消通知
public void send(View view) {
manager.notify(1,notification);
}