android 9.0通知栏适配,可用通知栏,悬浮通知,震动,提示音,
16lz
2021-12-04
直接代码,demo复制到一个新的activity可用
android 9.0,设置对应手机弹窗方式即可
package com.example.administrator.myapplication;import android.os.Build;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.app.Notification;import android.app.NotificationChannel;import android.app.NotificationChannelGroup;import android.app.NotificationManager;import android.app.PendingIntent;import android.app.TaskStackBuilder;import android.content.Context;import android.content.Intent;import android.graphics.Color;import android.os.Bundle;import android.provider.Settings;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.view.View;import static android.app.Notification.BADGE_ICON_SMALL;/** * @author Administrator */public class MainActivity extends AppCompatActivity { private NotificationManager mNotificationManager; private String groupId = "groupId"; private CharSequence groupName = "Group1"; private String groupId2 = "groupId2"; private CharSequence groupName2 = "Group2"; private String chatChannelId2 = "chatChannelId2"; private String adChannelId2 = "adChannelId2"; private String chatChannelId = "chatChannelId"; private String chatChannelName = "聊天通知"; private String chatChannelDesc = "这是一个聊天通知,建议您置于开启状态,这样才不会漏掉女朋友的消息哦"; private int chatChannelImportance = NotificationManager.IMPORTANCE_MAX; private String adChannelId = "adChannelId"; private String adChannelName = "广告通知"; private String adChannelDesc = "这是一个广告通知,可以关闭的,但是如果您希望我们做出更好的软件服务于你,请打开广告支持一下吧"; private int adChannelImportance = NotificationManager.IMPORTANCE_LOW; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); createGroup(); notification(); } public void notification() { createNotificationChannel(chatChannelId, chatChannelName, chatChannelImportance, chatChannelDesc, groupId2); Notification.Builder builder = null; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { builder = new Notification.Builder(this, chatChannelId); builder.setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("我是标题") .setContentText("内容:Today released Android 8.0 version of its name is Oreo") .setBadgeIconType(BADGE_ICON_SMALL) .setNumber(1) .setAutoCancel(true); Intent resultIntent = new Intent(this, MainActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addParentStack(MainActivity.class); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(resultPendingIntent); mNotificationManager.notify((int) System.currentTimeMillis(), builder.build()); } } public void notification2() { createNotificationChannel(adChannelId, adChannelName, adChannelImportance, adChannelDesc, groupId2); Notification.Builder builder = null; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { builder = new Notification.Builder(this, adChannelId); builder.setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("我是广告") .setContentText("内容:恰饭时间到了,Oreo is Coming.") .setBadgeIconType(BADGE_ICON_SMALL) .setNumber(1) .setAutoCancel(true); Intent resultIntent = new Intent(this, MainActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addParentStack(MainActivity.class); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(resultPendingIntent); mNotificationManager.notify((int) System.currentTimeMillis(), builder.build()); } } public void createNotificationChannel(String id, String name, int importance, String desc, String groupId) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (mNotificationManager.getNotificationChannel(id) != null) { return; } NotificationChannel notificationChannel = new NotificationChannel(id, name, importance); notificationChannel.enableLights(true); notificationChannel.enableVibration(true); notificationChannel.setLightColor(Color.RED); notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); notificationChannel.setShowBadge(true); notificationChannel.setBypassDnd(true); notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400}); notificationChannel.setDescription(desc); notificationChannel.setGroup(groupId);// notificationChannel.setSound(); mNotificationManager.createNotificationChannel(notificationChannel); } } public void delNotification(View view) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { mNotificationManager.deleteNotificationChannel(chatChannelId); } } public void delNotification2(View view) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { mNotificationManager.deleteNotificationChannel(adChannelId); } } public void setting(View view) { Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS); intent.putExtra(Settings.EXTRA_CHANNEL_ID, chatChannelId); intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName()); startActivity(intent); } public void createGroup() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { mNotificationManager.createNotificationChannelGroup(new NotificationChannelGroup(groupId, groupName)); mNotificationManager.createNotificationChannelGroup(new NotificationChannelGroup(groupId2, groupName2)); createNotificationChannel(chatChannelId2, chatChannelName, chatChannelImportance, chatChannelDesc, groupId); createNotificationChannel(adChannelId2, adChannelName, adChannelImportance, adChannelDesc, groupId); } } public void delNotificationGroup(View view) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { mNotificationManager.deleteNotificationChannelGroup(groupId2); } }}
具体可查看:http://gavinliu.cn/2017/08/22/Android-吃奥利奥系列-1-Notification/
更多相关文章
- Android中的Notification的使用
- android通知栏进度条
- Android通知推送 ——采用MQTT协议实现Android推送
- android Lollipop勿扰模式
- Android(安卓)开发问题总结 四
- Android(六)通知、样式、主题、HTML
- android v7兼容包RecyclerView的使用(一)
- Android学习系列(2)--App自动更新之通知栏下载
- 一年学习总结和分享android广告赚钱