android - 推送通知在 android 10 中不起作用
问题描述
此代码不会生成推送。
// intent used to click on notification
Intent intent = new Intent(this, AdminMainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
// 自定义通知视图 RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_push); contentView.setImageViewResource(R.id.image, R.mipmap.ic_launcher); contentView.setTextViewText(R.id.title, "mSchooling"); contentView.setTextViewText(R.id.text, message); // 通知代码 final String CHANNEL_ID = "channel_02"; NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, getString(R.string.app_name), NotificationManager.IMPORTANCE_DEFAULT); mNotificationManager.createNotificationChannel(mChannel); } NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) .setColor(Color.RED) .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) .setContentTitle( "mSchooling") .setContent(contentView) .setContentIntent(pendingIntent); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { builder.setChannelId(CHANNEL_ID); // 频道 ID } builder.setAutoCancel(true); mNotificationManager.notify(1, builder.build()); mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) .setColor(Color.RED) .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) .setContentTitle("mSchooling") .setContent (contentView) .setContentIntent(pendingIntent); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { builder.setChannelId(CHANNEL_ID); // 频道 ID } builder.setAutoCancel(true); mNotificationManager.notify(1, builder.build()); mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) .setColor(Color.RED) .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) .setContentTitle("mSchooling") .setContent (contentView) .setContentIntent(pendingIntent); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { builder.setChannelId(CHANNEL_ID); // 频道 ID } builder.setAutoCancel(true); mNotificationManager.notify(1, builder.build()); TYPE_NOTIFICATION)) .setContentTitle("mSchooling") .setContent(contentView) .setContentIntent(pendingIntent); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { builder.setChannelId(CHANNEL_ID); // 频道 ID } builder.setAutoCancel(true); mNotificationManager.notify(1, builder.build()); TYPE_NOTIFICATION)) .setContentTitle("mSchooling") .setContent(contentView) .setContentIntent(pendingIntent); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { builder.setChannelId(CHANNEL_ID); // 频道 ID } builder.setAutoCancel(true); mNotificationManager.notify(1, builder.build());
解决方案
我正在使用自定义布局进行推送,其中我有一个自定义文本视图,只是我将自定义文本视图替换为默认值,推送有效。
推荐阅读
- django - Django迁移冲突迁移图中的多个叶节点
- javascript - ckeditor4,除非需要,否则删除垂直滚动
- javascript - React Native - 将样式更改为边框颜色而不是边距
- apache-kafka - 如何控制 Kafka 集群发送的请求/消息?
- sql - 如何重新组织 postgres 表中的记录并将其永久保存
- excel - Excel 反向查找中的多重响应
- sql - 在 oracle 中对列进行分组
- amazon-web-services - ELB 在 EC2 上使用 Nginx 转移到 wordpress 显示 404
- java - 如何告诉休眠将调用哪个方法?我有一节课,两张桌子
- docker-compose - 无法从公司内部网外部访问站点时调试 Traefik