Sencha Phonegap Android推送通知

时间:2013-11-08 05:41:59

标签: cordova sencha-touch push-notification

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

该演示工作正常,我获得注册ID,我可以发送推送通知到该注册ID。但问题是,当我尝试将我的sencha应用程序集成到此演示推送插件时,我没有获得注册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函数的方法?我遇到了问题,请帮我解决这个问题,谢谢..

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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 &#34;这&#34; 指您的应用, app.onNotificationGCM MyAppName.app.onNotificationGCM

答案 2 :(得分:0)

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

&#13;
&#13;
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));
            }

        });
&#13;
&#13;
&#13;