Newline in Notifications

Hi everyone,

I’ve been searching back and forth but I couldn’t find a solution for this. I’m using node-red for my automation and also the companion app for Android and iOS. Currently I have a function which loops about the amount of information I get from the garbage collection integration so it builds a string of the garbage I need to take out. My intention is to have a linebreak after each of the names to get a decent formatting in the notification. While I know that iOS respects the \n as a newline, the Android notification completely ignores that. So I found out that in the template node I can use <br> instead. The only issue I have is that the string for the individual names is created in the function and when using <br> the notification literally shows me a
after each name instead of a newline.
Using \n is completely ignored so I currently do not know how to enforce a linebreak in the function for Android notifications.

Does anyone know how to get this within a function loop node in node-red?

Thanks!

1 Like

EDIT: yeah not sure about node red but this works in yaml automations.

Add two newlines:

data:
  message: >
    Line 1

    Line 2

This will be received as:

Line 1
Line 2

2 Likes

Thanks @tom_l but that doesn’t apply for node-red, does it?

I’m collecting the names of the trash with this function:

for (var b in msg.payload) {
    trash += ('\n' +'- '+msg.payload[b].attributes.friendly_name);
}

This creates a string within the payload which I will than enter in a template node along with some static text. And this dynamic part should create a newline with every loop.
So while \n works on iOS, Android seems to ignore it. I couldn’t find any hint what the correct value would be for Android notifications.

Some OSes need both \n\r while others only need \n

Thanks @WallyR,
I also checked that but it would not work. It does not break into an newline but also does not show like it would when I would enter a <br> instead which is what I need to use in the template node.

We just got a PR submitted the other day to support the same new line character that iOS supports. Once merged that will work. Android supports html tags so
would be correct.

Awesome, thanks a lot for letting me know. Kinda drove me crazy that it would not work on Android but did work on iOS. :joy:

Do you know if the PR has been included in the current released version?
I upgraded everything but my notifications still won’t respect the line breaks like they are shown on iOS

yes its included, see the example in the PR

Thanks for the link.
That’s strange, I do set it like in the function above, but there are no newlines on Android. The same message sent to an iPhone breaks the lines as intended. The Android phone is a Pixel 5 so no changes to the Android system under the hood. It has the latest version running.
Can anyone verify this working on their phones, maybe I’m missing something that needs to be added here?

Share the yaml you used so we can see what it looks like.

For me, \n works in a normal notification service call. But when I tried it in the following automation \n will not work, but <br> works.


alias: Mobile Notify Anniversary
description: ''
trigger:
  - platform: time
    at: '09:00:00'
condition:
  - condition: template
    value_template: '{{ names != "" }}'
action:
  - service: notify.mobile_app_b130dl
    data:
      title: Upcoming Birthday Reminder
      message: '{{ names }}'
variables:
  names: >-
    {% set period = 50 %} 
    {% set anni_list = states.sensor | select("search",
    "sensor.anniversary_")| sort(attribute='state') | map(attribute='entity_id')
    | list %} {% set days = expand(anni_list) | sort(attribute='state') |
    map(attribute='state') | reject('in',['unavailable', 'unknown']) |
    map('int') | select('lt', period)| list| join(',') %} 
    {% set x = {"0": "today","1": "tomorrow"}%}
    {%- for a in anni_list %} {%- set d = states(a)%}
    {%- if d in days %}
    {{ state_attr(a,'friendly_name') }} is {{ x[d] | default("in "~d~" days") }}{{"<br>"}} 
    {% endif %}{% endfor %}
mode: single

what does the output of the template look like? is one of hte slashes being stripped away?

In the template editor, the {{"\n"}} version looks correct but it doesn’t come through correctly in the actual notification. In the attached screenshot, the top notification is using {{"<br>"}} as in the template I posted above, the second is using {{"\n"}}.

click on the notification to look at the details, whatever you see there is exactly what was sent to the app. You can also look at Logs under Companion App to see the JSON data sent to the app

Using {{"<br>"}}

{
"message" : "Drew's Birthday is in 24 days<br> Mark Wm's Birthday is in 48 days<br>",
"title" : "Upcoming Birthday Reminder"
}

Using {{"\n"}}

{
"message" : "Drew's Birthday is in 24 days\n Mark Wm's Birthday is in 48 days",
"title" : "Upcoming Birthday Reminder"
}

It looks like your hypothesis that a slash is being striped away was correct. If I take the output from above and use it in the service tool the notification has the correct line returns and I get the following:

{
"message" : "Drew's Birthday is in 24 days\\n Mark Wm's Birthday is in 48 days",
"title" : "Upcoming Birthday Reminder"
}

I use NodeRed to handle Notifications. There I will put things together in a message object and send this to the notify node. An example out of my flow to check if battery operated devices need their batteries replaced would be:

let name = payload.attributes.friendly_name;
let status = payload.state;
let unit = payload.attributes.unit_of_measurement;
let icon = "\uD83D\uDD0B";

switch (status) {
case (status >= 10 && status < 25):
msg.payload = 
{
    "data": {
        "title": icon + " Niedriger Batteriestand! " + icon,
        "message": name + " ist " + status + unit + "\n" +
                   "Bitte Batterie tauschen!"
    }
}
break;
case (status >= 0 && status < 10):
msg.payload = 
{
    "data": {
        "title": "\u2620\uFE0F" + icon + " Kritischer Batteriestand! " + icon + "\u2620\uFE0F",
        "message": name + " ist " + status + unit + "\n" +
                   "Bitte Batterie sofort tauschen!",
        "data": {
            'ttl': 0, 
            'group': "energy", 
            'priority': "High" 
        }
    }
}
break;
}

return msg;

Is it maybe possible that instead of \n I would need \n\r ?

Still the iPhone gets the linebreaks right from what is created above but the Android keeps ignoring them. So the function is working in general due to the result on the iPhone but is not respected on the Android.

no

I thought you already solved your issue?

I found a work-around by going back to using "<br>" in templates. However, if the goal is parity between the iOS and Android and/or being able to use the template editor to accurately predict the output of notifications on the Android companion app, then I wouldn’t call it a solution.