Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android custom notification not showing code changes

There's some weird behavior here. I've got a bunch of code, pictures, followed by some description of the weird behavior and then my questions.

Files

I have a file random_start_bonus_cards_notification.xml, which looks like:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:gravity="center">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/firstCard" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/secondCard" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ifNotCoal"
        android:visibility="gone">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="("
            android:paddingTop="15dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/if_not_coal"
            android:paddingTop="15dp" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/thirdCard" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=")"
            android:paddingTop="15dp" />

    </LinearLayout>

And a file random_start_bonus_cards.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"></RelativeLayout>

This is a deliberately blank layout, because of weirdness we'll get into. It turns out this file has to be here, but it doesn't matter what it contains.

Annnnd my styles.xml:

<resources>
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
</style>

<style name="NotificationTitle">
    <item name="android:textColor">?android:attr/textColorPrimaryInverse</item>
    <item name="android:textStyle">bold</item>
</style>
<style name="NotificationText">
    <item name="android:textColor">?android:attr/textColorPrimaryInverse</item>
</style>

And some Android code:

    private void showStartBonusCardsDialog(DrawnCards drawnCards) {
    LayoutInflater factory = LayoutInflater.from(CalculatorActivity.this);
    final View dialogContent = factory.inflate(R.layout.random_start_bonus_cards_notification, null);
    ((ImageView) dialogContent.findViewById(R.id.firstCard)).setImageResource(getDrawableIdForStartCard(drawnCards.firstCard()));
    ((ImageView) dialogContent.findViewById(R.id.secondCard)).setImageResource(getDrawableIdForStartCard(drawnCards.secondCard()));

    if(drawnCards.hasThirdCard()) {
        dialogContent.findViewById(R.id.ifNotCoal).setVisibility(View.VISIBLE);
        ((ImageView) dialogContent.findViewById(R.id.thirdCard)).setImageResource(getDrawableIdForStartCard(drawnCards.thirdCard()));
    } else {
        dialogContent.findViewById(R.id.ifNotCoal).setVisibility(View.GONE);
    }

    AlertDialog drawnCardsDialog = new AlertDialog.Builder(CalculatorActivity.this).create();
    drawnCardsDialog.setView(dialogContent);
    drawnCardsDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); }
    });
    drawnCardsDialog.show();
}

private void showStartBonusCardsNotification(DrawnCards drawnCards) {
    Intent openIntent = new Intent(CalculatorActivity.this, CalculatorActivity.class);
    openIntent.setAction(refreshStartCardsAction);
    PendingIntent openPendingIntent = PendingIntent.getActivity(this, 0, openIntent, 0);

    RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.random_start_bonus_cards_notification);
    notificationView.setImageViewResource(R.id.firstCard, getDrawableIdForStartCard(drawnCards.firstCard()));
    notificationView.setImageViewResource(R.id.secondCard, getDrawableIdForStartCard(drawnCards.secondCard()));
    if(drawnCards.hasThirdCard()) {
        notificationView.setViewVisibility(R.id.ifNotCoal, View.VISIBLE);
        notificationView.setImageViewResource(R.id.thirdCard, getDrawableIdForStartCard(drawnCards.thirdCard()));
    } else {
        notificationView.setViewVisibility(R.id.ifNotCoal, View.GONE);
    }

    NotificationCompat.Builder notification = new NotificationCompat.Builder(this);
    notification.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
    notification.setSmallIcon(R.drawable.white);
    notification.setContentIntent(openPendingIntent);
    notification.setContent(notificationView);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    int notificationId = 1;
    notificationManager.notify(notificationId, notification.build());
}

And here's two screenshots to refer to: Dialog Notification

Weirdness

I initially had just one layout file, since the layouts were identical. With this, the text colour of the dialog was black (good) and the text colour of the notification was white and couldn't be read on my HTC M9.

