如何使用Phonegap和Parse发送推送通知

9
我正在使用php、jquery和phonegap创建一个Android应用程序。我在谷歌上搜索了很多东西,但是我找不到如何发送推送通知的方法。我看到了这个Phonegap and Parse.com Push Notifications IOS,但我不清楚如何获取设备令牌。
我还看到了下面的内容。

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

我知道如何发送通知,但如果没有设备标记,我如何发送推送通知呢? 有人可以告诉我如何获取设备标记吗?
1个回答

12

我按照这篇教程完成了推送通知的集成。教程还讲解了如何获取设备令牌。

在你键入设备令牌时会有一个警告,但你也可以将手机连接到电脑并读取logcat文件。(你可以使用android SDK中的"monitor"工具)

更新示例

大多数步骤基本上都是之前提到的devgirls教程的直接复制

在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函数中。显然,要使用你自己的Google密钥。

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);
}
  • 复制 /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;
        }
    }
    
  • 构建应用程序:phonegap远程构建android


  • 那似乎是半个错误。类似于该消息的情况是您未安装GIT,或未将GIT添加到您的路径变量中。也许可以参考http://blog.countableset.ch/2012/06/07/adding-git-to-windows-7-path/。 - Hugo Delsing
    最后我创建了一个示例应用程序并尝试在PhoneGap中上传,但是它显示错误“插件不受支持:com.adobe.plugins.pushplugin”。@Hugo Delsing,您能告诉我为什么会出现这种情况吗? - Struggling Developer
    我真的不知道为什么会显示那个。一些猜测:您正在使用较旧版本的phonegap。您在计算机上的另一个目录中安装了插件,但未包含在项目中。您需要本地编译它。我自己在phonegap方面还比较新。教程对我有用,但我还没有遇到过太多奇怪的错误。 - Hugo Delsing
    刚刚检查了一下,本地构建没有问题,但是远程构建出现了错误。在谷歌上搜索该错误,第一个链接就解决了问题:http://community.phonegap.com/nitobi/topics/error_plugin_unsupported_com_phonegap_plugins_pushplugin - Hugo Delsing
    嘿,@Hugo Delsing,抱歉打扰您了。您能给我提供这个应用程序的样本副本吗…… - Struggling Developer
    显示剩余6条评论

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接