2017-05-05 155 views
0

我在瀏覽器中有一個(超級)基本FCM應用程序,並且在YouTube上的Firecasts之後,我撞上了牆。Firebase FCM空令牌

我應該在打印時通過調用messaging.getToken()並將令牌記錄到控制檯來代替null

我的代碼如下:

<script src="https://www.gstatic.com/firebasejs/3.9.0/firebase-app.js"></script> 
<script src="https://www.gstatic.com/firebasejs/3.9.0/firebase-messaging.js"></script>' 

我只需要火力地堡SDK的FCM部分(我已經與它的所有進賬過嘗試這樣做)。

<script> 
var config = { 
    apiKey: "MY-API-KEY-IS-HERE-BUT-I-REMOVED-IT-FOR-STACKOVERFLOW", 
    authDomain: "DOMAIN.firebaseapp.com", 
    databaseURL: "https://DOMAIN.firebaseio.com", 
    projectId: "PROJECTS_ID", 
    storageBucket: "DOMAIN.appspot.com", 
    messagingSenderId: "ID" 
}; 

firebase.initializeApp(config); 

const messaging = firebase.messaging(); 

messaging.requestPermission() 
    .then(function(e=null) { 
     console.log("Granted!"+e); 
     return messaging.getToken(); 
    }) 
    .then(function(token) { 
     console.log(token); 
    }) 
     .catch(function(err) { 
     console.log("Error! :: "+err); 
    }); 
</script> 
</body> 
</html> 

回答

0

你做出了一個錯誤的函數鏈,

2nd then()必須getToken()後,

<script> 
var config = { 
    apiKey: "MY-API-KEY-IS-HERE-BUT-I-REMOVED-IT-FOR-STACKOVERFLOW", 
    authDomain: "DOMAIN.firebaseapp.com", 
    databaseURL: "https://DOMAIN.firebaseio.com", 
    projectId: "PROJECTS_ID", 
    storageBucket: "DOMAIN.appspot.com", 
    messagingSenderId: "ID" 
}; 

firebase.initializeApp(config); 

const messaging = firebase.messaging(); 

messaging.requestPermission() 
    .then(function(e=null) { 
     console.log("Granted!"+e); 
     return messaging.getToken() 
      .then(function(token) { 
       console.log(token); 
      }).catch(function(err) { 
       console.log("Error! :: "+err); 
      }); 
    }) 
</script> 
</body> 
</html>