【问题标题】:Handling onMessageReceived function for push notifications: NativeScript处理推送通知的 onMessageReceived 函数:NativeScript
【发布时间】:2016-05-04 12:53:59
【问题描述】:

我正在使用 NativeScript 开发一个包含推送通知功能的 Android 应用程序。 每当推送消息到来时,我都必须保存消息详细信息。为了实现此功能,我使用了 onMessageReceived 函数。

App.js file:::::

var application=require("application");
var applicationSettings = require("application-settings");
application.mainModule="Registration/Registration";
// application.mainModule="CreateTask/CreateTask";
application.css="styles/Follow.css";
application.start();
application.on(application.launchEvent, function (args) {
if (args.android) {
    var gcm = require("nativescript-push-notifications");
    gcm.register({ senderID: '616557539872' }, function (data) {
        console.log("message"+ JSON.stringify(data));
    }, function () { });
        gcm.onMessageReceived(function callback(data) {
            console.log("message received:::: ", "" + JSON.stringify(data));
         //  some code for storing notification contents to database
            var notifications=require("~/notifications");
             notifications.createViewModel(data);

        });

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

notifications.js

var LocalNotifications = require("nativescript-local-notifications");
var dialogs = require("ui/dialogs");
var frameModule=require("ui/frame");

var notifications=new Object();
function doAddOnMessageReceivedCallback() {
console.log("doAddOnMessageReceivedCallback::");
LocalNotifications.addOnMessageReceivedCallback(
    function(notificationData) {

        frameModule.topmost().navigate("Registration/Registration");
        dialogs.alert({
            title: "Notification received",
            message: "ID: " + notificationData.id +
            "\nTitle: " + notificationData.title +
            "\nBody: " + notificationData.body,
            okButtonText: "Excellent!"
        });
    }
);
}

function createViewModel(data) {
    console.log("createViewModel::"+data); 
    doAddOnMessageReceivedCallback();
    LocalNotifications.schedule([{
        id: 0,
        title: "AddTaskNotification",
        body: data,
        ticker: this.ticker,
        at: new Date(new Date().getTime())
    }]).then(() => {
        console.log("Notification scheduled");
    }, (error) => {
        console.log("ERROR", error);
    });

}

exports.createViewModel = createViewModel;

app->App_Resources->Android->AndroidManifest.xml 中的权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<uses-permission android:name="android.permission.INTERNET"/>

<uses-permission android:name="android.permission.GET_ACCOUNTS" />

<uses-permission android:name="android.permission.WAKE_LOCK" />

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>

清单文件内容位于:node_modules->nativescript-push-notifications->platforms->android->AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<application>
    <activity android:name="com.telerik.pushplugin.PushHandlerActivity"/>
    <receiver
            android:name="com.google.android.gms.gcm.GcmReceiver"
            android:exported="true"
            android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="com.pushApp.gcm" />
        </intent-filter>
    </receiver>
    <service
            android:name="com.telerik.pushplugin.PushPlugin"
            android:exported="false" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>
</application>

在 onMessageReceived 函数中,我正在执行一些操作,然后触发本地通知。我这样做是因为如果我提供 onMessageReceived 函数,则通知不会显示在通知栏中。所以我在保存到后触发本地通知分贝。

如果应用程序正在运行,则 onMessageReceived 函数的内容正在执行。 如果应用程序关闭,那么 onMessageReceived 的内容不会执行(甚至 console.log 也不会执行)。

app关闭时如何处理onMessageReceived函数中的数据?

任何建议都会很有帮助。谢谢。

【问题讨论】:

  • 关于这个问题的一篇很好的操作文章可以在这里找到:bradmartin.net/2015/12/28/…
  • 嗨,谢谢您的回复。我按照相同的教程来实现推送通知的概念。但问题是我需要在 onMessageReceived 函数中执行一些操作。但是如果应用程序关闭,该函数不会执行。可以你有什么建议吗??
  • @Karteek 你有没有找到解决这个问题的方法?我也遇到了同样的问题。

标签: android push-notification google-cloud-messaging nativescript


【解决方案1】:

请查看此插件:https://github.com/EddyVerbruggen/nativescript-local-notifications

这可能是你需要的

编辑:

我注意到你正在检查 onMessageReceived;请改用此代码:

var gcm=require("nativescript-push-notifications");
gcm.onMessageReceived(function callback(data) { 
   console.log("message received:::: ", "" + JSON.stringify(data));
   // code related to saving the details... 
});

你也正确设置了android权限??

【讨论】:

  • 感谢您的回复。以前我尝试过 localnotifications,但这也不适用于我的特殊情况(如果应用程序已关闭)。正如您所建议的,我删除了 onMessageReceived 检查然后尝试,但现在如果应用程序已关闭。我使用更新的代码编辑了我的帖子以供参考(删除 onMessageReceived 检查,实施本地通知)。如果我做任何错误,请纠正我。
  • 是的,我做了同样的事情,1 秒后预定通知。在接收推送通知时。但是当应用程序关闭时,那时本地通知不会安排,因此不会显示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多