Android Notification使用

一、测试环境

直接用得话使用最下面的工具类

  • 基于android.support环境,androidx环境参考官方demo,也可以自己进行改动。换下包名
  • 编译环境27、29都测试过,
  • 最低版本限制16
  • 悬浮窗在小米上测试过,需要手动开启悬浮窗权限
  • 文档待整理

二、相关代码

可使用代码:

当用户开通允许悬浮通知的设置后,可以悬浮显示通知(5.0后有效)

NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(this);
        Intent intent = new Intent(Main2Activity.this,ReceiveMailActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        builder.setContentIntent(pendingIntent);
        builder.setSmallIcon(R.drawable.logo);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.logo));
        builder.setAutoCancel(true);
        builder.setContentTitle("悬挂式通知");
        builder.setFullScreenIntent(pendingIntent, true);
        mNotificationManager.notify(2, builder.build());

国产手机默认只给微信、QQ这类程序开横幅(即提醒式通知),其余应用需要手动去设置里面选择打开横幅才会有

https://www.jianshu.com/p/20ad37d1418b
https://www.jianshu.com/p/97f9f20ab36d
PendingIntent notifyPendingIntent =
                PendingIntent.getActivity(
                        build.context,
                        1,
                        notifyIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT//更新之前的消息,使用FLAG_CANCEL_CURRENT时应保持requestCode每次更新一次
                );
/** 通知工具类
	代码基于android.support环境
*/
public class NotificationUtil {

    public void showNotification(NotificationUtilBuild build) {
        if (null == build){
            throw new RuntimeException("NotificationUtilBuild 不能为空");
        }
        NotificationManager notificationManager =
                (NotificationManager) build.context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_HIGH;//5.0以上会显示横幅
            //注意Name和description不能为null或者""
            NotificationChannel mChannel = new NotificationChannel(build.channelId, build.channelName, importance);
            // 配置通知渠道的属性
            mChannel.setDescription(build.description);
            notificationManager.createNotificationChannel(mChannel);
        }

        Intent notifyIntent = new Intent(build.context, MainActivity.class);
        PendingIntent notifyPendingIntent =
                PendingIntent.getActivity(
                        build.context,
                        1,
                        notifyIntent,
                        PendingIntent.FLAG_CANCEL_CURRENT
                );

        NotificationCompat.Builder notificationCompatBuilder = new NotificationCompat.Builder( build.context, build.channelId);
//Notification.Builder builder = new Notification.Builder(this);//不要使用这个,否则,弹出的通知不显示时间        
        notificationCompatBuilder.setAutoCancel(true);
        notificationCompatBuilder.setContentTitle(build.title);
        notificationCompatBuilder.setContentText(build.content);
        notificationCompatBuilder.setSmallIcon(R.mipmap.ic_launcher);
        notificationCompatBuilder.setContentIntent(notifyPendingIntent);
        notificationCompatBuilder.setPriority(NotificationManagerCompat.IMPORTANCE_HIGH);
        notificationCompatBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
//		 builder.setColor()//这个没效果,待测试
//       builder.setColor(ContextCompat.getColor())//这个有效果        
 //     自定义View-------start       
//        RemoteViews notificationLayout = new RemoteViews(getPackageName(), //R.layout.notification_custom_layout);

//        notificationCompatBuilder.setStyle(new //NotificationCompat.DecoratedCustomViewStyle());//完全自定义的话去掉这句话
//        notificationCompatBuilder.setCustomContentView(notificationLayout);
//     自定义View-------end 
        Notification notification = notificationCompatBuilder.build();
        notificationManager.notify(build.notificationId, notification);
    }

    public NotificationUtilBuild createBuild(){
        return new NotificationUtilBuild();
    }

    /** 通知工具类的Build */
    public class NotificationUtilBuild{
        public int notificationId = 1;
        public String notificationTag = "chat";
        public Context context;
        public String title = "";
        public String content = "";
        public String time = "";
        public String channelId = "chat";
        public String channelName = "聊天";
        public String description = "聊天通知";
    }

}

三、注意事项

Notification布局文件根布局的宽高如果需要设置宽高的话不可以写@dimien/height_60的样式,只能直接写60dp,否则在部分手机上会崩溃

四、参考链接

参考文档:

https://blog.csdn.net/qq_32209403/article/details/79238786

https://www.jianshu.com/p/c5aa2ba09925

https://blog.csdn.net/guolin_blog/article/details/79854070

https://blog.csdn.net/LosingCarryJie/article/details/79357257

https://blog.csdn.net/qq_35644307/article/details/85304874

https://blog.csdn.net/guolin_blog/article/details/79854070

https://blog.csdn.net/tiankongcheng6/article/details/78183377

https://blog.csdn.net/xialong_927/article/details/80253523 (广播接受)

https://blog.csdn.net/weixin_38196407/article/details/89556023 ( 据郭霖大神解释, 国产手机默认只给微信、QQ这类程序开横幅(即提醒式通知),其余应用需要手动去设置里面选择打开横幅才会有

google示例代码:

https://developer.android.com/training/notify-user/custom-notification

google示例合集:

https://github.com/android/user-interface-samples

官方:http://developer.android.com/design/patterns/notifications.html

译文:http://adchs.github.io/patterns/notifications.html

使用教程 :http://developer.android.com/training/notify-user/index.html

开发文档 :http://developer.android.com/reference/android/app/Notification.html

在Android13中通知栏会像用户所要权限,参考以下链接:
https://developer.android.google.cn/about/versions/13/changes/notification-permission?hl=zh-cn

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值