Push Notification using Backendless Event Handlers JS not sent

Backendless.ServerCode.Persistence.afterCreate('Information', function(req, res) {
var pubOps = new PublishOptions(
{
    headers: {
        "android-ticker-text": "KamiMart",
        "android-content-title": res.result.informationTitle,
        "android-content-text": res.result.informationDescription,
        "android-notification-type": "INFORMATION",
        "android-banner-url": res.result.informationBannerImageUrl
    }
})
Backendless.Messaging.publish("Information", res.result.informationTitle, pubOps).then(function (response) {
  
}).catch(function (error) {
  
});

}, true);

I try to send push notification to Android devices that registered within Information channel.
But did not receive any.
And there is no logs.

Please help.

Hello @KamiMart_KamiMart

I believe it happens because you missed return <Promise> or async/await

to fix your problem you have two ways:

  1. add a return before Backendless.Messaging.publish(...

  2. add async/await

Backendless.ServerCode.Persistence.afterCreate('Information', async function(req, res) {
...

await Backendless.Messaging.publish(...

for more information check this https://backendless.com/docs/bl-js/bl_sync_vs_async_code.html

Regards, Vlad

it’s not working. but if I try to send it from android client/rest api it works.
the log says nothing about errors after all.

try to add console.log before and after the publishing

Backendless.ServerCode.Persistence.afterCreate('Information', async function(req, res) {

...

  console.log('before publish')

  await Backendless.Messaging.publish("Information", res.result.informationTitle, pubOps)
  
  console.log('after publish')
});

and then check your logs, can you see these two messages there?

Get nothing. Where are the actual logs?
I searched here -> files -> root -> logging and opened a log file that matches current date.

Can I perform this with rest api?
As a workaround, I dont have too much time to play with trial and error for now.

How to call/post request to an API in Backendless event handler for javascript?

do you create a new Information object using Data Browser or API (REST Console)?

I created it with the console.

But that’s not what I am confused about
I have properly followed the sample code yet I cant get the event handler works.

Since using push notification rest works, can I call the rest api from the event handler?
How can i do that using axios/http/request somehow?
I code the js within the console.

Thanks.

I have properly followed the sample code yet I cant get the event handler works.

that’s the root problem, if you don’t see any logs it means that your Business Logic is not executing.

if you create new objects using Data Browser your BL won’t be executed, but if you use REST Console it will

So, let me ask you one more time, do you use Data Browser (img1) or REST Console (img2)?

img1:

img2:

@KamiMart_KamiMart any update?

Yes, thanks.
What you’re saying was right, the push notification works when we call it from REST API/client, not the Data Browser/console.

Case solved.