创建一个NotificationManager
NotificationManager类是一个通知管理类,这个对象是由系统维护的服务,是以单例模式的方法获得,所以一般并不直接实例化这个对象。这个Activity中,可以使用Activity.getSystemService(String)方法获取NotificationManager对象,Activity.getSystemService(String)方法可以通过Android系统级服务的句柄,返回对应的对象。在这里需要返回NotificationManager,所以直接传递Context.NOTIFICATION_SERRVICE即可。
使用Builder构造器来创建Notification对象
使用NotificationCompat类的Builder构造器来创建Notification对象,可以保证程序在所有的版本上都能正常工作。Android8.0新增了通知渠道这个概念,如果没有设置,则通知无法在Android8.0的机器上显示
通知渠道,Android8.0引入了通知渠道,其允许您为要显示的每种通知类型创建用户可自定义的渠道。
通知重要程度设置,NotificationManager类中
IMPORTANCE_NONE 关闭通知
IMPORTANCE_MIN 开启通知,不会弹出,但没有提示音,状态栏中无显示
IMPORTANCE_LOW 开启通知,不会弹出,不发出提示音,状态栏中显示
IMPORTANCE_DEFAULT 开启通知,不会弹出。发出提示音,状态栏中显示
IMPORTANCE_HIGH 开启通知,会弹出,发出提示音,状态栏中显示
notification常见方法说明
通过链式结构设置
1.setContentTitle(String string) 设置标题(必须设置)
2.setContentText(String string) 设置文本内容(必须设置)
3.setSmallIcon(int icon) 设置小图标(必须设置)
4.setLargeIcon(Bitmap icon) 设置通知的大图标
5.setColor(int argb) 设置小图标的颜色
6.setContentIntent(PendingIntent intent) 设置点击通知后的跳转意图
7.setAutoCancel(boolean boolean) 设置点击通知后自动清除通知
8.setWhen(long when) 设置通知被创建的时间
Android从5.0系统开始,对于通知栏图标的设计进行了修改,现在Google要求,所有应用程序的通知栏图标,应该只使用alpha图层来进行绘制,而不应该包括RGB图层。(图片不能带颜色)
package com.example.notification;
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.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private NotificationManager manage;
private Notification notification;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manage = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//安卓版本是8.0以上的时候创建
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//id要和notification的channelId一致
// id 字符串 重要程度
NotificationChannel channel = new NotificationChannel("Finny", "测试通知",
NotificationManager.IMPORTANCE_HIGH);
manage.createNotificationChannel(channel);
}
//创建意图,setContentIntent的参数PendingIntent
Intent intent = new Intent(this, NotificationActivity.class);
// 上下文环境 请求码 Intent 标记
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
//创建Notification
notification = new NotificationCompat.Builder(this,"Finny")
//1.setContentTitle(String string) 设置标题(必须设置)
.setContentTitle("官方通知")
//2.setContentText(String string) 设置文本内容(必须设置)
.setContentText("世界那么大,我想去看看")
//3.setSmallIcon(int icon) 设置小图标(必须设置)
.setSmallIcon(R.drawable.ic_baseline_tag_faces_24)
//4.setLargeIcon(Bitmap icon) 设置通知的大图标
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.pt))
//5.setColor(int argb) 设置小图标的颜色
.setColor(Color.parseColor("#FF03DAC5"))
//6.setContentIntent(PendingIntent intent) 设置点击通知后的跳转意图
.setContentIntent(pendingIntent)
//7.setAutoCancel(boolean boolean) 设置点击通知后自动清除通知
.setAutoCancel(true)
//8.setWhen(long when) 设置通知被创建的时间
// .setWhen()
.build();
}
//发送通知
public void sendNotification(View view) {
//id 可以随便写
manage.notify(1,notification);
}
//取消通知
public void cacelNotification(View view) {
//id要和发送的id相同
manage.cancel(1);
}
}
NotificationActivity.java
package com.example.notification;
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("Finny", "onCreate: 进入NotificationActivity");
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.notification">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NotificationActivity" />
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:text="发送通知"
android:onClick="sendNotification"
android:background="@color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="取消通知"
android:onClick="cacelNotification"
android:background="@color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
themes.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
刚进程序的图片
点击发送通知后
屏幕下拉
点击通知后跳转到另一个界面,我没设置内容,是白屏,但会打印消息
跳转另一个界面后,通知会自动消失,或者点击取消通知按钮,即可取消通知