参考博客:https://blog.csdn.net/qi85481455/article/details/82895507
基本案例:
public voID sendNotification(VIEw vIEw){ // 设置点击通知启动 意图 // Intent intent = new Intent(this,MainActivity.class); Intent intent = new Intent(); // 通过通知意图启动拨打电话界面 intent.setAction(Intent.ACTION_DIAL); intent.setData(Uri.parse("tel:"+110)); PendingIntent pi = PendingIntent.getActivity(MainActivity.this,0,intent,0); notificationmanager manager = (notificationmanager) getSystemService(NOTIFICATION_SERVICE); Notification.Builder builder=new Notification.Builder(this); builder.setContentTitle("小标题"); builder.setContentText("通知内容"); builder.setSmallicon(R.mipmap.ic_launcher); // 小图标 builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)); // 大图标 builder.setWhen(System.currentTimeMillis()); //设置时间 builder.setautoCancel(true); // 点击启动Activity以后自动取消 builder.setContentIntent(pi); Notification nf=builder.build(); nf.flags= Notification.FLAG_INSISTENT; // 设置以后通知栏不能被取消,不起作用???? manager.notify(1,nf); // 1 是通知ID 可以根据1 取消通知 }
2. andorID 8.0 以后通知参考郭神博客:https://blog.csdn.net/guolin_blog/article/details/79854070
androID 8.0 系统通知问题:通知不能分类,如果关闭通知栏开关以后,那么所有的通知都不能收到
androID 8.0引入到了通知渠道概念,通知分组
比如把 有聊天 消息、订阅消息,把 聊天消息放入放入一个通知渠道 ,设置高优先级,
那么这个时候可以关闭订阅消息通知,
还可以在通知栏中右滑,设置 再次显示通知时间, 通知通知开关对这个类别
基本案例:
package com.notificationdemo.denganzhi;import androID.annotation.TargetAPI;import androID.app.Notification;import androID.app.NotificationChannel;import androID.app.notificationmanager;import androID.content.Context;import androID.content.Intent;import androID.graphics.BitmapFactory;import androID.opengl.Visibility;import androID.os.Build;import androID.provIDer.Settings;import androID.support.v4.app.NotificationCompat;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.Toast;public class MainActivity extends AppCompatActivity { String channelID1 = "chat"; String channelID2 = "subscribe"; // 1. 创建通知渠道 @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { String channelname = "聊天消息"; /*** * 通知等级: * notificationmanager.importANCE_HIGH; * importANCE_DEFAulT * importANCE_LOW * importANCE_MIN */ int importance = notificationmanager.importANCE_HIGH; createNotificationChannel(channelID1, channelname, importance); channelname = "订阅消息"; importance = notificationmanager.importANCE_LOW; createNotificationChannel(channelID2, channelname, importance); } } @TargetAPI(Build.VERSION_CODES.O) private voID createNotificationChannel(String channelID, String channelname, int importance) { NotificationChannel channel = new NotificationChannel(channelID, channelname, importance); channel.setShowBadge(true); // 允许显示角标 notificationmanager notificationmanager = (notificationmanager) getSystemService( NOTIFICATION_SERVICE); notificationmanager.createNotificationChannel(channel); } // 2. 发送 聊挺通知 ,使用 channelID1 聊天渠道 public voID sendNotifiction1(VIEw vIEw){ notificationmanager manager = (notificationmanager) getSystemService(NOTIFICATION_SERVICE); Notification notification = new NotificationCompat.Builder(this, channelID1) .setContentTitle("收到一条聊天消息") .setContentText("今天中午吃什么?") .setWhen(System.currentTimeMillis()) .setSmallicon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) .setautoCancel(true) .setNumber(1000) // 未读消息 .build(); manager.notify(1, notification); } // 3. 发送 订阅通知,使用通知渠道 channelID2 public voID sendNotifiction2(VIEw vIEw){ notificationmanager manager = (notificationmanager) getSystemService(NOTIFICATION_SERVICE); Notification notification = new NotificationCompat.Builder(this, channelID2) .setContentTitle("发送一条订阅消息") .setContentText("地铁10号线商铺抢购中!!!!") .setWhen(System.currentTimeMillis()) .setSmallicon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) .setautoCancel(true) .build(); manager.notify(2, notification); }}
问题: 如果用户关闭聊天通知开关,那么用户无法使用通知,动态判断通知栏
package com.notificationdemo.denganzhi;import androID.annotation.TargetAPI;import androID.app.Notification;import androID.app.NotificationChannel;import androID.app.notificationmanager;import androID.content.Context;import androID.content.Intent;import androID.graphics.BitmapFactory;import androID.opengl.Visibility;import androID.os.Build;import androID.provIDer.Settings;import androID.support.v4.app.NotificationCompat;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.Toast;public class MainActivity extends AppCompatActivity { String channelID1 = "chat"; // 1. 创建通知渠道 @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { String channelname = "聊天消息"; /*** * 通知等级: * notificationmanager.importANCE_HIGH; * importANCE_DEFAulT * importANCE_LOW * importANCE_MIN */ int importance = notificationmanager.importANCE_HIGH; // 只会调用一次,系统有优化 createNotificationChannel(channelID1, channelname, importance) } } @TargetAPI(Build.VERSION_CODES.O) private voID createNotificationChannel(String channelID, String channelname, int importance) { NotificationChannel channel = new NotificationChannel(channelID, channelname, importance); channel.setShowBadge(true); // 允许显示角标 notificationmanager notificationmanager = (notificationmanager) getSystemService( NOTIFICATION_SERVICE); notificationmanager.createNotificationChannel(channel); } public voID sendNotifiction3(VIEw vIEw){ /** * 如果 channelID1 通知渠道开关关闭, * 比如聊天 功能无法使用 * 可以通过 API判断 改通知去掉 开关状态, * 如果用户关闭,那么手动去打开 */ notificationmanager manager = (notificationmanager) getSystemService(NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = manager.getNotificationChannel(channelID1); // 获取通知 渠道配置 importANCE_NONE 表示 关闭通知渠道了 if (channel.getimportance() == notificationmanager.importANCE_NONE) { Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS); intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackagename()); intent.putExtra(Settings.EXTRA_CHANNEL_ID, channel.getID()); startActivity(intent); Toast.makeText(this, "请手动将通知打开", Toast.LENGTH_SHORT).show(); } } }}
总结
以上是内存溢出为你收集整理的 android 通知栏理解使用全部内容,希望文章能够帮你解决 android 通知栏理解使用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)