2

我正在开发一个需要推送通知功能的煎茶触摸应用程序。根据 sencha 文档,我知道他们不支持 Android 推送通知。所以我试图将我的项目与 Phonegap 3.0 集成。对于推送通知,我正在使用此插件 https://github.com/hollyschinsky/PushNotificationSample30/

该演示在获取注册 ID 时运行良好,我可以向该注册 ID 发送推送通知。但问题是当我尝试将我的 sencha 应用程序集成到这个演示推送插件时,我没有得到 reg id

我的 index.html 看起来像这样

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>crmapp</title>
    <script id="microloader" type="text/javascript" src="touch/microloader/development.js"></script>
    <script type="text/javascript" src="phonegap.js"></script>
    <script type="text/javascript" src="PushNotification.js"></script>
   <script type="text/javascript" src="js/index.js"></script>
</head>
<body>
<div id="appLoadingIndicator">
    <div></div>
    <div></div>
    <div></div>
</div>
</body>
</html>

并从我的 app.js 文件中调用 js/index.js 文件中的推送通知功能,它看起来像这样

Ext.application({
    name: 'WinReo',
    requires: [
        'Ext.MessageBox',

    ],


    views: [
        'Login',
       // 'MainMenu',
        'CrmFooter',
        'CrmHead',



    ],
    controllers:[
        'Login',
        'Main',
        'Task',

    ],
    models: [
        'Event',
        "Task"
    ],
    stores: [
        'Events',
        'EventsDueListStore'
        //'Contactsstore'
    ],


    icon: {
        '57': 'resources/icons/Icon.png',
        '72': 'resources/icons/Icon~ipad.png',
        '114': 'resources/icons/Icon@2x.png',
        '144': 'resources/icons/Icon~ipad@2x.png'
    },

    isIconPrecomposed: true,

    startupImage: {
        '320x460': 'resources/startup/320x460.jpg',
        '640x920': 'resources/startup/640x920.png',
        '768x1004': 'resources/startup/768x1004.png',
        '748x1024': 'resources/startup/748x1024.png',
        '1536x2008': 'resources/startup/1536x2008.png',
        '1496x2048': 'resources/startup/1496x2048.png'
    },

    launch: function() {

        Ext.fly('appLoadingIndicator').destroy();
        app.initialize(); // **Please see this line**

    },
    onUpdated: function() {
        Ext.Msg.confirm(
            "Application Update",
            "This application has just successfully been updated to the latest version. Reload now?",
            function(buttonId) {
                if (buttonId === 'yes') {
                    window.location.reload();
                }
            }
        );
    }
});

这是 index.js 文件

var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicity call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
        var pushNotification = window.plugins.pushNotification;
        pushNotification.register(app.successHandler, app.errorHandler,{"senderID":"675077458226","ecb":"app.onNotificationGCM"});

    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    },
    // result contains any message sent from the plugin call
    successHandler: function(result) {
        alert('Callback Success! Result = '+result)
    },
    errorHandler:function(error) {
        alert(error);
    },
    onNotificationGCM: function(e) {
        switch( e.event )
        {
            case 'registered':
                if ( e.regid.length > 0 )
                {
                    console.log("Regid " + e.regid);
                    alert('registration id = '+e.regid);
                    document.write(e.regid);
                }
                break;

            case 'message':
                // this is the actual push notification. its format depends on the data model from the push server
                alert('message = '+e.message+' msgcnt = '+e.msgcnt);
                break;

            case 'error':
                alert('GCM error = '+e.msg);
                break;

            default:
                alert('An unknown GCM event has occurred');
                break;
        }
    }

};

我不确定这是调用此函数的方式,根本无法访问该函数,没有获取注册 ID.. 请指导我正确的方向.. 这是从 sencha touch 调用 phonegap 函数的方式吗?遇到问题,请帮我解决这个问题,谢谢..

4

3 回答 3

0

问题是您需要为 ecb 传递 {application name}.app.onNotificationGCM。查看您的 app.js 文件,并说您的名字是“MyApp”。然后,它将是 MyApp.app.onNotificationGCM。希望这会有所帮助!

于 2014-02-04T05:02:21.633 回答
0

嗨,你有没有解决这个问题,我找到了解决方案

不要在你的 Cordova 的index.js中添加推送注册代码,而是直接将它添加到你的 Sencha 的app.js启动函数中

launch: function() {
    var pushNotification = window.plugins.pushNotification;
    pushNotification.register(this.successHandler, this.errorHandler,{"senderID":"675077458226","ecb":"MyAppName.app.onNotificationGCM"});
 },
 //then followed by your othe functions
 receivedEvent: function(id)
  {
    //code here
  },
 errorHandler:function(error) {
   //code here
   },
 onNotificationGCM: function(e) {
  //code here
  }

另请注意,我更改了函数的调用方式,将app.successHandler 更改this.successHandler“this”指的是您的应用程序,还有app.onNotificationGCMMyAppName.app.onNotificationGCM

于 2014-11-19T08:38:30.047 回答
0

在您的应用程序的 lanch 方法中尝试此代码段

var pushNotification;
        document.addEventListener("deviceready", function(){
            pushNotification = window.plugins.pushNotification;
            if ( device.platform == 'android' || device.platform == 'Android' || device.platform == "amazon-fireos" ){
                pushNotification.register(
                 function(result){
                    alert('result = ' + result);
                },
                function(error){
                    alert('error = ' + error);
                },
                {
                    "senderID":"YOUR_google_project_id",
                    "ecb":"onNotification"
                });
            }
            onNotification = function(e) {
                alert("aaa"+JSON.stringify(e));
            }

        });

于 2014-12-19T05:38:48.993 回答