Node Red to Android App Notifications Delayed

When I send notifications to the Android App they are often delayed. I have turned off battery optimization for the app.

I’m sending this function node (I believe “importance” has replace “priority” but I have tried both with the same results):

msg.payload = {
    domain: 'notify',
    data: {
        "message":"Front Door Bell",
        "title":"Front Door Bell",
        "data":
            {
            "channel":"Front Doorbell",
            "importance":"high",
            "ttl": 0,
            }
    }
}
return msg;

Into this Call Service Node

Why would these messages be delayed, often until I activate the screen?

Cheers, Richard

I believe your yaml/JSON is malformed.

The output is probably nested too much. Your putting everything in the message-key, that means it shouldn’t work in my opinion, but if it does then I can see that the data parameters is not read.

What I believe is correct is to replace the JSON in the call service with just {{payload}} because the payload has all the parameters.

I think the function needs to be trimmed also.

msg.payload = {
        "message":"Front Door Bell",
        "title":"Front Door Bell",
        "data":
            {
            "channel":"Front Doorbell",
            "importance":"high",
            "ttl": 0,
            }
}
return msg;

Because notify is already set in the call service.

you need to use priority in place of importance always follow the docs: Critical notifications | Home Assistant Companion Docs

I had those same thoughts. But when I do exactly that, I get an API Error in Node Red, whether I use {"message":"{{payload}}"} or just {{payload}} as you suggested

Always follow the docs is good advice, and that’s exactly what I did. Not only did I read the companion Docs, but I also read the Android Docs which say for Android 8.0 and above to use Importance.

Regardless of which should prevail, you may note in my original question I point out that I have tried both with the same results.

So notifications also use Firebase which uses priority the link you sent is for the importance of a notification channel which controls the appearance of the notification once received. Firebase has to first send the notification to the device.

double check that the app has battery optimizations disabled and that you have also checked any additional setting your device may have. Samsung devices also have power saving features which need to be disabled for example.

So many questions, let’s start with #1.
Why are you using “quotes” for some keys and not for others?
msg.payload.domain = 'notify' but msg.payload.data."message" somewhere else? Things aren’t magically different inside the data object, it all gets converted to JSON anyway before it is sent to Home Assistant.

Next - by writing:

{"message":"{{payload}}"}

in the data field - it means the JSON delivered to Home Assistant looks like this:

{
    "message": {
      "domain": "notify",
      "data": {
        "message":"Front Door Bell",
        "title":"Front Door Bell",
        "data": {
          "channel":"Front Doorbell",
          "importance":"high",
          "ttl": 0,
        }
    }
}
}

Which is clearly not what you intended?

The YAML for which looks like this:

message:
  domain: notify
  data:
    message: Front Door Bell
    title: Front Door Bell
    data:
      channel: Front Doorbell
      importance: high
      ttl: 0

If your msg.payload was simply this:

msg.payload = 
{
  "message": "Front Door Bell",
  "title": "Front Door Bell",
  "data": {
    "channel": "Front Doorbell",
    "priority": "high",
    "ttl": 0
  }
}

Which looks like this in YAML:

message: Front Door Bell
title: Front Door Bell
data:
  channel: Front Doorbell
  priority: high
  ttl: 0

Because I am pretty sure that adding anything in the data field in nodered, automatically adds the data: part. So all you need to put in the data field in nodered would be {{ payload }}

1 Like

Now you’re holding back information.
There must be something more in the debug window.

1 Like

Thanks to all for responses.

Good to know. However, that still won’t solve the problem – since I wasn’t sure, I’ve already been testing both ways.

Good advice. But I think I’ve covered that. I’m using a One Plus. Battery Saver is off and app is set to Don’t optimize.

No good reason. Is that really the #1 question? Anyway, as pointed out I can eliminate the line with ‘notify’ so everything will be in double-quotes.

SUMMARY OF ACTUAL RESULTS

If I clean up my original code like this and send it to {"message":"{{payload}}"} it works, but is still sometimes delayed until I turn on the phone screen (no different than where I started).

msg.payload = {
    data: {
        "message":"Front Door Bell",
        "title":"Front Door Bell",
        "data":
            {
            "channel":"Front Doorbell",
            "priority":"high",
            "ttl": 0,
            }
    }
}
return msg;

If I send the Hellis81 or Andrew Jones code to {"message":"{{payload}}"} I get the error
"Call-service error. extra keys not allowed @ data['channel']"

If I send the Hellis81 or Andrew Jones code to {{payload}} I get the error
"Call-service error. extra keys not allowed @ data['0']"

