如何使用PhoneGap的发送推送通知和解析如何使用、通知、PhoneGap

2023-09-05 10:41:10 作者:曾习惯的习惯つ

我使用PHP,jQuery和PhoneGap的创建一个Android应用程序。我寻觅这么多的事情在谷歌,但我不能找到发送推送通知。我看到这个Phonegap和Parse.com推送通知IOS 但我并不清楚浩我可以得到deviceToken。

I am creating an android app using php,jquery and phonegap. I have searched so many things in google but i cant find to send push notification. I have seen this Phonegap and Parse.com Push Notifications IOS But i am not clear ho can i get deviceToken.

我也看到了以下

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

我知道如何发送通知。但是,如果没有devicetoken我怎么能发送推送通知。 可以anybosy告诉我,我怎样才能得到设备令牌。

I understood how to send notification. But without devicetoken how can i send push notification. Can anybosy tell me how can i get the device token.

推荐答案

我跟着其中的工作非常出色,直接本教程。这也解释了如何获取设备令牌。

I followed this tutorial which worked very well directly. It also explains how to get the device token.

这是提醒你给它输入了,但你还可以连接你的手机到您的计算机并阅读logcat的文件。 (您可以使用在Android SDK中的监视工具)

It is alerted for you to type it over, but you can also hook your phone up to your computer and read the logcat files. (You can use the "monitor" tool in the android SDK)

更新实例

大多数步骤基本上都是 devgirls教程中,我提到过

Most steps are basically a direct copy of devgirls tutorial I mentioned before

在Windows命令提示符:

In windows command prompt:

PhoneGap的创建quickpush CD quickpush 的PhoneGap本地构建Android

的PhoneGap本地插件添加https://github.com/phonegap-build/PushPlugin phonegap create quickpush cd quickpush phonegap local build android

phonegap local plugin add https://github.com/phonegap-build/PushPlugin

我跳过了这一点,我不将文件复制到www目录。我只是把它在哪里。

I skipped this, I dont copy the file to the www dir. I just leave it where it is.

添加<脚本类型=文/ JavaScript的SRC =PushNotification.js>< / SCRIPT> 来的index.html

add <script type="text/javascript" src="PushNotification.js"></script> to index.html

添加&LT;差距:插件名称=com.phonegap.plugins.pushplugin/&GT; 来config.xml中(这是从网站不同,解决了不支持的错误)

add <gap:plugin name="com.phonegap.plugins.pushplugin" /> to config.xml (this is different from site and solves not supported error)

复制推code在/js/index.js文件onDeviceReady功能。显然,从谷歌添加自己的钥匙

Copy the push code in the onDeviceReady function in /js/index.js file. Obviously add your own key from 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文件中的回调处理函数

Copy the callback handler function in /js/index.js file

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