duanjiushu5063 2016-03-18 07:10
浏览 43

使用cordova在我的网络服务器上存储GCM ID无法正常工作

I am trying to register GCM ids of various devices on my webserver so that I can send push notifications. I have used the following code to get the GCM id of a device.

<script type="text/javascript">
                document.addEventListener("deviceready",onDeviceReady,false);
                function onDeviceReady(){
                console.log("Device Ready")
                var push = PushNotification.init({ "android": {"senderID":"989946143452"},
                "ios": {"alert": "true", "badge": "true", "sound": "true"}, "windows": {} } );

                push.on('registration', function(data) {
                console.log(data.registrationId);
                $("#gcm_id").html(data.registrationId);
                });

                push.on('notification', function(data) {
                console.log(data.message);
                alert(data.title+" Message: " +data.message);
                // data.title,
                // data.count,
                // data.sound,
                // data.image,
                // data.additionalData
                });

                push.on('error', function(e) {
                console.log(e.message);
                });
                }
</script>

Then I am using the following this form field to parse the gcm id from the javascript to the html form field,

<input type="hidden" name="gcm_id" id="gcm_id" required/>

The form field seems not to be getting the gcm_id value. Any workarounds on this?

Of couser I am using ajax to send the data to the php script that will store the gcm_id on my MySQL table.

  • 写回答

1条回答 默认 最新

  • douzhuang1900 2016-03-18 09:55
    关注

    I am going to answer the question as I just realize that the error was on my ajax submission script. Here is the entire script that is working perfectly. Note the changes I made on $("#gcm_id").html(data.registrationId); to $("#gcm_id").text(data.registrationId);

    <script type="text/javascript">
            document.addEventListener("deviceready",onDeviceReady,false);
            function onDeviceReady(){
            console.log("Device Ready")
            var push = PushNotification.init({ "android": {"senderID":"XXXXXXXXXXXXXX"},
            "ios": {"alert": "true", "badge": "true", "sound": "true"}, "windows": {} } );
    
            push.on('registration', function(data) {
            console.log(data.registrationId);
            //$("#gcm_id").html(data.registrationId);
            $("#gcm_id").text(data.registrationId);
                        $.ajax({
                        url: "http://YOURDOMAIN.COM/gcmreg.php",
                        type: "POST",
                        dataType: 'text',
                        data: {registrationId: data.registrationId},
                        success: function (data) {
                            alert(data);
                        }
                        });
            });
    
            push.on('notification', function(data) {
            console.log(data.message);
            alert(data.title+" Message: " +data.message);
            // data.title,
            // data.count,
            // data.sound,
            // data.image,
            // data.additionalData
            });
    
            push.on('error', function(e) {
            console.log(e.message);
            });
            }
            </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM