通知与服务——消息通知——通知推送Notification

冷不防 2023-09-28 16:28 115阅读 0赞

c461564591dbc577c1664e620eee7ab7.png

手机通知栏存放的是App主动推给用户的提醒消息,每条消息包括消息图标、消息标题、消息内容等,这些消息元素由通知建造器Notification.Builder设定。

常用方法如下:

setSmallIcon:设置应用名称左边的小图标。setLargeIcon:设置通知栏右边的大图标。setContentTitle:设置通知栏的标题文本。setContentText:设置通知栏的内容文本。setSubText:设置通知栏的附加文本,它位于应用名称右边。setProgress:设置进度条并显示当前进度。setUsesChronometer:设置是否显示计时器setContentIntent:设置通知内容的延迟意图,点击通知时触发该意图。build:构建通知。

4ba0f25391094c7e65d38c915225cdeb.png

获得Notification消息对象之后,还要由通知管理器NotificationManager推送消息。

通知管理器的常用方法如下。

notify:把指定消息推送到通知栏。cancel:取消指定的消息通知。cancelAll:取消所有的消息通知。createNotificationChannel:创建指定的通知渠道。getNotificationChannel:获取指定编号的通知渠道。

ed684cd9823e3032f001e9710c750b6e.png

80a8a812683f4146019a085f6fcf0e40.png

==============================================================================================

布局:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical"
  5. android:padding="5dp" >
  6. <LinearLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="50dp"
  9. android:orientation="horizontal">
  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="match_parent"
  13. android:gravity="center"
  14. android:text="消息标题:"
  15. android:textColor="@color/black"
  16. android:textSize="17sp" />
  17. <EditText
  18. android:id="@+id/et_title"
  19. android:layout_width="0dp"
  20. android:layout_height="match_parent"
  21. android:layout_weight="1"
  22. android:layout_margin="5dp"
  23. android:background="@drawable/editext_selector"
  24. android:hint="请填写消息标题"
  25. android:textColor="@color/black"
  26. android:textSize="17sp" />
  27. </LinearLayout>
  28. <LinearLayout
  29. android:layout_width="match_parent"
  30. android:layout_height="70dp"
  31. android:orientation="horizontal">
  32. <TextView
  33. android:layout_width="wrap_content"
  34. android:layout_height="match_parent"
  35. android:gravity="center"
  36. android:text="消息内容:"
  37. android:textColor="@color/black"
  38. android:textSize="17sp" />
  39. <EditText
  40. android:id="@+id/et_message"
  41. android:layout_width="0dp"
  42. android:layout_height="match_parent"
  43. android:layout_weight="1"
  44. android:gravity="top"
  45. android:layout_margin="5dp"
  46. android:background="@drawable/editext_selector"
  47. android:hint="请填写消息内容"
  48. android:textColor="@color/black"
  49. android:textSize="17sp" />
  50. </LinearLayout>
  51. <Button
  52. android:id="@+id/btn_send_simple"
  53. android:layout_width="match_parent"
  54. android:layout_height="wrap_content"
  55. android:gravity="center"
  56. android:text="发送简单消息"
  57. android:textColor="@color/black"
  58. android:textSize="17sp" />
  59. </LinearLayout>

8c387960e3105d9db5d19e7fa3cfc128.png

696ac76243098f2b97751cd20244340d.png

a5b2be535c0dd12de1241568477fdbe5.png

