I am trying to create a custom message and in all the situations it creates. The design is created only after the message goes to the Android notification thread, but in the first moments before it goes there and it is displayed at the front of the screen, the message is displayed according to the default NOTIFICATION ANDROID, is there a solution to make and change the TITLE and TEXT of the message even when it is at the front of the screen or change the settings of Its default?
This is my code : custom_notification.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/custom_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="@android:color/black"/>
<TextView
android:id="@+id/custom_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="@android:color/black"/>
</LinearLayout>
This is my code java:
private void showCustomNotification(Context context, String title, String message) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String channelId = "custom_channel_id";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "Custom Notifications", NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
}
RemoteViews customView = new RemoteViews(context.getPackageName(), R.layout.custom_notification);
customView.setTextViewText(R.id.custom_title, title);
customView.setTextViewText(R.id.custom_message, message);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.drawable.ic_notification)
.setCustomContentView(customView)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true);
Intent intent = new Intent(context, YourActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
notificationManager.notify(1, builder.build());
}
Z Z is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.