首页 > 解决方案 > 通知未显示

问题描述

我正在尝试显示在用户屏幕上弹出的通知,例如 IMPORTANCE.HIGH 应该这样做,但是它没有显示,并且我的 logcat 中没有错误。任何帮助都会得到帮助。


private final String NOTIFICATION_CHANNEL_ID = "NOTIFICATION_CHANNEL_ID";
    private final int NOTIFICATION_ID = 2;

    private void CreateNotificationChannel() {
        if(Build.VERSION.SDK_INT == Build.VERSION_CODES.O) {
            String channel_name = getApplicationContext().getString(R.string.notification_channel_name);
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channel_name, NotificationManager.IMPORTANCE_HIGH);
            notificationChannel.enableVibration(true);
            notificationChannel.enableLights(true);

            NotificationManager notificationManager = getApplicationContext().getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(notificationChannel);

        }
    }


private void CreateNotification(String podcastName) {

        CreateNotificationChannel();

        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);


        NotificationCompat.Builder notification = new NotificationCompat.Builder(getApplicationContext(), NOTIFICATION_CHANNEL_ID)
                .setAutoCancel(true)
                .setChannelId(NOTIFICATION_CHANNEL_ID)
                .setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
                .setContentTitle("Hi")
                .setContentText("Downloading " + podcastName + " Metadata")
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setSmallIcon(R.drawable.ic_download)
                .setStyle(new NotificationCompat.BigTextStyle().bigText("Downloading " + podcastName + " Metadata"))
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);

        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(getApplicationContext());
        notificationManagerCompat.notify(NOTIFICATION_ID, notification.build());

    }

如果有帮助,这里是 ic_download.xml

<vector android:height="24dp"
    android:viewportHeight="24" android:viewportWidth="24"
    android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#FFF" android:pathData="M19.35,10.04C18.67,6.59 15.64,4 12,4 9.11,4 6.6,5.64 5.35,8.04 2.34,8.36 0,10.91 0,14c0,3.31 2.69,6 6,6h13c2.76,0 5,-2.24 5,-5 0,-2.64 -2.05,-4.78 -4.65,-4.96zM17,13l-5,5 -5,-5h3V9h4v4h3z"/>
</vector>

标签: androidandroid-notifications

解决方案


You have to create channel not only for Oreo, but for Pie, Q... too.


推荐阅读