Android 自定义通知栏带声音

最近单位要集成极光推送 而android这面需要走极光onMessage()方法自己去定义通知栏

下面开始实现

第一步   声明NotificationManager

    public static NotificationManager  notificationChannelManager;

第二步  初始化 通知栏方法  判断Build.VERSION.SDK_INT>26 才可以使用

    notificationChannelManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            AudioAttributes att = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                    .build();
            Uri huifang = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + this.getPackageName() + "/" + R.raw.huifang);
            NotificationChannel huifangcannel = new NotificationChannel("huifang", "回访提示音", NotificationManager.IMPORTANCE_HIGH);
            huifangcannel.setSound(huifang, att);//可以不调用 ,我测试没生效不知道为啥  我觉得可以注释
            huifangcannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            notificationChannelManager.createNotificationChannel(huifangcannel);

        }

第三步 因为判断了 大于sdk大于26初始化通知栏  那也得兼容小于26得啊  小于26不用处理直接上方法   这个是SDK小于26 调用此方法

   private void showTongzhi() {
        Intent intent = new Intent(MainActivity.this, Main2Activity.class);
        PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
        Notification notification = new NotificationCompat.Builder(MainActivity.this,"")
                .setContentTitle("xxx")  //设置标题
                .setContentText("您的账号已经在其他设备登录,如果不是您本人操作请及时修改密码。") //设置内容
                .setWhen(System.currentTimeMillis())  //设置时间
                .setSmallIcon(R.mipmap.ic_launcher)  //设置小图标  只能使用alpha图层的图片进行设置
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))   //设置大图标
                .setAutoCancel(true)
                .setContentIntent(pi)
                .build();
        notificationChannelManager.notify(NOTIFICATION_ID, notification);
//        index++;
    }

以上看还得有个SDK大于26的方法

  /*
     * 简单的发送通知
     */
    @SuppressLint("NewApi")
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void showNotification(String qudaoid) {
        Intent intent = new Intent(MainActivity.this, Main2Activity.class);
        PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this, qudaoid);
        builder
//                .setContent( new RemoteViews(getPackageName(),R.layout.item_new_notification))
                .setContentTitle("xxx")
                .setContentText("您的账号已经在其他设备登录,如果不是您本人操作请及时修改密码。")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setWhen(System.currentTimeMillis())
                .setContentIntent(pi)
                .setAutoCancel(true)

                .setPriority(Notification.PRIORITY_DEFAULT)     //设置该通知的优先级
                .setOngoing(true)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .setWhen(System.currentTimeMillis());
        Notification notification = builder.build();
       notificationChannelManager.notify(index, notification);
        index++;
    }

以上就完成了  下面开始调用

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    showNotification("huifang");
                } else {
                    showTongzhi();
                }
                notificationPlayVideo();

下面还有个声音提醒方法


    public void notificationPlayVideo() {
        //自定义声音
//        Uri uri  = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + this.getPackageName() + "/" + R.raw.huifang);
        //默认
        Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);//类型通知  默认
        Ringtone rt = RingtoneManager.getRingtone(getApplicationContext(), uri);
        rt.play();
    }

以上就完成了 欢迎大家点评

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值