通知与服务——消息通知——通知渠道NotificationChannel

浅浅的花香味﹌ 2023-09-28 16:28 130阅读 0赞

a42cf7ab4f15d7a92c64d0cb5cbf8d2e.png

一个应用允许拥有多个通知渠道,每个渠道的重要性各不相同,从Android8开始必须指定通知渠道才能正常推送消息。

引入通知渠道之后,推送消息的过程分为下列几个步骤:

(1)创建默认的通知渠道(2)使用通知渠道创建通知建造器(3)利用通知建造器设置消息参数,并生成通知对象(4)通过通知管理器推送消息

351de1358dc93718a4312cbb63ee25ab.png

1ad405202c08c545ad203c2ea4e0773c.png

6e23d00b0bb44ef090062e7dd42548fb.png

422bc65c644be43eb7198fdfe37aa543.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. <LinearLayout
  52. android:id="@+id/ll_channel"
  53. android:layout_width="match_parent"
  54. android:layout_height="50dp"
  55. android:orientation="horizontal"
  56. android:visibility="gone">
  57. <TextView
  58. android:layout_width="wrap_content"
  59. android:layout_height="match_parent"
  60. android:gravity="center"
  61. android:text="渠道级别:"
  62. android:textColor="@color/black"
  63. android:textSize="17sp" />
  64. <Spinner
  65. android:id="@+id/sp_importance"
  66. android:layout_width="0dp"
  67. android:layout_height="match_parent"
  68. android:layout_weight="1"
  69. android:layout_margin="5dp"
  70. android:gravity="center"
  71. android:spinnerMode="dialog" />
  72. </LinearLayout>
  73. <Button
  74. android:id="@+id/btn_send_channel"
  75. android:layout_width="match_parent"
  76. android:layout_height="wrap_content"
  77. android:gravity="center"
  78. android:text="发送渠道消息"
  79. android:textColor="@color/black"
  80. android:textSize="17sp" />
  81. <Button
  82. android:id="@+id/btn2"
  83. android:layout_width="match_parent"
  84. android:layout_height="wrap_content"
  85. android:gravity="center"
  86. android:text="跳转下一个页面"
  87. android:textColor="@color/black"
  88. android:textSize="17sp" />
  89. <Button
  90. android:id="@+id/btn3"
  91. android:layout_width="match_parent"
  92. android:layout_height="wrap_content"
  93. android:gravity="center"
  94. android:text="跳转下一个页面————消息推送"
  95. android:textColor="@color/black"
  96. android:textSize="17sp" />
  97. <Button
  98. android:id="@+id/btn4"
  99. android:layout_width="match_parent"
  100. android:layout_height="wrap_content"
  101. android:gravity="center"
  102. android:text="跳转下一个页面————消息推送——-计算"
  103. android:textColor="@color/black"
  104. android:textSize="17sp" />
  105. </LinearLayout>

1140aed4c08631de21535a361161599d.png

32b1e82c0bc1ea865e757b858e78f7dd.png

ccb7155ec03437f6de96a509ee4ea978.png

fa90a3270ab6296f73a9097f3457a54d.png

76b9c6fbbed7e30913f9cb785f929c80.png

代码:

  1. package com.example.myapplication;
  2. import android.app.Notification;
  3. import android.app.NotificationManager;
  4. import android.app.PendingIntent;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.os.Build;
  8. import android.os.Bundle;
  9. import android.text.TextUtils;
  10. import android.view.View;
  11. import android.widget.AdapterView;
  12. import android.widget.ArrayAdapter;
  13. import android.widget.EditText;
  14. import android.widget.Spinner;
  15. import android.widget.Toast;
  16. import androidx.appcompat.app.AppCompatActivity;
  17. public class MainActivity extends AppCompatActivity implements View.OnClickListener
  18. {
  19. private EditText et_title;
  20. private EditText et_message;
  21. private String mChannelId = "0"; // 通知渠道的编号
  22. private String mChannelName; // 通知渠道的名称
  23. private int mImportance; // 通知渠道的级别
  24. @Override
  25. protected void onCreate(Bundle savedInstanceState)
  26. {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.activity_main);
  29. et_title = findViewById(R.id.et_title);
  30. et_message = findViewById(R.id.et_message);
  31. findViewById(R.id.btn_send_channel).setOnClickListener(this);
  32. findViewById(R.id.btn2).setOnClickListener(this);
  33. findViewById(R.id.btn3).setOnClickListener(this);
  34. findViewById(R.id.btn4).setOnClickListener(this);
  35. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
  36. {
  37. initImportanceSpinner(); // 初始化渠道级别的下拉框
  38. }
  39. }
  40. // 初始化渠道级别的下拉框
  41. private void initImportanceSpinner()
  42. {
  43. findViewById(R.id.ll_channel).setVisibility(View.VISIBLE);
  44. ArrayAdapter<String> importanceAdapter = new ArrayAdapter<String>(this, R.layout.item_select, importanceDescArray);
  45. Spinner sp_importance = findViewById(R.id.sp_importance);
  46. sp_importance.setPrompt("请选择渠道级别");
  47. sp_importance.setAdapter(importanceAdapter);
  48. sp_importance.setSelection(3);
  49. sp_importance.setOnItemSelectedListener(new TypeSelectedListener());
  50. }
  51. private int[] importanceTypeArray = {NotificationManager.IMPORTANCE_NONE,
  52. NotificationManager.IMPORTANCE_MIN,
  53. NotificationManager.IMPORTANCE_LOW,
  54. NotificationManager.IMPORTANCE_DEFAULT,
  55. NotificationManager.IMPORTANCE_HIGH,
  56. NotificationManager.IMPORTANCE_MAX};
  57. private String[] importanceDescArray = {"不重要", // 无通知
  58. "最小级别", // 通知栏折叠,无提示声音,无锁屏通知
  59. "有点重要", // 通知栏展开,无提示声音,有锁屏通知
  60. "一般重要", // 通知栏展开,有提示声音,有锁屏通知
  61. "非常重要", // 通知栏展开,有提示声音,有锁屏通知,在屏幕顶部短暂悬浮(有的手机需要在设置页面开启横幅)
  62. "最高级别" // 通知栏展开,有提示声音,有锁屏通知,在屏幕顶部短暂悬浮(有的手机需要在设置页面开启横幅)
  63. };
  64. class TypeSelectedListener implements AdapterView.OnItemSelectedListener
  65. {
  66. public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
  67. {
  68. mImportance = importanceTypeArray[arg2];
  69. mChannelId = "" + arg2;
  70. mChannelName = importanceDescArray[arg2];
  71. }
  72. public void onNothingSelected(AdapterView<?> arg0)
  73. {
  74. }
  75. }
  76. // 发送指定渠道的通知消息(包括消息标题和消息内容)
  77. private void sendChannelNotify(String title, String message)
  78. {
  79. // 创建一个跳转到活动页面的意图
  80. Intent clickIntent = new Intent(this, MainActivity2.class); // MainActivity。class ---用于跳转页面
  81. // 创建一个用于页面跳转的延迟意图
  82. PendingIntent contentIntent = PendingIntent.getActivity(this, R.string.app_name, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  83. // 创建一个通知消息的建造器
  84. Notification.Builder builder = new Notification.Builder(this);
  85. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
  86. {
  87. // Android 8.0开始必须给每个通知分配对应的渠道
  88. builder = new Notification.Builder(this, mChannelId);
  89. }
  90. builder.setContentIntent(contentIntent) // 设置内容的点击意图
  91. .setAutoCancel(true) // 点击通知栏后是否自动清除该通知
  92. .setSmallIcon(R.mipmap.ic_launcher) // 设置应用名称左边的小图标
  93. .setContentTitle(title) // 设置通知栏里面的标题文本
  94. .setContentText(message); // 设置通知栏里面的内容文本
  95. Notification notify = builder.build(); // 根据通知建造器构建一个通知对象
  96. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
  97. {
  98. NotifyUtil.createNotifyChannel(this, mChannelId, mChannelName, mImportance);
  99. }
  100. // 从系统服务中获取通知管理器
  101. NotificationManager notifyMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  102. // 使用通知管理器推送通知,然后在手机的通知栏就会看到该消息,多条通知需要指定不同的通知编号
  103. notifyMgr.notify(Integer.parseInt(mChannelId), notify);
  104. if (mImportance != NotificationManager.IMPORTANCE_NONE)
  105. {
  106. Toast.makeText(this, "已发送渠道消息", Toast.LENGTH_SHORT).show();
  107. }
  108. }
  109. @Override
  110. public void onClick(View v)
  111. {
  112. if (v.getId() == R.id.btn_send_channel)
  113. {
  114. ViewUtil.hideOneInputMethod(this, et_message); // 隐藏输入法软键盘
  115. if (TextUtils.isEmpty(et_title.getText()))
  116. {
  117. Toast.makeText(this, "请填写消息标题", Toast.LENGTH_SHORT).show();
  118. return;
  119. }
  120. if (TextUtils.isEmpty(et_message.getText()))
  121. {
  122. Toast.makeText(this, "请填写消息内容", Toast.LENGTH_SHORT).show();
  123. return;
  124. }
  125. // 发送指定渠道的通知消息(包括消息标题和消息内容)
  126. sendChannelNotify(et_title.getText().toString(), et_message.getText().toString());
  127. }
  128. if (v.getId() == R.id.btn2)
  129. {
  130. startActivity(new Intent(this,MainActivity2.class));
  131. }
  132. if (v.getId() == R.id.btn3)
  133. {
  134. startActivity(new Intent(this,MainActivity3.class));
  135. }
  136. if (v.getId() == R.id.btn4)
  137. {
  138. startActivity(new Intent(this,MainActivity4.class));
  139. }
  140. }
  141. }