(1) If I put the XML from random_start_bonus_cards_notification.xml into random_start_bonus_cards.xml, and replace all usages of random_start_bonus_cards_notification with random_start_bonus_cards, the text colours are wrong. If I keep the code as-is, but delete random_start_bonus_cards, the text colours are wrong. If I use random_start_bonus_cards_notification.xml and keep random_start_bonus_cards.xml in the project, even if it's empty, I get black text in both the dialog and notification!

(2) The background on the notification is a pale green. This colour was in the resource file at one point, but has been removed (as shown). After removing the background colour the dialog layout has a white background, but the notification background is still pale green, no matter whether I change it to red or anything.

(3) If I stop calling the dialog code, the same behavior occurs. I've included the dialog code since I'm worried there might be some interaction here, but I don't see how.

Questions

Generally, how does RemoteViews work with custom notification layouts? Is there any kind of system caching that could account for the background colour not changing?

Why is my notification not changing background colour when I change the layout background colour?

Why does my app change behavior when I delete this empty file random_start_bonus_cards.xml, that isn't even being used?

Why does my notification text colour not work unless I use the file named random_start_bonus_cards_notification.xml? (If I remove the _notification, the text changes colour).

Thanks for reading!

like image 660
rythos42 Avatar asked Jul 19 '16 18:07

rythos42


People also ask

Why are my notifications not showing up on Android?

Cause of Notifications Not Showing up on AndroidDo Not Disturb or Airplane Mode is on. Either system or app notifications are disabled. Power or data settings are preventing apps from retrieving notification alerts. Outdated apps or OS software can cause apps to freeze or crash and not deliver notifications.

How to create custom notification layouts and text colors in Android?

This example demonstrates how to create custom notification layouts and text colors in Android . Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.

Why are notifications not showing up on my Android device?

Here are some of the ways to fix it when notifications are not showing up on your Android device. Check That Do Not Disturb is not enabled. It may seem obvious, but forgetting Do Not Disturb is enabled is one of the most common causes for not receiving notifications. If this setting is on (enabled), turn it off, and they will start working again.

How to fix Android 11 push notifications not working?

So, most of the time, the issue with Android 11 push notifications not working is caused by faulty settings. To resolve this problem: Go to Settings > Notifications > Apps & Notifications. Tap Notifications on the lock screen or On the lock screen under ‘Lock screen.’ 10. Enable app notification settings

How will Android 12 notifications work with custom content views?

For apps targeting Android 12, notifications with custom content views will no longer use the full notification area; instead, the system applies a standard template.


2 Answers

My guess is that this is happenning for one of the following two reasons:

  1. The first of these could be that somewhere else in your code, you are refering to the creation of the notification, and there the changes you have made would not affect the code.

  2. This is more likely to happen. The build has broken down and your built version of the app is not using the latest code. You can solve this by rebuilding the app from your IDE.

If you still can't manage to get hold of why this is happenning, set a breakpoint just before calling the method to display the notification and follow the logic the app is using.

Hope this helps :)

like image 179
Michele La Ferla Avatar answered Oct 21 '22 05:10

Michele La Ferla


This was maddening, and is still not great.

(1) After confirming that my phone was doing some caching, I started using the emulator and it worked better.

(2) I left the notification with a separate layout files from the dialog, even though they are identical. My reasoning is that apparently the notification background can change between Android releases (there's a lot of complaints).

(3) I didn't post it above, but at one point I had a "v9/styles.xml". I didn't realize this applied to all versions above API9.

The final solution looks a bit like the following code. I copied from this SO answer (5.1.1 notification TextView, the correct way to deal with white text?) and used textAppearance instead of style.

        <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/if_not_coal"
        android:textAppearance="@style/NotificationText"
        android:paddingTop="15dp" />

textAppearance is great.

<style name="NotificationText" parent="android:TextAppearance.StatusBar.EventContent" />

Gives the text a greyish colour for < API 21.

<style name="NotificationText" parent="@android:style/TextAppearance.Material.Notification.Title"></style>

Gives the text a black colour for API >= 21.

like image 33
rythos42 Avatar answered Oct 21 '22 04:10

rythos42