Android 在API26之后NotificationCompat.Builder (Context context)被废弃,创建通知需要使用新的构造器NotificationCompat.Builder (Context context, String channelId)

so:

NotificationManager manager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);String CHANNEL_ID = "com.example.test";//自定义的字符串String CHANNEL_NAME = "TEST";NotificationChannel notificationChannel = null;NotificationCompat.Builder builder = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {    notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);    notificationManager.createNotificationChannel(notificationChannel);}Intent intent = new Intent(this, HomeActivity.class);PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder = new NotificationCompat.Builder(this, CHANNEL_ID).        setContentTitle(getResources().getString(R.string.app_name)).        setContentText(getResources().getString(R.string.notice_001)).        setWhen(System.currentTimeMillis()).        setSmallIcon(R.mipmap.app_icon).        setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.app_icon)).        setContentIntent(pendingIntent).setDefaults(NotificationCompat.FLAG_ONGOING_EVENT)        .setPriority(Notification.PRIORITY_MAX);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//SDK版本>=21才能设置悬挂式通知栏    builder.setCategory(String.valueOf(Notification.FLAG_ONGOING_EVENT))            .setVisibility(Notification.VISIBILITY_PUBLIC)            .setColor(getResources().getColor(R.color.white));    Intent intent2 = new Intent();    PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent2, 0);    builder.setFullScreenIntent(pi, true);}Notification notification = builder.build();manager.notify(1, notification);

更多相关文章

  1. Android(安卓)attrs文件(自定义)属性详解
  2. Android下Notification(通知栏)的使用
  3. Android(安卓)记录一个智障错误
  4. 【Unity3D】与Android相互传递消息
  5. 转:Android推送通知指南
  6. 【Unity3D】与Android相互传递消息
  7. android之调用webservice 实现图片上传下载
  8. iPhone和Android(安卓)UI模式比较列表
  9. android之调用webservice 实现图片上传下载

随机推荐

  1. android PhoneGap 的入门例子
  2. Ubuntu下连接Android设备
  3. 开发Android第四步,Android(安卓)NDK 及 a
  4. android 最全的shape属性
  5. android SQLite的使用
  6. View组件之各xml属性
  7. Android环境搭建
  8. Android(安卓)图片侧滑展示RecyclerView
  9. android的TableLayout布局界面元素填满整
  10. Android系列教程之十:Intents and Intent