0 Replies - 606 Views - Last Post: 31 July 2018 - 09:09 AM

#1 vik100   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 12-August 17

Ionic2 app badge with push notification in back-and-foreground

Posted 31 July 2018 - 09:09 AM

I'm trying to get badges over the app icon with push notification receiving in `Ionic2` `Type Script` `Android` application:

in app.components.ts I use
this.increaseBadges(this.badgeNumber);
in
initPushNotification()
from constructor
platform.ready().then(() => { this.initPushNotification();  })
with previous
let permission = await this.badge.requestPermission();
function to get permission value true:



    ...
    
     const options: PushOptions = {
      android: {
        senderID: 'my sender id'
      },
      ios: {
        alert: 'true',
        badge: true,
        sound: 'false'
      },
      windows: {}
      };
      const pushObject: PushObject = this.push.init(options);
        pushObject.on('notification').subscribe((notification: any) => {
          if (notification.additionalData.foreground || notification.additionalData.background) {
            console.log('Received a notification', notification);
            this.increaseBadges(this.badgeNumber);
    
    ...



which is
badgeNumber: number;
variable, it works for foreground, but I'm not sure if it is proper way to use this variable in background to increase or decrease badge:



  async increaseBadges(badgeNumber: number) {
        try {
          let badge = await this.badge.increase(badgeNumber);
          console.log("Badge notification counter: ", badge);
          this.badgeNumber = badge;
          var badgeAmount = badge;
          this.setBadges(badgeAmount);
        } catch (e) {
          console.log(e);
        }
      }
    
      async setBadges(badgeAmount: number) {
        try {
          let badges = await this.badge.set(badgeAmount);
          console.log("Set badges: ", badges);
          this.out = badges;
          this.getBadges()
        } catch (e) {
          console.log(e);
        }
      }
    
      async getBadges() {
        try {
          let badgeAmount = await this.badge.get();
          console.log("Get badges amount: ", badgeAmount);
          this.out = badgeAmount;
        } catch (e) {
          console.log(e);
        }
      }




badgeAmount value increases with receiving of push notification in foreground from device debug console, but badge does not appears on icon. For example, increasing badge manually with button, shows counter circle on icon. Seems like I'm doing something completely wrong way. I'm not sure, how to get badges counter for application icon, with receiving push notification in background and foreground.

I've tried to use background plugin
import { BackgroundMode } from '@ionic-native/background-mode';
to increase in background, but this way application freezes on loading. I'm not sure if I'm using background correctly, and if this solves badge problem in result:



    background(){
          this.backgroundMode.on('activate').subscribe(() => {
            console.log('activated');
             this.increaseBadges(this.badgeNumber);
          });
          this.backgroundMode.enable();
    }


Is This A Good Question/Topic? 0
  • +

Page 1 of 1