2444349195579e8dae871e3a777f501b.png

fde2d5b747c0c831822d79c734b527e2.png

fffe46194daa40c31611b5aa634421c7.png

3a2464e932df097649fdb5b629f68bf5.png

60b47216421296f3dad059921f42c35f.png

  1. ViewUtil
  2. package com.example.myapplication;
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.view.View;
  6. import android.view.inputmethod.InputMethodManager;
  7. public class ViewUtil
  8. {
  9. public static void hideAllInputMethod(Activity act)
  10. {
  11. // 从系统服务中获取输入法管理器
  12. InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
  13. if (imm.isActive()) // 软键盘如果已经打开则关闭之
  14. {
  15. imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
  16. }
  17. }
  18. public static void hideOneInputMethod(Activity act, View v)
  19. {
  20. // 从系统服务中获取输入法管理器
  21. InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
  22. // 关闭屏幕上的输入法软键盘
  23. imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
  24. }
  25. }

0c4457e888885fc2fae136ef060a4399.png

  1. NotifyUtil
  2. package com.example.myapplication;
  3. import android.annotation.TargetApi;
  4. import android.app.Notification;
  5. import android.app.NotificationChannel;
  6. import android.app.NotificationManager;
  7. import android.content.Context;
  8. import android.net.Uri;
  9. import android.os.Build;
  10. import android.os.Bundle;
  11. import android.util.Log;
  12. import java.lang.reflect.Field;
  13. import java.lang.reflect.Method;
  14. public class NotifyUtil
  15. {
  16. private final static String TAG = "NotifyUtil";
  17. @TargetApi(Build.VERSION_CODES.O)
  18. // 创建通知渠道。Android 8.0开始必须给每个通知分配对应的渠道
  19. public static void createNotifyChannel(Context ctx, String channelId, String channelName, int importance)
  20. {
  21. // 从系统服务中获取通知管理器
  22. NotificationManager notifyMgr = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
  23. if (notifyMgr.getNotificationChannel(channelId) == null) // 已经存在指定编号的通知渠道
  24. {
  25. // 创建指定编号、指定名称、指定级别的通知渠道
  26. NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
  27. channel.setSound(null, null); // 设置推送通知之时的铃声。null表示静音推送
  28. channel.enableLights(true); // 通知渠道是否让呼吸灯闪烁
  29. channel.enableVibration(true); // 通知渠道是否让手机震动
  30. channel.setShowBadge(true); // 通知渠道是否在应用图标的右上角展示小红点
  31. // VISIBILITY_PUBLIC显示所有通知信息,VISIBILITY_PRIVATE只显示通知标题不显示通知内容,VISIBILITY_SECRET不显示任何通知信息
  32. channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); // 设置锁屏时候的可见性
  33. channel.setImportance(importance); // 设置通知渠道的重要性级别
  34. notifyMgr.createNotificationChannel(channel); // 创建指定的通知渠道
  35. }
  36. }
  37. // 在桌面上的应用图标右上角显示数字角标
  38. public static void showMarkerCount(Context ctx, int count, Notification notify)
  39. {
  40. showBadgeOfEMUI(ctx, count); // 华为手机EMUI系统的消息角标
  41. // 小米手机还要进入设置里面的应用管理,开启当前App的“显示桌面图标角标”
  42. showBadgeOfMIUI(count, notify); // 小米手机MIUI系统的消息角标
  43. }
  44. // 华为的消息角标需要事先声明两个权限:android.permission.INTERNET、com.huawei.android.launcher.permission.CHANGE_BADGE
  45. private static void showBadgeOfEMUI(Context ctx, int count)
  46. {
  47. try
  48. {
  49. Bundle extra = new Bundle(); // 创建一个包裹对象
  50. extra.putString("package", ctx.getPackageName()); // 应用的包名
  51. // 应用的首屏页面路径
  52. extra.putString("class", ctx.getPackageName()+".MainActivity");
  53. extra.putInt("badgenumber", count); // 应用的消息数量
  54. Uri uri = Uri.parse("content://com.huawei.android.launcher.settings/badge/");
  55. // 通过内容解析器调用华为内核的消息角标服务
  56. ctx.getContentResolver().call(uri, "change_badge", null, extra);
  57. } catch (Exception e)
  58. {
  59. e.printStackTrace();
  60. }
  61. }
  62. // 小米的消息角标需要在发送通知的时候一块调用
  63. private static void showBadgeOfMIUI(int count, Notification notify)
  64. {
  65. try
  66. {
  67. // 利用反射技术获得额外的新增字段extraNotification
  68. Field field = notify.getClass().getDeclaredField("extraNotification");
  69. Log.d(TAG, "field.getName="+field.getName());
  70. // 该字段为Notification类型,下面获取它的实例对象
  71. Object extra = field.get(notify);
  72. Log.d(TAG, "extraNotification.toString="+extra.toString());
  73. // 利用反射技术获得额外的新增方法setMessageCount
  74. Method method = extra.getClass().getDeclaredMethod("setMessageCount", int.class);
  75. Log.d(TAG, "method.getName="+method.getName());
  76. // 利用反射技术调用实例对象的setMessageCount方法,设置消息角标的数量
  77. method.invoke(extra, count);
  78. Log.d(TAG, "invoke count="+count);
  79. } catch (Exception e)
  80. {
  81. e.printStackTrace();
  82. }
  83. }
  84. }

