Loading the 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
    }
});
Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 前端开发 » Loading the plugin reference in app.js: NativeScript

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录