0
我正在研究使用Google Cloud Messaging API接收推送通知的Chrome应用/扩展程序。我遵循的教程是this。在“获取GCM注册令牌”部分之前,一切都是清楚的。如何从Chrome应用的GCM连接服务器获取注册令牌?
下面的代码解释了注册过程的一部分。
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
方法是否会返回可用作注册令牌的东西?请帮助我!
P.S:可用的教程非常过时。如果有人有任何更新的教程/样本,请告诉我。