Maybe Node Red doesn’t automatically add the ‘data:’ part. I don’t know.

Here are the nodes if anyone wants to look

[{"id":"6cc3ee53.4cd89","type":"inject","z":"786db0c1.8b55b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":920,"y":1280,"wires":[["a421aeda10488a62"]]},{"id":"b1bf4f9d19e8063a","type":"api-call-service","z":"786db0c1.8b55b","name":"Notify M:P","server":"2bcf671a.f2b3c8","version":3,"debugenabled":false,"service_domain":"notify","service":"mobile_app_dad_1_8","entityId":"","data":"{\"message\":\"{{payload}}\"}","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1390,"y":1160,"wires":[["8ff5f6df3bdc7f6e"]]},{"id":"a421aeda10488a62","type":"function","z":"786db0c1.8b55b","name":"Jones","func":"msg.payload = \n{\n  \"message\": \"Front Door Bell\",\n  \"title\": \"Front Door Bell\",\n  \"data\": {\n    \"channel\": \"Front Doorbell\",\n    \"priority\": \"high\",\n    \"ttl\": 0\n  }\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1110,"y":1280,"wires":[["4a0be93a8a8c2fa5","b1bf4f9d19e8063a"]]},{"id":"8ff5f6df3bdc7f6e","type":"debug","z":"786db0c1.8b55b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1630,"y":1200,"wires":[]},{"id":"4a0be93a8a8c2fa5","type":"api-call-service","z":"786db0c1.8b55b","name":"Notify P","server":"2bcf671a.f2b3c8","version":3,"debugenabled":false,"service_domain":"notify","service":"mobile_app_dad_1_8","entityId":"","data":"{{ payload }}","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1380,"y":1240,"wires":[["8ff5f6df3bdc7f6e"]]},{"id":"5da0dd93d351e61a","type":"function","z":"786db0c1.8b55b","name":"Hellis81","func":"msg.payload = {\n        \"message\":\"Front Door Bell\",\n        \"title\":\"Front Door Bell\",\n        \"data\":\n            {\n            \"channel\":\"Front Doorbell\",\n            \"importance\":\"high\",\n            \"ttl\": 0,\n            }\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1120,"y":1220,"wires":[["4a0be93a8a8c2fa5","b1bf4f9d19e8063a"]]},{"id":"26eebe2b513dde4e","type":"inject","z":"786db0c1.8b55b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":920,"y":1220,"wires":[["5da0dd93d351e61a"]]},{"id":"1f8e0407654559d6","type":"function","z":"786db0c1.8b55b","name":"Front Door Bell","func":"msg.payload = {\n    data: {\n        \"message\":\"Front Door Bell\",\n        \"title\":\"Front Door Bell\",\n        \"data\":\n            {\n            \"channel\":\"Front Doorbell\",\n            \"priority\":\"high\",\n            \"ttl\": 0,\n            }\n    }\n}\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1140,"y":1160,"wires":[["b1bf4f9d19e8063a","4a0be93a8a8c2fa5"]]},{"id":"5092d5dd26aad1b6","type":"inject","z":"786db0c1.8b55b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":920,"y":1160,"wires":[["1f8e0407654559d6"]]},{"id":"2bcf671a.f2b3c8","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]

AhHa. I just found another sneaky hidden battery optimization. I’ll test that tomorrow. Thanks again.

dontkillmyapp.com does a good job of showing what needs to be changed :slight_smile:

1 Like

Looked at it now on the computer.

You where right, the json should be:

msg.payload = {
    "data":{
        "message":"Front Door Bell",
        "title":"Front Door Bell",
        "data":
            {
            "channel":"Front Doorbell",
            "importance":"high",
            "ttl": 0,
            }
    }
}
return msg;

However the data field in the call service should be empty.
image

1 Like

Awesome. This should be in every “Delayed” notification thread. And yes they had the sneaky setting and yes it worked.

Interestingly, that’s how I had it to start, but other threads lead me to try the message:payload option. I’m marking this as the solution.

Thanks to all.

Late to the party, but wanted to say thanks for this! Couldn’t find a good example for the “priority” high so my phone would light up when notification received.

Also managed to insert a variable from Waze Travel Time integration:

msg.payload = {
    domain: 'notify',
    data: {
        "message":"ON MY WAY HOME, IT TAKES  {{states('sensor.werk_naar_huis') }} MINUTES",
        "title":"OMW HOME",
        "data":
            {
            "priority":"high",
            "ttl": 0,
            }
    }
}
return msg;