徽章计数器索尼,三星Android Oreo和。馅饼

时间:2019-02-14 02:25:59

标签: java android notifications sony badge

我正在开发一个android应用程序,我需要在该应用程序的主图标中显示一个帐户,在android oreo中该类是Notification Badge,但在android 9中,一个Google像素不会显示该帐户,并且带有Android Pie的Sony Xperia XZ2不起作用,或者不显示未读通知图标。

我尝试使用ShortcutBadger之类的第三方库,但它们与新的android版本不兼容

NotificationChannel fyfChannel = new NotificationChannel(FYF_CHANNEL__ID, FYF_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
    fyfChannel.enableLights(true);
    fyfChannel.enableVibration(true);
    fyfChannel.setLightColor(Color.GREEN);
 fyfChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
    fyfChannel.setShowBadge(true);
    AudioAttributes audioAttributes = new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION).setUsage(AudioAttributes.USAGE_NOTIFICATION).build();
    fyfChannel.setSound( Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.audio1), audioAttributes);
    getManager().createNotificationChannel(fyfChannel);

1 个答案:

答案 0 :(得分:0)

请尝试:

String NOTIF_CHANNEL_ID = "my_notification_channel";

Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(this, NOTIF_CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentText(message)
                .setContentTitle("RideWhiz")
                .setAutoCancel(true)
                .setSound(sound)
                .setContentIntent(pendingIntent);

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

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

            // Configure the notification channel.
            NotificationChannel notifChannel = new NotificationChannel(NOTIF_CHANNEL_ID, message, NotificationManager.IMPORTANCE_DEFAULT);
            notifChannel.setDescription(message);
            notifChannel.enableLights(true);
            notifChannel.setLightColor(Color.GREEN);
            notifChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
            notifChannel.enableVibration(true);
            notificationManager.createNotificationChannel(notifChannel);
        }

        notificationManager.notify((int)System.currentTimeMillis(), notifBuilder.build());