一、Notification与NotificationManager

1.创建一个NotificationManager

  1. NotificationManager类是一个通知管理器类,这个对象是由系统维护的服务,是以单例模式的方式获得,所以一般并不直接实例化这个对象。在Activity中,可以使用Activity.getSystemService(String)方法获取NotificationManager对象,Activity.getSystemService(String)方法可以通过Android系统级服务的句柄,返回对应的对象。在这里需要返回NotificationManager,所以直接传递Context.NOTIFICATION_SERVICE即可。

2.使用Builder构造器来创建Notification对象

  1. 使用NotificationCompat类的Builder构造器来创建Notification对象,可以保证程序在所有的版本上都能正常工作。Android8.0新增了通知渠道这个概念,如果没有设置,则通知无法在Android8.0的机器上显示。

二、NotificationChannel

通知渠道:Android8.0引入了通知渠道,其允许您为要显示的每种通知类型创建用户可自定义的渠道。

通知重要程度设置,NotificationManager类中

IMPORTANCE_NONE 关闭通知
IMPORTANCE_MIN 开启通知,不会弹出,但没有提示音,状态栏中无显示
IMPORTANCE_LOW 开启通知,不会弹出,不发出提示音,状态栏中显示
IMPORTANCE_DEFAULT 开启通知,不会弹出,发出提示音,状态栏中显示
IMPORTANCE_HIGH 开启通知,会弹出,发出提示音,状态栏中显示

三、常见方法说明

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图层(图标不能带颜色)

四、发送通知的实现

  1. 1.布局activity_main.xml代码
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity"
  8. android:orientation="vertical"
  9. android:gravity="center">
  10. <Button
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:onClick="sendNotification"
  14. android:text="发出通知" />
  15. <Button
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:onClick="cancelNotification"
  19. android:text="取消通知" />
  20. </LinearLayout>
  1. 2.MainActivity文件代码
  1. package com.example.mynotification;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import androidx.core.app.NotificationCompat;
  4. import android.app.Notification;
  5. import android.app.NotificationChannel;
  6. import android.app.NotificationManager;
  7. import android.app.PendingIntent;
  8. import android.content.Intent;
  9. import android.graphics.Bitmap;
  10. import android.graphics.BitmapFactory;
  11. import android.graphics.Color;
  12. import android.os.Build;
  13. import android.os.Bundle;
  14. import android.view.View;
  15. public class MainActivity extends AppCompatActivity {
  16. private NotificationManager manager;
  17. private Notification notification;
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main);
  22. //1.创建通知管理器
  23. manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  24. //3.创建通知栏频道,跳出通知,把频道放进管理器
  25. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  26. NotificationChannel channel = new NotificationChannel("he",
  27. "测试通知", NotificationManager.IMPORTANCE_HIGH);
  28. manager.createNotificationChannel(channel);
  29. }
  30. //5.创建意图,点击通知栏跳转到对应的页面
  31. Intent intent = new Intent(this, NotificationActivity.class);
  32. PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
  33. //2.通过Build创建通知栏
  34. notification = new NotificationCompat.Builder(this, "he")
  35. //4.设置通知栏的方法属性
  36. .setContentTitle("官方通知")
  37. .setContentText("世界那么大,想去走走吗")
  38. .setSmallIcon(R.drawable.ic_person_black_24dp)
  39. .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.name))
  40. .setColor(Color.parseColor("#ff0000"))
  41. .setContentIntent(pendingIntent)
  42. .setAutoCancel(true)
  43. .build();
  44. }
  45. //6.触发发送通知,点击按钮,发送通知
  46. public void sendNotification(View view) {
  47. manager.notify(1, notification);
  48. }
  49. public void cancelNotification(View view) {
  50. manager.cancel(1);
  51. }
  52. }
  1. 3.NotificationActivity文件代码
  1. package com.example.mynotification;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import android.widget.Toast;
  6. import androidx.annotation.Nullable;
  7. public class NotificationActivity extends Activity{
  8. @Override
  9. protected void onCreate(@Nullable Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. Log.e("he","进入NotificationActivity");
  12. Toast.makeText(this, "页面跳转成功", Toast.LENGTH_SHORT).show();
  13. }
  14. }
  1. 4.效果图<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/1574587/1621935856853-e431cdd4-6e9c-4534-8fa1-a80a2ff4474c.png#clientId=ud263173f-3ade-4&from=paste&height=487&id=u9e6878ad&margin=%5Bobject%20Object%5D&name=image.png&originHeight=487&originWidth=851&originalType=binary&size=89780&status=done&style=none&taskId=ufbc9612f-e114-47bd-aee1-11c58696f1a&width=851)<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/1574587/1621935887295-c71f07bb-062e-4872-bbdc-223028f93939.png#clientId=ud263173f-3ade-4&from=paste&height=499&id=ubc78b8aa&margin=%5Bobject%20Object%5D&name=image.png&originHeight=499&originWidth=912&originalType=binary&size=91429&status=done&style=none&taskId=u9a4adef6-a950-48b2-b936-fa665c8b511&width=912)