其他分享
首页 > 其他分享> > android – 无法在频道上发布通知“null”目标Api是26

android – 无法在频道上发布通知“null”目标Api是26

作者:互联网

两个日志显示

1:对于音量控制以外的操作,不推荐使用流类型

2:请参阅setSound()的文档,了解使用什么来代替android.media.AudioAttributes来限定播放用例

Showing this

解决方法:

When you target Android 8.0 (API level 26), you must implement one or more notification channels to display notifications to your users.

int NOTIFICATION_ID = 234;

    NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);


    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {


        String CHANNEL_ID = "my_channel_01";
        CharSequence name = "my_channel";
        String Description = "This is my channel";
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
        mChannel.setDescription(Description);
        mChannel.enableLights(true);
        mChannel.setLightColor(Color.RED);
        mChannel.enableVibration(true);
        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        mChannel.setShowBadge(false);
        notificationManager.createNotificationChannel(mChannel);
    }

    NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx, CHANNEL_ID)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setContentText(message);

    Intent resultIntent = new Intent(ctx, MainActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(ctx);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    builder.setContentIntent(resultPendingIntent);

    notificationManager.notify(NOTIFICATION_ID, builder.build());

标签:android-8-0-oreo,android,android-notifications
来源: https://codeday.me/bug/20191004/1851530.html