android 8.0和android 9.0通知栏不显示解决方法

 

 

/**
    * 创建通知
    */
   private void setUpNotification() {
        if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){
           //安卓9.0发送通知
         Notification.Builder builder = null;
         RemoteViews view_custom = new RemoteViews(getPackageName(), R.layout.update_download_notification_layout);
         view_custom.setTextViewText(R.id.name, notify_name + getString(R.string.app_name));
         builder = new Notification.Builder(this, "chatChannelId");
         //setSmallIcon 必须添加否则不能在通知栏显示(Android 8.0)
         builder.setSmallIcon(R.mipmap.ic_logo)
               .setBadgeIconType(3)
               .setNumber(1)
               .setAutoCancel(true);
         Intent resultIntent = new Intent(this, LoginActivity.class);
         TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
         stackBuilder.addParentStack(IndexMainActivity.class);
         stackBuilder.addNextIntent(resultIntent);
         PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
         builder.setContentIntent(resultPendingIntent);
         //setChannelId 必须添加否则不能在通知栏显示(Android 8.0)
         builder.setChannelId(getPackageName());
         //createNotificationChannel 和 NotificationChannel必须添加,否则  Android9.0  不显示
         NotificationChannel channel = new NotificationChannel(
               getApplication().getPackageName(),
               "通知栏消息",
               NotificationManager.IMPORTANCE_DEFAULT

         );
         mNotificationManager.createNotificationChannel(channel);


         notification = builder.setContent(view_custom).build();

//       mNotificationManager.notify((int) System.currentTimeMillis(), notification);
         mNotificationManager.notify(NOTIFY_ID, notification);
      }else {
         //先设定RemoteViews
         RemoteViews view_custom = new RemoteViews(getPackageName(), R.layout.update_download_notification_layout);
         view_custom.setTextViewText(R.id.name, notify_name + getString(R.string.app_name));
         Notification.Builder builder1 = new Notification.Builder(getApplicationContext());
         builder1.setSmallIcon(R.mipmap.ic_logo); //设置图标
         builder1.setWhen(System.currentTimeMillis()); //发送时间
         notification = builder1.setContent(view_custom).build();
         mNotificationManager.notify(NOTIFY_ID, notification); // 通过通知管理器发送通知
      }
   }

 

可参考https://blog.csdn.net/qq_31392539/article/details/93799917

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值