Background notifications using Ionic like a mortal

Viewed 158

I have been trying for several months to implement an app that receives notifications whose content I want to be stored in the database of my application. I am using the Firebase Messagging plugin and I am receiving the notifications correctly, both in Foreground and in Background.

However, in the background I am unable to execute my callback without the need to press the notification explicitly by the user. Likewise, there is no way to increase the badge of the application when it is not open.

Now, I do not understand how applications like WhatsApp, Telegram and any other application that is not made by mere mortals work. How is it possible that they can get the data in backgroud, manage the badges, synchronize messages, etc? Being that, supposedly the services like Firebase are limited in background. Even with plugins like Background Mode my application is suspended by Android when the user closes it.

The code that I am currently using to handle notifications is the following:

// In foreground (WORKS)
this.firebaseMessaging.onMessage().subscribe((notificacion) => { 
    // Insert in DB
    ...
});

// In background (DOESN'T WORK)
this.firebaseMessaging.onBackgroundMessage().subscribe((notificacion) => {
    // Insert in DB
    ...
});

What alternative is left? The only thing that occurs to me is to use notifications in Foreground and background only as a warning. So every time I open the application I'll have to call a message synchronization callback with the server (with badge management included).

If someone has some better way, I would greatly appreciate it if you throw a little light on the subject.

From already thank you very much

1 Answers

Ok, after more than a year I think it's time to close this question. I solved the problem this way:

  1. Suppose I need to get a list of messages. This action is made by a function called getMessages()
  2. When a new message is created in the backend, I send a push notification through Firebase service.
  3. If the push notification is received in foreground I refresh call the method directly:
this.firebaseMessaging.onMessage().subscribe((notificacion) => { 
    getMessages();
});
  1. If the push notification is received in background and the user taps on it there isn't a problem as it's supported by the plugin:
this.firebaseMessaging.onBackgroundMessage().subscribe((notificacion) => {
    getMessages();
});
  1. If the push notification is received in background but the user DOES NOT tap on it and opens the app in another way than the notification, we need to excecute the corresponing function when the app is opened in app.component.ts file:
this.platform.ready().then(() => {
  getMessages();
  // Extra stuff like this.statusBar.styleDefault(); or this.splashScreen.hide();
});

That way all the cases are considered! You should keep in mind that apps like Whatsapp or Telegram are developed in official technologies like Java or Kotlin which not only have official native plugins, also its code are excecuted natively and not in a limited browser as Cordova or Capacitor frameworks do

Related