gpt4 book ai didi

javascript - window.plugin.pushNotification 在 Cordova 3.6.4 上未定义

转载 作者:太空宇宙 更新时间:2023-11-03 12:52:08 25 4
gpt4 key购买 nike

我不熟悉使用 Cordova 构建多平台应用程序。我已经按照许多教程来创建推送通知,但无法做到。我遵循的过程是:

  1. 在 Google 控制台中创建了一个项目,将适用于 Android 的 Google 云通知消息传递设置为“开启”。

  2. 创建项目如下

指令:

Cordova create sampleApp com.sample sampleApp
cordova platform add android
cordova add plugin https://github.com/phonegap-build/PushPlugin.git
  1. 编辑 www/ 下的 index.html 添加了 pushNotification.js 文件。
  2. 更改了 www/js 下的 index.js 文件以包含注册。

代码:

var pushNotification = window.plugins.pushNotification;
console.log(pushNotification);
pushNotification.register(app.successHandler, app.errorHandler,{"senderID":"MY_SENDER_ID","ecb":"app.onNotificationGCM"});
},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;
        }
    }
  1. 添加所有需求后,我使用Cordova s​​erve。然后打开 index.html 页面,我看到 window.plugin.pushNotificationundefined。我做错了什么任何人都可以帮我解决这个问题。我已经坚持了很多天。

这是我的 index.html 文件

<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="msapplication-tap-highlight" content="no" />
<!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
<link rel="stylesheet" type="text/css" href="css/index.css" />
<title>Hello World</title>
</head>
<body>
<div class="app">
<h1>Apache Cordova</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
</div>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript" src="PushNotification.js"></script>
</body>

最佳答案

好的,我遇到了完全相同的问题 - 我花了好几个小时才找到解决方案。

首先,我不必将“PushNotification.js”包含到我的 index.html 文件中。 Cordova 构建管理器通过名为“corodva_plugins.js”的文件管理包含。

当我查看上述文件时,我发现了以下条目:

   {
"file": "plugins/com.phonegap.plugins.PushPlugin/www/PushNotification.js",
"id": "com.phonegap.plugins.PushPlugin.PushNotification",
"clobbers": [
"PushNotification"
]
},

所以我看到调用插件的方法实际上不是pushNotification,而是PushNotification(大写“P”)。

//var pushNotification = window.plugins.pushNotification;
var pushNotification = window.plugins.PushNotification;

编辑:查看 PushNotification.js 我发现该插件与window.plugins.pushNotification...但如果 cordova.js 包含在您的 Controller 之前。因此,如果您在使用 cordova.js(以及 PushNotification.js)之前包含它,它将起作用。

关于javascript - window.plugin.pushNotification 在 Cordova 3.6.4 上未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28398104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com