0c9c4c6637b3c0c91f58dc1b690f7db3.png

a099a1970aa407994b41115d9502ca4b.png

3df6e2a60a325c59727e7e04f0b6d99f.png

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

77ee71ee164ab0892d9c5bafd9aacc40.png

987092aa0035378ebb2bbdc54ff06a23.png

29426c4b6b3587728e23fc66d29f9370.png

b1ff3a459473a01d64db03f1a10cfa87.png

afd7d335a4c0dd499e27b5ada13c06b8.png

d273b1f4e4b79d03aea1c1a6fcad50d5.png

050db8d28e0acee5b65c18531cf45d29.png

f491dc06a5e833c249801907d909e1d4.png

183316f6052759f690b031b224d60b4e.png

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

96e9d9cda94ca0704d40cceaf3cf26b6.png

62c69a0ceab329aa247452f4129aadab.png

476f65a3067e26c760a16578afe6b109.png

ab277275e605fcef237a7a12cfb8f45f.png

49e340feb3d1224325c3dd6631aeb87f.png

发表评论

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

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

相关阅读

    相关 9.实现消息通知

     消息通知 任务队列:即传递任务的队列,可以借助任务队列实现通知的过程。 任务队列的好处:松耦合,生产者和消费者无需知道彼此的实现细节;易于扩展消费者,可以有多个

    相关 paypal消息通知IPN

    paypal支付成功时会实时的把支付交易信息返回给我们,java会返回一个payment对象,里面有交易的信息包含付款人,订单费用,订单的收货地址,收款人,交易号等信息。我们拿