我关注了这个推送通知 tutorial .
当我完成教程时,我发现有两个类没有使用,它们是 AuthenticationUtil
和 MessageUtil
。
此外,Google Login ,这个链接似乎行不通。其次,这是 Android 设备或帐户的 token ID?我认为推送通知是将消息推送到 Android 设备的 token ID。
另一方面,我发现 bundle.putExtra(key, value)
,所有的键都没有使用它。例如放入 "app"
但在 C2DMRegistrationReceiver()
中没有得到 key 。
在这个sendRegistrationIdToServer()
中,好像从来没有被调用过。
我对这个关于推送通知的教程感到困惑。
谁可以指导我或给我有关推送通知的可行教程或示例?
希望高手指出我的错误。
这是我的注册号
public static final String[] REGISTRATION_ID = {
"APA91bFV6MwoAH0UNop69PZ2liKpSBUHSHenIuPzh44_6GdGKzVCLvoH_NM31eMZMVLZi-SAIFwP4iZaE72dSWkIh3GaD0RQYpPm9zO0ARWmnoxFyyyreL_KpQ9Qd_p0broclT12RhA4Ymk0cBT00CmpsbSHIwyxig",
"APA91bEwmxgvs7zNbKC4p0n4DoTEM73DTihnQgBOP8Gxhf2sVW-fgltugDgS1Fh2S4KvN1wQHbMNJEIzieJ9F1nNPqs3NWeKGbB7IBYpKJq4xmN4Z7uzkjZQQUKGD8jW--AwfQY5McINBto9GAL_87_u5WkIq-kx3g",
"APA91bH63Zgxn1X_MZ56UzrlRpffvmiLAIsqxvBUTMUHP2O_MT_VU9Ork_edXKHlml-PZSkjKEqdk8EKv5HvxbPdK1Vva3WtmqsPZfhXzEbtNIrwrqIvvRf7hL835rDc4t2E8EKUBj1dX2ta0OxY5pY3Xlhkyb1sBg",
"APA91bGqT5Wo6eUaMdqT5r9TlGbKSX6GN2W6r-RjrRXz5T5v3j87flcQRyfSajmMNGXuPVe-fwZydRmvyYu63tWnYohDmpJyKkXOxs8Vx6P_FplFQ__ufR_hekwqGOspeUc6bfc8fhbMPGN3Ft9l-bfrghJwwk79jw"};
消息工具
public static int sendMessage(String auth_token, String registrationId,
String message, String title) throws IOException {
StringBuilder postDataBuilder = new StringBuilder();
postDataBuilder.append(PARAM_REGISTRATION_ID).append("=")
.append(registrationId);
postDataBuilder.append("&").append(PARAM_COLLAPSE_KEY).append("=")
.append("0");
postDataBuilder.append("&").append("data.payload").append("=")
.append(URLEncoder.encode(message, UTF8));
postDataBuilder.append("&").append("data.title").append("=")
.append(URLEncoder.encode(title, UTF8));
byte[] postData = postDataBuilder.toString().getBytes(UTF8);
// Hit the dm URL.
URL url = new URL("https://android.clients.google.com/c2dm/send");
HttpsURLConnection
.setDefaultHostnameVerifier(new CustomizedHostnameVerifier());
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
conn.setRequestProperty("Content-Length",
Integer.toString(postData.length));
conn.setRequestProperty("Authorization", "GoogleLogin auth="
+ auth_token);
OutputStream out = conn.getOutputStream();
out.write(postData);
out.close();
int responseCode = conn.getResponseCode();
return responseCode;
}
private static class CustomizedHostnameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
return true;
}
}
消息发送者
public static void main(String[] args) throws IOException {
String token = AuthenticationUtil.getToken(SecureStorage.USER,
SecureStorage.PASSWORD);
for (int i = 0; i < ServerConfiguration.REGISTRATION_ID.length; i++) {
MessageUtil.sendMessage(token,
ServerConfiguration.REGISTRATION_ID[i], "12358",
"印尼羽賽:馬2單1雙止步入選賽");
System.out.println(ServerConfiguration.REGISTRATION_ID[i]
.toString());
}
System.out.println(token);
}
最佳答案
你应该关注this android c2dm 实现教程。
对于服务器,你可以使用任何东西,一些在互联网上可用的代码示例。对于服务器,我使用了名为“C2DM Sharp”的 .NET 库
过程很简单,就像...
- 首先在 - https://developers.google.com/android/c2dm/signup 上注册您的 c2dm 谷歌电子邮件地址
- 在 Android 2.2 或更高版本上运行 android 应用程序并发送您可以在“C2DMReceiver”中获取的 registrationID 或通过写入 LOG 获取该 ID
- 使用服务器代码,出于测试目的,将您的注册 ID 粘贴到服务器代码中,您就可以开始了。
C2DM的基本流程是... Register Phone for C2DM -> Get registrationID -> 将 registrationID 发送到服务器 -> 服务器使用 google id 获取身份验证 token -> server use registrationID and auth token to send message.
关于android - Android中推送通知的困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10990211/