首页 > 解决方案 > 推送通知在 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());

标签: android

解决方案


我正在使用自定义布局进行推送,其中我有一个自定义文本视图,只是我将自定义文本视图替换为默认值,推送有效。


推荐阅读