2016-07-28 145 views
2

我試圖弄明白如何使用聚合物<platinum-push-messaging>元素,但沒有運氣。在該元素的文檔,它說,你需要註冊你的「gcm_sender_id」,然後定義元素本身,就像這樣:聚合物鉑推送消息元素

<platinum-push-messaging 
    title="Application updated" 
    message="The application was updated in the background" 
    icon-url="icon.png" 
    click-url="notification.html"> 
</platinum-push-messaging> 

爲了測試它有一個叫testPush(message)方法,但它總是返回我以下錯誤:

platinum-push-messaging.html:437 Uncaught (in promise) TypeError: Cannot read property 'active' of undefined(…)

我不知道如果我忘了什麼東西,和Cat Push Notifications project不會在所有幫助,我無法得到它的工作...

任何人都可以點我向正確的方向?

回答

4

platinum-push-messaging元素可用於使用gcm_sender_id(GCM代表Google Cloud Messaging)訂閱推送通知。

如果您在Firebase上託管項目,則可以在項目>項目設置>雲消息傳遞的Console中找到您的gcm_sender_id

否則,您可以創建在Google Developers Console項目,發現在設置(項目編號)的ID。

注意:由於Docs中的要求部分,推送消息傳送當前僅在Google Chrome中可用。

但讓我們做一步步演示(Works爲Mac和Unix - 那是什麼的官方演示支持):

  1. 裏面
  2. 運行bower init(用於創建一個新的文件夾,然後打開一個終端在bower.json
  3. 運行bower install PolymerElements/paper-elements(由官方演示中使用的元素,我們將使用)
  4. 運行bower install PolymerElements/platinum-push-messaging(你會得到bower_components文件夾所需的一切演示)
  5. 獲取the offical demoplatinum-push-messaging和(複製這些文件,你剛剛創建的文件夾中)
  6. index.html變化的相應元素的.html文件中bower_components和webcomponents包括對bower_components/webcomponentsjs/webcomponents-lite.min.js。進口應該看起來有點像這樣:

    <script src="bower_components/webcomponentsjs/webcomponents-lite.min.js"></script> 
    <link rel="import" href="bower_components/paper-styles/paper-styles.html"> 
    <link rel="import" href="bower_components/paper-styles/classes/global.html"> 
    <link rel="import" href="bower_components/paper-item/paper-item.html"> 
    <link rel="import" href="bower_components/paper-material/paper-material.html"> 
    <link rel="import" href="bower_components/paper-toggle-button/paper-toggle-button.html"> 
    <link rel="import" href="bower_components/platinum-push-messaging/platinum-push-messaging.html"> 
    <link rel="manifest" href="manifest.json"> 
    
  7. 轉到Google Developers Console並創建一個項目。

  8. 選擇「管理所有項目」,「設置」,選擇您的項目並複製「項目編號」
  9. 打開manifest.json並設置gcm_sender_id到複製的號碼。
  10. 返回到Google Developers Console並打開「API管理器」(左側欄),「資格證書」
  11. 創建一個新的憑據服務器密鑰並複製API (你會需要它在後面的步驟所以它保存在一個.txt或類似的東西)
  12. 打開「庫」,然後點擊「谷歌雲通訊」,然後單擊啓用(在屏幕的頂部)。
  13. 運行polymer serve --open
  14. 點擊paper-toggle-button啓用推送消息(如果當前訂閱不顯示任何東西,嘗試禁用和重新啓用它)在的paper-material
  15. 底部
  16. 複製curl命令命令粘貼到你的終端,並與您在步驟11
  17. 複製的API密鑰替換[YOUR_PUBLIC_API_KEY]當您運行命令,您會收到一個推送消息!

注:如果你在谷歌開發者控制檯中找到的東西的問題,還有一個great Tutorial

+0

太棒了!這比它在Polymer網站上的文檔更好,非常感謝... – RoO