zoukankan      html  css  js  c++  java
  • 如何解决android 通知栏不显示的问题

    android 8.0 以后的版本,在创建通知栏的时候,加了一个channelId的东西。要想在上述版本中显示通知,总共分两步

    1.创建Channel

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
      String channelId = "whatever"; //根据业务执行
      String channelName = "whatever conent"; //这个是channelid 的解释,在安装的时候会展示给用户看
      int importance = NotificationManager.IMPORTANCE_HIGH;
      createNotificationChannel(channelId, channelName, importance);
    
    }

    2.引用

    Notification notification = new Notification.Builder(this,"whatever") //引用加上channelid
      .setSmallIcon(R.drawable.donkey)
      .setWhen(System.currentTimeMillis())
      .setContentTitle("随便")
      .setContentText("随随便便写")
      .setContentIntent(pendingIntent)
      .build();

    为了兼容android所有版本,最好在代码里做一下适配

    manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    
    Intent intent = new Intent(this, AudioPlayerActivity.class);
    intent.putExtra("Notifiction",true);
    
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
      String channelId = "whatever"; //根据业务执行
      String channelName = "whatever conent"; //这个是channelid 的解释,在安装的时候会展示给用户看
      int importance = NotificationManager.IMPORTANCE_HIGH;
      createNotificationChannel(channelId, channelName, importance);
    
    }
    
    PendingIntent pendingIntent = PendingIntent.getActivity(this,1,intent,PendingIntent.FLAG_UPDATE_CURRENT);
    Notification notification = null;
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
    
      notification = new Notification.Builder(this,"whatever") //引用加上channelid
        .setSmallIcon(R.drawable.donkey)
        .setWhen(System.currentTimeMillis())
        .setContentTitle("随便")
        .setContentText("随随便便写")
        .setContentIntent(pendingIntent)
        .build();
    }else{
      notification = new Notification.Builder(this)
        .setSmallIcon(R.drawable.donkey)
        .setWhen(System.currentTimeMillis())
        .setContentTitle("随便")
        .setContentText("随随便便写")
        .setContentIntent(pendingIntent)
        .build();
    }
    
    manager.notify(1,notification);
    请关于一下啦^_^

    微信公众号

  • 相关阅读:
    MySQL锁之三:MySQL的共享锁与排它锁编码演示
    服务链路追踪(Spring Cloud Sleuth)
    服务网关zuul之四:zuul网关配置
    hdu 1505 City Game (hdu1506加强版)
    PHP设计模式——訪问者模式
    极客互联网电视不是噱头,用户体验成创维G7200核心竞争力
    深入理解JavaScript系列(23):JavaScript与DOM(上)——也适用于新手
    使用php分页类实现简单分类
    管理之路(四)
    poj 2485 Highways (最小生成树)
  • 原文地址:https://www.cnblogs.com/haloujava/p/11376290.html
Copyright © 2011-2022 走看看