在app.js中加载插件引用:NativeScript

Kar*_*eek 1 android push-notification google-cloud-messaging nativescript

我正在开发一个涉及推送通知的NativeScript应用程序.无论何时推送通知我都需要将通知内容存储到数据库中.

为此,我在"onMessageReceived"函数中编写了一些代码.此代码位于GCM注册码所在的页面中.

如果app正在运行,那么每件事情都运转正常 问题是如果应用程序关闭,则"onMessageReceived"函数甚至不执行(我使用控制台日志检查).

所以为此我试图在app.js中放置"onMessageReceived"函数,这样即使应用程序关闭它也会执行.为此我试图在app.js中导入"nativescript-push-notifications",但是错误说"应用程序为空,它没有正确传递" .Below是我的app.js代码.

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" });
Run Code Online (Sandbox Code Playgroud)

我们可以在app.js中导入"nativescript-push-notifications"引用吗?

任何建议都会有所帮助.谢谢.

Ose*_*une 5

应用程序为空,因为您的应用尚未启动尝试在应用程序启动事件中添加插件

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
    }
});
Run Code Online (Sandbox Code Playgroud)