我正在嘗試幾天來獲取運行推送通知的基本應用程序。使用phonegap構建接收推送通知
我正在使用PhoneGap Build(桌面應用程序)並正在Android設備上進行測試。 我試過各種教程,但他們似乎從來沒有工作。運行下面的代碼只是沒有任何反應。
這是我當前的代碼:
config.xml中我加了以下幾行:
<preference name="android-build-tool" value="gradle" />
<plugin name="phonegap-plugin-push" source="npm">
<param name="SENDER_ID" value="540732047871" />
</plugin>
的index.html
<body>
<div class="app">
<h1>PhoneGap</h1>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
app.initialize();
</script>
</body>
index.js
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
var push = PushNotification.init({
"android": {
"senderID": "540732047871"
},
"ios": {"alert": "true", "badge": "true", "sound": "true"},
"windows": {}
});
push.on('registration', function(data) {
console.log("registration event");
document.getElementById("regId").innerHTML = data.registrationId;
console.log(JSON.stringify(data));
});
push.on('notification', function(data) {
console.log("notification event");
console.log(JSON.stringify(data));
var cards = document.getElementById("cards");
var card = '<div class="row">' +
'<div class="col s12 m6">' +
' <div class="card darken-1">' +
' <div class="card-content black-text">' +
' <span class="card-title black-text">' + data.title + '</span>' +
' <p>' + data.message + '</p>' +
' </div>' +
' </div>' +
' </div>' +
'</div>';
cards.innerHTML += card;
push.finish(function() {
console.log('finish successfully called');
});
});
push.on('error', function(e) {
console.log("push error");
});
}};
我做錯了什麼?我正在使用基於「phonegap-plugin-push」的「github」頁面的代碼。任何想法,我似乎錯過了創建該項目?
哦好吧,沒有讀到任何地方。感謝那。然後我會嘗試使用PhoneGap Build來獲得它。也許這可以解決問題。我會評論我發現的內容。 –
@Sven,它是寫在文檔中,但不是很好**。 – JesseMonroy650