Example of polling request to the server while in background using Local Notifications

The documentation says:

Examples of Notification Usage

Local notifications are ideally suited for applications with time-based behaviors, such as calendar and to-do list applications. Applications that run in the background for the limited period allowed by iOS might also find local notifications useful.
For example, applications that depend on servers for messages or data can poll their servers for incoming items while running in the background; if a message is ready to view or an update is ready to download, they can then present a local notification immediately to inform their users.

Source.

I need to get data from the server to devide if I will show or not a message to the user in the notifications. Perhaps I would just download data in background periodically.

The problem is, when I close, by pressing back key or via side menu, the notification icon appears in notification area but data is not immediately delivered and processed to my app, so I cannot to decide if I will show or not the notification until the user clicks on it.

When the app is just minimized, by pressing home key, the notification is delivered directly to the app.

Could someone show me a example on this?

I don’t have an example to show you, but to make sure you’ve got the concept right as per the example you gave, this is how it would work:

  1. You have some code that periodically checks your server for some data, according to that data you will either trigger a notification to that user or you won’t.

  2. In order to make sure this process continues to happen when the app is in the background, you will need to use a plugin to allow the code to continue to run whilst the app is in the background: https://github.com/katzer/cordova-plugin-background-mode

You make a reference to the data only being processed when the user clicks the notifications, you can have a handler to run some code when a notification is interacted with, but (at least in the context of the requirements above) you don’t need to use this callback for anything.

  1. LocalNotifications.on method is only calld after the user click the notification or open the app. I need to check the data in the server before click on notification or open the app if the app is in background. Where could I put this code?

  2. In have background mode installed and enabled.

This is my current code:

App.ts

  constructor(
      private platform:           Platform,
      private menu:               MenuController) {  


      platform.ready().then(() => {
          this.setStatusbar();                         
          this.setBackgroundMode();
          this.scheduleNotifications(); 
          this.setInitialRootPage();
      });
  }

  scheduleNotifications () {
    // Schedule delayed notification
    LocalNotifications.schedule({
       at: new Date(new Date().getTime() + 10000),
       sound: null
    });

    LocalNotifications.on('click', (result, state)=>{
        console.log('click', JSON.stringify(result, null, 4));
        console.log('state', JSON.stringify(state, null, 4));
    })

    LocalNotifications.on('trigger', (result, state)=>{
        console.log('trigger', JSON.stringify(result, null, 4));
        console.log('state', JSON.stringify(state, null, 4));
    })
     
  }