0

我正在嘗試幾天來獲取運行推送通知的基本應用程序。使用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」頁面的代碼。任何想法,我似乎錯過了創建該項目?

回答

0

@Sven,

套用消息
我寫博客這個明天,所以我不必一次又一次地重複這個答案。

您不能使用Phonegap桌面應用程序Phonegap Build

PhoneGap的桌面應用程式使用PhoneGap的CLI,所以如果你想使用PhoneGap的桌面應用程序你需要保持與PhoneGap的CLI

如果你想使用PhoneGap的建設,然後忘記你做了什麼,並開始了OR使Minor modifications移動PhoneGap的桌面應用程式的PhoneGap構建。要清楚,差異很小,但足以讓你絆倒。

此外,您將無法使用PhoneGap的開發應用程序爲是指具有PhoneGap的桌面應用程式工作。您不能使用CLI或與Developer App一起構建。

FWIW:文檔是問題。我一直在試圖讓Phonegap來解決這個問題。這裏是report on the issue.

讓我知道你想要做什麼。

傑西

+0

哦好吧,沒有讀到任何地方。感謝那。然後我會嘗試使用PhoneGap Build來獲得它。也許這可以解決問題。我會評論我發現的內容。 –

+0

@Sven,它是寫在文檔中,但不是很好**。 – JesseMonroy650