如何使用phonegap和parse发送推送通知

时间:2013-12-06 10:32:11

标签: php android jquery cordova parse-platform

我正在使用php,jquery和phonegap创建一个Android应用程序。我在谷歌搜索了很多东西,但我无法发送推送通知。我已经看到了这个Phonegap and Parse.com Push Notifications IOS但是我不清楚我可以得到deviceToken。

我也见过以下

https://parse.com/questions/php-rest-example-of-targeted-push

我了解如何发送通知。但没有devicetoken我怎么能发送推送通知。 anybosy可以告诉我如何获得设备令牌。

1 个答案:

答案 0 :(得分:12)

我跟着this tutorial直接工作得非常好。它还解释了如何获取设备令牌。

警告您输入它,但您也可以将手机连接到计算机并阅读logcat文件。 (您可以使用android SDK中的“监视器”工具)

更新示例

大部分步骤基本上都是devgirls tutorial I mentioned before

的直接副本

在Windows命令提示符下:

  1. phonegap create quickpush
  2. cd quickpush
  3. phonegap local build android
  4. phonegap local plugin add https://github.com/phonegap-build/PushPlugin

  5. 我跳过这个,我不把文件复制到www目录。我把它放在原处。

  6. <script type="text/javascript" src="PushNotification.js"></script>添加到index.html

  7. <gap:plugin name="com.phonegap.plugins.pushplugin" />添加到config.xml(这与网站不同,并解决了不支持的错误)

  8. 将推送代码复制到/js/index.js文件中的onDeviceReady函数中。显然,从谷歌

    添加自己的密钥
    alert('device ready');
    try {
        var pushNotification = window.plugins.pushNotification;
        pushNotification.register(app.successHandler, app.errorHandler,{"senderID":"--SENDER ID FROM GOOGLE--","ecb":"app.onNotificationGCM"});
    } catch (ex) {
        alert('error: ' + ex);
    }
    
  9. 复制/js/index.js文件中的回调处理函数

    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);
                }
            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;
        }
    }
    
  10. 构建应用:phonegap remote build android

相关问题