代码;

  1. package com.example.myapplication;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.app.Notification;
  4. import android.app.NotificationManager;
  5. import android.app.PendingIntent;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.graphics.BitmapFactory;
  9. import android.os.Build;
  10. import android.os.Bundle;
  11. import android.text.TextUtils;
  12. import android.view.View;
  13. import android.widget.EditText;
  14. import android.widget.Toast;
  15. public class MainActivity3 extends AppCompatActivity implements View.OnClickListener
  16. {
  17. private EditText et_title;
  18. private EditText et_message;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState)
  21. {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_main3);
  24. et_title = findViewById(R.id.et_title);
  25. et_message = findViewById(R.id.et_message);
  26. findViewById(R.id.btn_send_simple).setOnClickListener(this);
  27. }
  28. @Override
  29. public void onClick(View v)
  30. {
  31. if (v.getId() == R.id.btn_send_simple)
  32. {
  33. ViewUtil.hideOneInputMethod(this, et_message); // 隐藏输入法软键盘
  34. if (TextUtils.isEmpty(et_title.getText()))
  35. {
  36. Toast.makeText(this, "请填写消息标题", Toast.LENGTH_SHORT).show();
  37. return;
  38. }
  39. if (TextUtils.isEmpty(et_message.getText()))
  40. {
  41. Toast.makeText(this, "请填写消息内容", Toast.LENGTH_SHORT).show();
  42. return;
  43. }
  44. String title = et_title.getText().toString();
  45. String message = et_message.getText().toString();
  46. sendSimpleNotify(title, message); // 发送简单的通知消息
  47. }
  48. }
  49. // 发送简单的通知消息(包括消息标题和消息内容)
  50. private void sendSimpleNotify(String title, String message)
  51. {
  52. // 发送消息之前要先创建通知渠道,创建代码见MainApplication.java
  53. // 创建一个跳转到活动页面的意图
  54. Intent clickIntent = new Intent(this, MainActivity2.class);
  55. // 创建一个用于页面跳转的延迟意图
  56. PendingIntent contentIntent = PendingIntent.getActivity(this, R.string.app_name, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  57. // 创建一个通知消息的建造器
  58. Notification.Builder builder = new Notification.Builder(this);
  59. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
  60. {
  61. // Android 8.0开始必须给每个通知分配对应的渠道
  62. builder = new Notification.Builder(this, getString(R.string.app_name));
  63. }
  64. builder.setContentIntent(contentIntent) // 设置内容的点击意图
  65. .setAutoCancel(true) // 点击通知栏后是否自动清除该通知
  66. .setSmallIcon(R.mipmap.ic_launcher) // 设置应用名称左边的小图标
  67. .setSubText("这里是副本") // 设置通知栏里面的附加说明文本
  68. .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_app)) // 设置通知栏右边的大图标
  69. .setContentTitle(title) // 设置通知栏里面的标题文本
  70. .setContentText(message); // 设置通知栏里面的内容文本
  71. Notification notify = builder.build(); // 根据通知建造器构建一个通知对象
  72. // 从系统服务中获取通知管理器
  73. NotificationManager notifyMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  74. // 使用通知管理器推送通知,然后在手机的通知栏就会看到该消息
  75. notifyMgr.notify(R.string.app_name, notify);
  76. }
  77. }

6d72b63b209bfbbcd2dbaed064d955f3.png

02d640bd3058ec61389fca919d1b8c3f.png

e39a4b58682bd90a5fb7c5e2857786b8.png

0af87756f8d91e50be9eba4c1a3ace3e.png

55d82df7813e2e77161b01d69d4e9448.png

============================================================================================================

布局:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical"
  5. android:padding="5dp" >
  6. <LinearLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="50dp"
  9. android:orientation="horizontal">
  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="match_parent"
  13. android:gravity="center"
  14. android:text="消息标题:"
  15. android:textColor="@color/black"
  16. android:textSize="17sp" />
  17. <EditText
  18. android:id="@+id/et_title"
  19. android:layout_width="0dp"
  20. android:layout_height="match_parent"
  21. android:layout_weight="1"
  22. android:layout_margin="5dp"
  23. android:background="@drawable/editext_selector"
  24. android:hint="请填写消息标题"
  25. android:textColor="@color/black"
  26. android:textSize="17sp" />
  27. </LinearLayout>
  28. <LinearLayout
  29. android:layout_width="match_parent"
  30. android:layout_height="70dp"
  31. android:orientation="horizontal">
  32. <TextView
  33. android:layout_width="wrap_content"
  34. android:layout_height="match_parent"
  35. android:gravity="center"
  36. android:text="消息内容:"
  37. android:textColor="@color/black"
  38. android:textSize="17sp" />
  39. <EditText
  40. android:id="@+id/et_message"
  41. android:layout_width="0dp"
  42. android:layout_height="match_parent"
  43. android:layout_weight="1"
  44. android:gravity="top"
  45. android:layout_margin="5dp"
  46. android:background="@drawable/editext_selector"
  47. android:hint="请填写消息内容"
  48. android:textColor="@color/black"
  49. android:textSize="17sp" />
  50. </LinearLayout>
  51. <Button
  52. android:id="@+id/btn_send_counter"
  53. android:layout_width="match_parent"
  54. android:layout_height="wrap_content"
  55. android:gravity="center"
  56. android:text="发送计时消息"
  57. android:textColor="@color/black"
  58. android:textSize="17sp" />
  59. </LinearLayout>

