Android通知应用

1、使用高德定位开启后台定位

1.1创建通知

  @SuppressLint("NewApi")
    private Notification buildNotification() {

        Notification.Builder builder = null;
        Notification notification = null;
        if (android.os.Build.VERSION.SDK_INT >= 26) {
            //Android O上对Notification进行了修改,如果设置的targetSDKVersion>=26建议使用此种方式创建通知栏
            if (null == notificationManager) {
                notificationManager = (NotificationManager) BaseApplication.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);
            }
            String channelId = BaseApplication.getInstance().getPackageName();
            if (!isCreateChannel) {
                NotificationChannel notificationChannel = new NotificationChannel(channelId,
                        NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
                notificationChannel.enableLights(false);//是否在桌面icon右上角展示小圆点
                notificationChannel.setLightColor(Color.BLUE); //小圆点颜色
                /**是否显示桌面角标,默认显示*/
                notificationChannel.setShowBadge(false);
                /** 设置通知出现时声音,默认通知是有声音的*/
                notificationChannel.setSound(null, null);
                // 设置通知出现时的震动(如果 android 设备支持的话)
                notificationChannel.enableVibration(false);
                notificationManager.createNotificationChannel(notificationChannel);
                isCreateChannel = true;
            }
            builder = new Notification.Builder(BaseApplication.getInstance(), channelId);
        } else {
            builder = new Notification.Builder(BaseApplication.getInstance());
        }
        builder.setSmallIcon(R.drawable.app_logo)
                .setContentTitle("title")
                .setContentText("正在后台运行")
                .setWhen(System.currentTimeMillis());

        if (android.os.Build.VERSION.SDK_INT >= 16) {
            notification = builder.build();
        } else {
            return builder.getNotification();
        }
        return notification;
    }

注意在三星手机8.0上notificationChannel.setShowBadge(true);时,应用退出到后台会在应用桌面生成带数字1角标,不设置notificationChannel.setSound(null, null);的话,推出到后台有默认声音和震动,影响用户体验;

1.2、通过调用高德api 启动后台定位功能

  client.enableBackgroundLocation(2001, buildNotification());

2、通知的基本使用

2.1如果是Android o以上系统,需要使用通知渠道,代码如下:

  NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        String title = "消息提示";
        String desc = "您有" + num + "条未读消息";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channelId = "default";
            String channelName = "默认通知";
            NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
            channel.setShowBadge(true);
            notificationManager.createNotificationChannel(channel);
        }

2.2创建通知对象:

   Notification notification = new NotificationCompat.Builder(context, "default")
                .setContentTitle(title)
                .setContentText(desc)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setAutoCancel(true)
                .setChannelId("default")
                .setNumber(num)//桌面角标数量
                .setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
                .build();

2.3发送通知

发送通知前,先取消上一次发送的通知,代码如下:

  int notificationId =1;  
  //取消掉上一条通知消息
   notificationManager.cancel(notificationId);
   notificationManager.notify(notificationId, notification);

3、特别注意(Android o)

当如果创建通知的时候使用了 Builder(Context context, String channelId) 带有channelId的构造方法创建的通知,如下:

new NotificationCompat.Builder(getApplicationContext(),"default")

则发送通知的NotificationManager对象也需要使用channelld指定通知渠道,代码如下:

NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
//                channel.setShowBadge(true);
notificationManager.createNotificationChannel(channel);

否则发送的通知不会在通知栏显示,且桌面不显示角标

当使用Builder(Context context)方法创建的通知,则创建manager对象也可以不用指定channeld,

protected void  setNotification(Notification notification, int notificationId, Context context) {
        if (notification != null) {
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);


//            String title = "消息提示";
//            String desc = "您有" + 1 + "条未读消息";
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//                String channelId = "default";
//                String channelName = "默认通知";
//                    NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
//                    channel.setShowBadge(true);
//                    notificationManager.createNotificationChannel(channel);
//            }
            notificationManager.notify(notificationId, notification);
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值