job.answiz.com
  • 1
Votes
name
name Punditsdkoslkdosdkoskdo

How to Load plugin reference in app.js : NativeScript

I'm developing a NativeScript app which involves push notifications.Whenever push notification came I need to store notification contents into database.

For that I have written some code in "onMessageReceived" function.This code is in the page in which GCM registration code is there.

If app is running then every thing working fine. The problem is if app is closed then "onMessageReceived" function is not even executing(I checked with console logs).

So for that I'm trying to place "onMessageReceived" function in app.js so that even app is closed it will execute.For that I'm trying to import the "nativescript-push-notifications" in app.js, but getting error saying "application is null,it's not passed correctly".Below is my app.js code.

app.js

var application = require("application");
var gcm=require("nativescript-push-notifications");
if(gcm.onMessageReceived) {
    gcm.onMessageReceived(function callback(data) { 
    console.log("message received:::: ", "" + JSON.stringify(data));
    storeInDatabase(data);// some function to store notification content into db. 

});
 }
application.mainModule="main-page";
application.start({ moduleName: "main-page" });

Can we import "nativescript-push-notifications" reference in app.js??

Any suggestions will be helpful.Thanks.

Application is null because your app has not started yet try adding the plugin in the application launch event

var application = require("application");
application.start({ moduleName: "main-page" });
application.on(application.launchEvent, function (args) {
    if (args.android) {
        var gcm = require("nativescript-push-notifications");
        gcm.register({ senderID: 'conversate-1148' }, function (data) {
            self.set("message", "" + JSON.stringify(data));
        }, function () { });
        if (gcm.onMessageReceived) {
            gcm.onMessageReceived(function callback(data) {
                console.log("message received:::: ", "" + JSON.stringify(data));
                 storeInDatabase(data);// some function to store notification content into db. 

            });
        }

    } else if (args.ios !== undefined) {
        //Do ios stuff here
    }
});
  • 0
Reply Report