我已经使用带有textview和imageview的简单布局创建了一个通知。但是在调用notificationManager.notify(...)之后不显示通知。
以下是我创建的代码
// Create notificationmanager instance
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int importance = NotificationManager.IMPORTANCE_LOW;
// Setting the channelid if versioncode is greater than Oreo's
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, importance);
notificationManager.createNotificationChannel(mChannel);
}
// Creating remote views for the notifications
RemoteViews notificationLayoutCollapsed = new RemoteViews(context.getPackageName(), R.layout.notification_layout_collapsed);
RemoteViews notificationLayoutExpanded = new RemoteViews(context.getPackageName(), R.layout.notification_layout_expanded);
// Passing the intent
Intent notificationIntent = new Intent(context, HomeActivity.class);-
// setting the flags
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
// Style MOST LIKELY ISSUE IS HERE
NotificationCompat.Style style = new androidx.media.app.NotificationCompat.DecoratedMediaCustomViewStyle();
NotificationCompat.Builder mBuilder1 = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification_icon)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(context.getString(R.string.app_name))
.setContent(notificationLayoutExpanded)
.setCustomContentView(notificationLayoutCollapsed)
.setCustomBigContentView(notificationLayoutExpanded)
.setStyle(style)
.setAutoCancel(false)
.setContentIntent(pendingIntent);
notificationManager.notify(NOTIFICATION_ID, mBuilder1.build());
我也尝试使用style = new NotificationCompat.DecoratedCustomViewStyle();
但是当我将样式设置为BigText
或BigPicture
并且未设置customcontentview时,我会看到通知。
修改
我同时使用了v4.NotificationCompat
和androidx.NotificationCompat
问题出在我的代码中,我认为缺少一些步骤。请帮助。