Help on FCMPlugin implementation (and SQLite)


#1

Hi all, I’m trying to insert in a SQLLite table all notifications received by FCMPlugin. Notifications are received correctly (as a console.log confirm that) but I’m unable to fire whatever other function follows, as example, I’m unable to fire a query for my slqlite db inside the onNotification method.
No error… seems simply the piece of code starting with this.sqlite.create(options) not fired…

Anyone can help me or take me some suggestion for debugging?

Here the code I’m using (initializeNotification is called in app.component.ts in this.platform.ready()). Is a provider containg code for FMCPlugin handling. NotificationsDbProvider is a simple provider for SQLLite and it’s ok. In the query I’m tring to insert the stringify object as a test, as you can see:

import { Injectable } from ‘@angular/core’;
import { Http } from ‘@angular/http’;
import ‘rxjs/add/operator/map’;
import { SQLiteObject } from ‘@ionic-native/sqlite’;
import { NotificationsDbProvider } from ‘…/…/providers/notifications-db/notifications-db’

declare var FCMPlugin, SQLitePlugin;

/*
Generated class for the NotificationsProvider provider.

See https://angular.io/docs/ts/latest/guide/dependency-injection.html
for more info on providers and Angular 2 DI.
*/
@Injectable()
export class NotificationsProvider {

constructor(public http: Http, public notificationDb: NotificationsDbProvider) {
console.log(‘Hello NotificationsProvider Provider’);
this.notificationDb.createNotificationDbIfNotExists();
}

initializeNotifications() {

const options: any = {
  name: 'data.db',
  location: 'default',
  createFromLocation: 1
};

//Push notifications
if (typeof(FCMPlugin) !== "undefined") {
  FCMPlugin.getToken(function (t) {
    console.log("Use this token for sending device specific messages\nToken: " + t);
    alert('DeviceId: ' + t);
  }, function (e) {
    console.log("Uh-Oh!\n" + e);
  });

  FCMPlugin.onNotification(
    function (d) {
    if (d.wasTapped) {
      // Background recieval (Even if app is closed),
      //   bring up the message in UI
      //this.notificationDb.addNotificationItem('notifica 1', 'notification', new Date());
      console.log('wasTapped');
      console.log( JSON.stringify(d) );
      this.sqlite.create(options)
        .then((db: SQLiteObject) => {
          var query = "INSERT INTO notifications (title, body, data) VALUES (?,?,?)";
          db.executeSql(query, [JSON.stringify(d), JSON.stringify(d), JSON.stringify(d)])
            .then(() => console.log('Populate Executed SQL'))
            .catch(e => console.log(e));
        })
        .catch(e => console.log(e));
    } else {
      // Foreground recieval, update UI or what have you...
      //this.notificationDb.addNotificationItem('notifica 2', 'notification', new Date());
      console.log('foregroundReceive');
      console.log( JSON.stringify(d) );

      this.sqlite.create(options)
        .then((db: SQLiteObject) => {
          var query = "INSERT INTO notifications (title, body, data) VALUES (?,?,?)";
          db.executeSql(query, [JSON.stringify(d), JSON.stringify(d), JSON.stringify(d)])
            .then(() => console.log('Populate Executed SQL'))
            .catch(e => console.log(e));
        })
        .catch(e => console.log(e));
    }
  }, function (msg) {
    // No problemo, registered callback
  }, function (err) {
    console.log("Arf, no good mate... " + err);
  });
} else console.log("Notifications disabled, only provided in Android/iOS environment");

}
}

Thank you
Emanuele


#2

Resolved… just wrappedthe code in an arrow function instead of the function (d) (which change the scope… damnnn!) .