通知不显示
Notification Not showing up
我正在尝试显示在用户屏幕上弹出的通知,就像 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>
你不仅要为 Oreo 创建频道,还要为 Pie、Q... 创建频道。
我正在尝试显示在用户屏幕上弹出的通知,就像 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>
你不仅要为 Oreo 创建频道,还要为 Pie、Q... 创建频道。