1b26ec8d8705fa58ad7266cc9a29e3af.png

9ca36506dc8e5b2e70f656ac3b54a4c0.png

3afbb2592ad62dd550a107d661102e45.png

代码:

  1. package com.example.myapplication;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.app.Notification;
  4. import android.app.NotificationManager;
  5. import android.app.PendingIntent;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.graphics.BitmapFactory;
  9. import android.os.Build;
  10. import android.os.Bundle;
  11. import android.text.TextUtils;
  12. import android.view.View;
  13. import android.widget.EditText;
  14. import android.widget.Toast;
  15. public class MainActivity4 extends AppCompatActivity implements View.OnClickListener
  16. {
  17. private EditText et_title;
  18. private EditText et_message;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState)
  21. {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_main4);
  24. et_title = findViewById(R.id.et_title);
  25. et_message = findViewById(R.id.et_message);
  26. findViewById(R.id.btn_send_counter).setOnClickListener(this);
  27. }
  28. @Override
  29. public void onClick(View v)
  30. {
  31. if (v.getId() == R.id.btn_send_counter)
  32. {
  33. ViewUtil.hideOneInputMethod(this, et_message); // 隐藏输入法软键盘
  34. if (TextUtils.isEmpty(et_title.getText()))
  35. {
  36. Toast.makeText(this, "请填写消息标题", Toast.LENGTH_SHORT).show();
  37. return;
  38. }
  39. if (TextUtils.isEmpty(et_message.getText()))
  40. {
  41. Toast.makeText(this, "请填写消息内容", Toast.LENGTH_SHORT).show();
  42. return;
  43. }
  44. String title = et_title.getText().toString();
  45. String message = et_message.getText().toString();
  46. sendCounterNotify(title, message); // 发送计时的通知消息
  47. }
  48. }
  49. // 发送计时的通知消息
  50. private void sendCounterNotify(String title, String message)
  51. {
  52. // 发送消息之前要先创建通知渠道,创建代码见MainApplication.java
  53. // 创建一个跳转到活动页面的意图
  54. Intent cancelIntent = new Intent(this, MainActivity2.class);
  55. // 创建一个用于页面跳转的延迟意图
  56. PendingIntent deleteIntent = PendingIntent.getActivity(this, R.string.app_name, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  57. // 创建一个通知消息的建造器
  58. Notification.Builder builder = new Notification.Builder(this);
  59. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
  60. {
  61. // Android 8.0开始必须给每个通知分配对应的渠道
  62. builder = new Notification.Builder(this, getString(R.string.app_name));
  63. }
  64. builder.setDeleteIntent(deleteIntent) // 设置内容的清除意图
  65. .setSmallIcon(R.mipmap.ic_launcher) // 设置应用名称左边的小图标
  66. .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_app)) // 设置通知栏右边的大图标
  67. .setProgress(100, 60, false) // 设置进度条及其具体进度
  68. .setUsesChronometer(true) // 设置是否显示计时器
  69. .setContentTitle(title) // 设置通知栏里面的标题文本
  70. .setContentText(message); // 设置通知栏里面的内容文本
  71. Notification notify = builder.build(); // 根据通知建造器构建一个通知对象
  72. // 从系统服务中获取通知管理器
  73. NotificationManager notifyMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  74. // 使用通知管理器推送通知,然后在手机的通知栏就会看到该消息
  75. notifyMgr.notify(R.string.app_name, notify);
  76. }
  77. }

224c448efcb8eec658ce77c02fd3aef5.png

683986c1496af5222d5ab5332cdb83aa.png

c70dd49dda7fc924416377c71d873194.png

a4f3effe5f54683596f6156cc53a62c8.png

10f53f3ee428e2a319d6e6b032c28f32.png

===================================================================================================

f116c85e0d339de7cf30d2743b3a10a0.png

8ed05b954806ba5657c6fcadf7a9aaf9.png

c14be94846b152935f2bf6aae1e855c4.png

ffd80e45ea17480d986ebd66cbe94b29.png

912dffa152c94a4f7c10bef86f740c9f.png

67419a364cf44950b22d23ee2ef49bad.png

发表评论

表情:
评论列表 (有 0 条评论,115人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Notification通知

    Notification: 例子的NotificationCompat使用的是v4包下的,稍微有点老,如果大家使用的话,建议使用v7包下的,通知也没什么好介绍的,直接看代...