繁体   English   中英

无法使用Phonegap Build在应用程序中注册PushPlugin?

[英]Unable to register the PushPlugin in app using Phonegap Build?

我正在尝试在我的Phonegap应用程序中实现一项功能,以便它可以接收推送通知,我指的是以下博客:

http://devgirl.org/2013/07/17/tutorial-implement-push-notifications-in-your-phonegap-application/

我有“Google Cloud Messaging Project ID”和“用于上述Project ID的Google Cloud Messaging API密钥”(服务器需要)我正在按照我添加的PhoneGap Build插件安装

<script type="text/javascript" src="PushNotification.js"></script>

到我的index.html文件。

并补充说

<gap:plugin name="com.phonegap.plugins.pushplugin" />

到我的config.xml文件。

我也在config.xml中指定了phonegap的版本为3.1.0

<preference name="phonegap-version" value="3.4.0" />

使用Google Cloud Messaging服务注册应用程序以接收推送通知。 我将下面的代码添加到index.html的head部分

<script>
        if (window.cordova) {
            if ((navigator.userAgent.match(/Android/i)) == "Android") {


                pushNotification = window.plugins.pushNotification;

                PushNotification.register(successHandler, errorHandler, { "senderID": "469325418209", "ecb": "onNotificationGCM" });

                function successHandler(result) {
                    alert('Callback Success! Result = ' + result);
                }

                function errorHandler(error) {
                    alert(error);
                }

                function onNotificationGCM(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;
                    }
                }

            }
        }

    </script>

当我使用phonegap构建应用程序并在我的手机上运行它时它会正常运行,但是当启动应用程序时,不会触发关于PushNotification.register成功或失败的警报。

我是否在步骤中错过了一步或犯了错误? 任何有关实现推送通知功能的建议或指导都表示赞赏。

确保您使用的是GCM的浏览器或Android密钥。

您可以从位于github的CLI添加Push插件

[https://github.com/phonegap-build/PushPlugin.git][1]

只需添加插件并按照github中描述的步骤操作即可

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM