Android 通知栏显示与设置

Android通知栏

Android 基础设置和发送通知在通知栏显示。

Android 版本通知适配

Android 8.0 及以上使用通知都必须设置 NotificationChannel 类才能正常在通知栏弹出通知,只需 Application 中设置一次就可以了。 当 NotificationChannel 设置成功后具体可以在手机系统(设置 / 通知和状态栏 / 自身应用 / 通知类别)里看到详细的设置介绍。
Android 8.0 以下可直接创建通知然后发送,并不需要设置 NotificationChannel 类。

设置 NotificationChannel 代码

在继承了 Application 的类中的 onCreate() 中进行设置。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
	// 通知渠道的id(后台推送则与后台设置的Id保持一致和创建通知时传入的ChannelId保持一致)
    String channelId = "1";
    // 用户可以看到的通知渠道的名字.
    String name = "新通知";
    // 用户可以看到的通知渠道的描述
    String description = "新通知描述";
    // 该通知的重要级别
    int importance = NotificationManager.IMPORTANCE_HIGH;
    // 创建渠道
    NotificationChannel mChannel = new NotificationChannel(channelId, name, importance);
    // 配置通知渠道的属性
    mChannel.setDescription(description);
    // 设置通知出现时的闪灯(如果 android 设备支持的话)
    mChannel.enableLights(true);
    // 设置桌面图标右上角通知提示的颜色
    mChannel.setLightColor(Color.RED);
    // 设置通知出现时的震动(如果 android 设备支持的话)
    mChannel.enableVibration(true);
    // 设置发布到此频道的通知的振动模式
    mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
    // 是否在久按桌面图标时显示此渠道的通知
    mChannel.setShowBadge(true);
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
	if (mNotificationManager != null) {
		// 创建单个渠道
		mNotificationManager.createNotificationChannel(mChannel);
		// 创建多个渠道
		// List<NotificationChannel> list = new ArrayList<>();
    	// list.add(mChannel);
		// mNotificationManager.createNotificationChannels(list);
 	}
}

设置通知栏代码

当接收到服务器的推送或自身应用服务发出命令后,则创建一个通知并在通知栏弹出提示。

Notification notification;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
	Notification.Builder builder = new Notification.Builder(context, channelId);
        builder.setSmallIcon(R.mipmap.ic_launcher_round)//设置通知小图标在状态栏显示(必须设置)
        		//设置通知大图标,在通知栏显示
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
                //通知产生的时间,会在通知信息里显示,一般是系统获取到的时间
                .setWhen(System.currentTimeMillis())
                //设置该通知优先级
                .setPriority(Notification.PRIORITY_HIGH)
                //通知首次出现在通知栏,带上升动画效果的
                .setTicker(message.getTitle())
                //设置通知栏标题
                .setContentTitle(message.getTitle())
                //设置通知栏内容
                .setContentText(message.getContent())
                //设置通知出现时的震动具体值
                .setVibrate(new long[0])
                //ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
                .setOngoing(false)
                //设置这个标志当用户单击面板就可以让通知将自动取消
                .setAutoCancel(true);
                //创建PendingIntent,处理点击通知之后的逻辑
       			Intent intent = new Intent(context, MainActivity.class);
        		PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
       			builder.setContentIntent(pendingIntent);
                notification = builder.build();
} else {
	NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
        mBuilder.setContentTitle(message.getTitle())//设置通知栏标题
        		//通知首次出现在通知栏,带上升动画效果的
                .setTicker("通知来啦")
                //通知产生的时间,会在通知信息里显示,一般是系统获取到的时间
                .setWhen(System.currentTimeMillis())
                //设置该通知优先级
                .setPriority(NotificationCompat.PRIORITY_HIGH) 
                //设置通知栏内容
                .setContentText(message.getContent())
                //设置通知大图标,在通知栏显示
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
                //设置这个标志当用户单击面板就可以让通知将自动取消
                .setAutoCancel(true)
                //ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
                .setOngoing(false)
                //设置通知小图标在状态栏显示(必须设置)
                .setSmallIcon(R.mipmap.ic_launcher_round);//设置通知小ICON
        Intent intent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        mBuilder.setContentIntent(pendingIntent);
        notification = mBuilder.build();
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
	notificationManager.notify("1", notification);
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值