0

I'm trying to get an ionic App to get the notification badges on launch app icon. As far as I have seen, it isn't possible if the ionic app is closed (not in background) so, anyone know if it's possible to create an android service that i always running on background and syncing my ionic app, making the update of the icon badge?

Thank you in advance

João Silva
  • 531
  • 4
  • 21
  • 40

1 Answers1

0

Since @Shiben asked, this is what I did to solve it.

  • Install cordova-plugin-firebase

  • Go to https://firebase.google.com and create your firebase project (see a guide for the configurations)

-In your app.component.ts do something like:

export class MyApp {
rootPage:any = HomePage;
firebase : any;

constructor(public platform: Platform, 
                   public statusBar: StatusBar, 
                   public splashScreen: SplashScreen, 
                   private _firebase: Firebase,
                   public alertCtrl: AlertController) {

  platform.ready().then(() => {
  (your things)
  this.firebase = _firebase;
  this.initFirebase();
  this.firebase.setBadgeNumber(0);
 });
}

And this was my initFirebase():

initFirebase(){

this.firebase.grantPermission();

this.firebase.onTokenRefresh()
  .subscribe((token: string) => localStorage.setItem("pushToken", token)) 

this.firebase.onNotificationOpen()
.subscribe((notification) => {
  let alert = this.alertCtrl.create({
    title: 'New Notification',
    subTitle: "Your notification",
    buttons:['OK']
  });
  alert.present();
});
}

-In yor index.html insert something like this (You got it from firebase)

<script src="https://www.gstatic.com/firebasejs/3.9.0/firebase.js"></script>
<script>
  // Initialize Firebase
  var config = {
    apiKey: "your key",
    authDomain: "your domain",
    databaseURL: "your url",
    projectId: "your projid",
    storageBucket: "your storagebucket",
    messagingSenderId: "your messageid"
  };
firebase.initializeApp(config);
</script>

I did this a long time ago and something might be change. This can be deprecated or not be the best practices, however, I hope it can get your in the right direction.

João Silva
  • 531
  • 4
  • 21
  • 40