Sencha Phonegap Android 推送通知

Posted

技术标签:

【中文标题】Sencha Phonegap Android 推送通知【英文标题】:Sencha Phonegap Android Push Notification 【发布时间】:2013-11-08 05:41:59 【问题描述】:

我正在开发一个需要推送通知功能的煎茶触摸应用程序。根据 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 函数的方式?遇到问题,请帮我解决这个问题,谢谢..

【问题讨论】:

【参考方案1】:

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

【讨论】:

【参考方案2】:

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

不要在你的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.successHandlerthis.successHandler"this" 指的是你的应用程序,以及 app.onNotificationGCMMyAppName.app.onNotificationGCM

【讨论】:

【参考方案3】:

在你应用的 lanch 方法中试试这个 sn-p

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));
            

        );

【讨论】:

以上是关于Sencha Phonegap Android 推送通知的主要内容,如果未能解决你的问题,请参考以下文章

PhoneGap + Android 模拟器 + Sencha Touch

Sencha touch + phonegap。在 android 4.0 上渲染数据视图

Sencha Touch 2.3.1 + Phonegap 3.4 jsonp 在 Android 中不工作,但在浏览器上工作

PhoneGap 0.9.6 (Blackberry) + Sencha Touch 1.1.0:deviceready 不触发

Sencha Touch 应用部署到 iPhone 和 Android

sencha touch & PhoneGap中的基本程序