gpt4 book ai didi

android - 实现 GCM 客户端的 GoogleCloudMessaging API

转载 作者:太空宇宙 更新时间:2023-11-03 11:44:04 25 4
gpt4 key购买 nike

你好,我正在按照 android 的官方文档发送推送通知,因为旧方法已被弃用,但我在实现 GCM 客户端时遇到问题

http://developer.android.com/google/gcm/client.html

教程就写到这里了,往下翻看

private void registerInBackground()

当我在我的应用程序上写这个函数时它给我这个错误

Syntax error on token "void", @ expected

我用谷歌搜索了一下,现在我知道了这个错误,因为这个方法试图在一个方法内部创建一个方法,但我仍然很困惑,因为它是官方文档,我一定做错了什么,有人能指出来吗?这是本教程中的方法:

private void registerInBackground() {
new AsyncTask() {
@Override
protected String doInBackground(Void... params) {
String msg = "";
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context);
}
regid = gcm.register(SENDER_ID);
msg = "Device registered, registration ID=" + regid;

// You should send the registration ID to your server over HTTP,
// so it can use GCM/HTTP or CCS to send messages to your app.
// The request to your server should be authenticated if your app
// is using accounts.
sendRegistrationIdToBackend();

// For this demo: we don't need to send it because the device
// will send upstream messages to a server that echo back the
// message using the 'from' address in the message.

// Persist the regID - no need to register again.
storeRegistrationId(context, regid);
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
// If there is an error, don't just keep trying to register.
// Require the user to click a button again, or perform
// exponential back-off.
}
return msg;
}

@Override
protected void onPostExecute(String msg) {
mDisplay.append(msg + "\n");
}
}.execute(null, null, null);
...
/**
* Sends the registration ID to your server over HTTP, so it can use GCM/HTTP
* or CCS to send messages to your app. Not needed for this demo since the
* device sends upstream messages to a server that echoes back the message
* using the 'from' address in the message.
*/
private void sendRegistrationIdToBackend() {
// Your implementation here.
}
}

现在看到 sendRegistrationIdToBackend 位于方法本身内部,有什么帮助吗?

最佳答案

这是我找到的解决方案,令人失望的是,有 33 人查看了它,但无论如何都没有人愿意回答,这里是代码

private void registerInBackground() {
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
String msg = "";
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context);
}
regid = gcm.register(SENDER_ID);
msg = "Device registered, registration ID=" + regid;

// You should send the registration ID to your server over HTTP, so it
// can use GCM/HTTP or CCS to send messages to your app.
sendRegistrationIdToBackend();

// For this demo: we don't need to send it because the device will send
// upstream messages to a server that echo back the message using the
// 'from' address in the message.

// Persist the regID - no need to register again.
storeRegistrationId(context, regid);
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
// If there is an error, don't just keep trying to register.
// Require the user to click a button again, or perform
// exponential back-off.
}
return msg;
}

@Override
protected void onPostExecute(String msg) {
mDisplay.append(msg + "\n");
}
}.execute(null, null, null);
}

关于android - 实现 GCM 客户端的 GoogleCloudMessaging API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19068464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com