当前位置:   article > 正文

android 9.0通知栏适配,可用通知栏,悬浮通知,震动,提示音,_android 9.0 展开通知栏

android 9.0 展开通知栏

直接代码,demo复制到一个新的activity可用

android 9.0,设置对应手机弹窗方式即可

 

  1. package com.example.administrator.myapplication;
  2. import android.os.Build;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.app.Notification;
  6. import android.app.NotificationChannel;
  7. import android.app.NotificationChannelGroup;
  8. import android.app.NotificationManager;
  9. import android.app.PendingIntent;
  10. import android.app.TaskStackBuilder;
  11. import android.content.Context;
  12. import android.content.Intent;
  13. import android.graphics.Color;
  14. import android.os.Bundle;
  15. import android.provider.Settings;
  16. import android.support.annotation.Nullable;
  17. import android.support.v7.app.AppCompatActivity;
  18. import android.view.View;
  19. import static android.app.Notification.BADGE_ICON_SMALL;
  20. /**
  21. * @author Administrator
  22. */
  23. public class MainActivity extends AppCompatActivity {
  24. private NotificationManager mNotificationManager;
  25. private String groupId = "groupId";
  26. private CharSequence groupName = "Group1";
  27. private String groupId2 = "groupId2";
  28. private CharSequence groupName2 = "Group2";
  29. private String chatChannelId2 = "chatChannelId2";
  30. private String adChannelId2 = "adChannelId2";
  31. private String chatChannelId = "chatChannelId";
  32. private String chatChannelName = "聊天通知";
  33. private String chatChannelDesc = "这是一个聊天通知,建议您置于开启状态,这样才不会漏掉女朋友的消息哦";
  34. private int chatChannelImportance = NotificationManager.IMPORTANCE_MAX;
  35. private String adChannelId = "adChannelId";
  36. private String adChannelName = "广告通知";
  37. private String adChannelDesc = "这是一个广告通知,可以关闭的,但是如果您希望我们做出更好的软件服务于你,请打开广告支持一下吧";
  38. private int adChannelImportance = NotificationManager.IMPORTANCE_LOW;
  39. @Override
  40. protected void onCreate(@Nullable Bundle savedInstanceState) {
  41. super.onCreate(savedInstanceState);
  42. setContentView(R.layout.activity_main);
  43. mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  44. createGroup();
  45. notification();
  46. }
  47. public void notification() {
  48. createNotificationChannel(chatChannelId, chatChannelName, chatChannelImportance, chatChannelDesc, groupId2);
  49. Notification.Builder builder = null;
  50. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
  51. builder = new Notification.Builder(this, chatChannelId);
  52. builder.setSmallIcon(R.mipmap.ic_launcher)
  53. .setContentTitle("我是标题")
  54. .setContentText("内容:Today released Android 8.0 version of its name is Oreo")
  55. .setBadgeIconType(BADGE_ICON_SMALL)
  56. .setNumber(1)
  57. .setAutoCancel(true);
  58. Intent resultIntent = new Intent(this, MainActivity.class);
  59. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
  60. stackBuilder.addParentStack(MainActivity.class);
  61. stackBuilder.addNextIntent(resultIntent);
  62. PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
  63. builder.setContentIntent(resultPendingIntent);
  64. mNotificationManager.notify((int) System.currentTimeMillis(), builder.build());
  65. }
  66. }
  67. public void notification2() {
  68. createNotificationChannel(adChannelId, adChannelName, adChannelImportance, adChannelDesc, groupId2);
  69. Notification.Builder builder = null;
  70. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
  71. builder = new Notification.Builder(this, adChannelId);
  72. builder.setSmallIcon(R.mipmap.ic_launcher)
  73. .setContentTitle("我是广告")
  74. .setContentText("内容:恰饭时间到了,Oreo is Coming.")
  75. .setBadgeIconType(BADGE_ICON_SMALL)
  76. .setNumber(1)
  77. .setAutoCancel(true);
  78. Intent resultIntent = new Intent(this, MainActivity.class);
  79. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
  80. stackBuilder.addParentStack(MainActivity.class);
  81. stackBuilder.addNextIntent(resultIntent);
  82. PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
  83. builder.setContentIntent(resultPendingIntent);
  84. mNotificationManager.notify((int) System.currentTimeMillis(), builder.build());
  85. }
  86. }
  87. public void createNotificationChannel(String id, String name, int importance, String desc, String groupId) {
  88. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  89. if (mNotificationManager.getNotificationChannel(id) != null) {
  90. return;
  91. }
  92. NotificationChannel notificationChannel = new NotificationChannel(id, name, importance);
  93. notificationChannel.enableLights(true);
  94. notificationChannel.enableVibration(true);
  95. notificationChannel.setLightColor(Color.RED);
  96. notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
  97. notificationChannel.setShowBadge(true);
  98. notificationChannel.setBypassDnd(true);
  99. notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400});
  100. notificationChannel.setDescription(desc);
  101. notificationChannel.setGroup(groupId);
  102. // notificationChannel.setSound();
  103. mNotificationManager.createNotificationChannel(notificationChannel);
  104. }
  105. }
  106. public void delNotification(View view) {
  107. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  108. mNotificationManager.deleteNotificationChannel(chatChannelId);
  109. }
  110. }
  111. public void delNotification2(View view) {
  112. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  113. mNotificationManager.deleteNotificationChannel(adChannelId);
  114. }
  115. }
  116. public void setting(View view) {
  117. Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
  118. intent.putExtra(Settings.EXTRA_CHANNEL_ID, chatChannelId);
  119. intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
  120. startActivity(intent);
  121. }
  122. public void createGroup() {
  123. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  124. mNotificationManager.createNotificationChannelGroup(new NotificationChannelGroup(groupId, groupName));
  125. mNotificationManager.createNotificationChannelGroup(new NotificationChannelGroup(groupId2, groupName2));
  126. createNotificationChannel(chatChannelId2, chatChannelName, chatChannelImportance, chatChannelDesc, groupId);
  127. createNotificationChannel(adChannelId2, adChannelName, adChannelImportance, adChannelDesc, groupId);
  128. }
  129. }
  130. public void delNotificationGroup(View view) {
  131. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  132. mNotificationManager.deleteNotificationChannelGroup(groupId2);
  133. }
  134. }
  135. }

具体可查看:http://gavinliu.cn/2017/08/22/Android-吃奥利奥系列-1-Notification/

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/111434?site
推荐阅读
相关标签
  

闽ICP备14008679号