Javascript 如何为Chrome应用程序从GCM连接服务器获取注册令牌?
我正在开发一个chrome应用程序/扩展,它使用Google云消息API接收推送通知。我下面的教程是。在“获取GCM注册令牌”部分之前,一切都很清楚 下面的代码解释了注册过程的一部分Javascript 如何为Chrome应用程序从GCM连接服务器获取注册令牌?,javascript,google-chrome,google-chrome-extension,google-cloud-messaging,chrome-gcm,Javascript,Google Chrome,Google Chrome Extension,Google Cloud Messaging,Chrome Gcm,我正在开发一个chrome应用程序/扩展,它使用Google云消息API接收推送通知。我下面的教程是。在“获取GCM注册令牌”部分之前,一切都很清楚 下面的代码解释了注册过程的一部分 function registerCallback(registrationId) { if (chrome.runtime.lastError) { // When the registration fails, handle the error and retry the // regist
function registerCallback(registrationId) {
if (chrome.runtime.lastError) {
// When the registration fails, handle the error and retry the
// registration later.
return;
}
// Send the registration token to your application server.
sendRegistrationId(function(succeed) {
// Once the registration token is received by your server,
// set the flag such that register will not be invoked
// next time when the app starts up.
if (succeed)
chrome.storage.local.set({registered: true});
});
}
function sendRegistrationId(callback) {
// Send the registration token to your application server
// in a secure way.
}
chrome.runtime.onStartup.addListener(function() {
chrome.storage.local.get("registered", function(result) {
// If already registered, bail out.
if (result["registered"])
return;
// Up to 100 senders are allowed.
var senderIds = ["Your-Sender-ID"];
chrome.gcm.register(senderIds, registerCallback);
});
});
我知道我们必须使用chrome.gcm.register
来注册我们的应用程序,但他们没有提到如何获得代币。chrome.gcm.register
方法是否返回可以用作注册令牌的内容?请帮帮我
附言:现有的教程已经过时了。如果有人有任何更新的教程/示例,请告诉我。您的回复:
function registerCallback(registrationId) {
将被调用并将注册ID传递给它。在那里,您可以将其保存到本地存储或执行任何您想要的操作