Java 如何为GCM创建库,库创建不起作用
我正在尝试为我的应用创建一个库,在我的所有应用中都有推送通知 我想用这个包创建一个库 在GCM中我有什么限制吗?因为它看起来像是获取生成ID_令牌的包名 我有一个应用程序,它有一个包,里面有我用于推送通知的类,它工作得非常完美 现在我已经迁移了这个包并创建了一个库,因为所有其他应用程序都只是指向库,它将正常工作 只是出于某种原因,他没有给自由党打电话,我做了所有的事情,但我做不到 在GCM中注册ID并启动服务的代码如下:Java 如何为GCM创建库,库创建不起作用,java,android,Java,Android,我正在尝试为我的应用创建一个库,在我的所有应用中都有推送通知 我想用这个包创建一个库 在GCM中我有什么限制吗?因为它看起来像是获取生成ID_令牌的包名 我有一个应用程序,它有一个包,里面有我用于推送通知的类,它工作得非常完美 现在我已经迁移了这个包并创建了一个库,因为所有其他应用程序都只是指向库,它将正常工作 只是出于某种原因,他没有给自由党打电话,我做了所有的事情,但我做不到 在GCM中注册ID并启动服务的代码如下: Intent intent = new Intent(th
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
上述代码在我的主要活动中
我想这样做,他就可以给图书馆打电话了
编辑:
我正在使用Eclipse和GCM
My class`registrationtentservice`
公共类注册服务扩展了IntentService{
私有静态最终字符串TAG=“RegServicePush”;
字符串newRegID=“”;
字符串GetEmail=“”;
公共注册服务(){
超级(标签);
}
@凌驾
受保护的手部内容无效(意图){
试一试{
InstanceID InstanceID=InstanceID.getInstance(此);
String token=instanceID.getToken(Constants.GCM\u SENDER\u ID,GoogleCloudMessaging.INSTANCE\u ID\u SCOPE,null);
//TODO:实现此方法以将任何注册发送到
//应用程序的服务器。
sendRegistrationToServer(令牌、电子邮件);
}捕获(例外e){
Log.d(标记“未能完成令牌刷新”,e);
}
//通知UI注册已完成,因此进度指示器
//可以隐藏。
}
私有void sendRegistrationToServer(字符串令牌、字符串电子邮件){
//MainActivity.newRegID=令牌;
WebServerRegistrationTask webServer=新的WebServerRegistrationTask();
execute();
}
公共类WebServerRegistrationTask扩展了AsyncTask{
@凌驾
受保护的Void doInBackground(Void…参数){
SharedPreferences SharedPreferences=首选项管理器
.GetDefaultSharedReferences(RegistrationEntertService.this);
URL=null;
试一试{
url=新url(常量.WEB\u服务器\u url);
}捕获(格式错误){
e、 printStackTrace();
SharedReferences.edit().putString(Constants.PREF_GCM_REG_ID,”).apply();
}
Map dataMap=newhashmap();
dataMap.put(“regID”,newRegID);
dataMap.put(“appID”,Constants.APP_ID);
StringBuilder postBody=新建StringBuilder();
迭代器迭代器=dataMap.entrySet().Iterator();
while(iterator.hasNext()){
Entry param=(Entry)迭代器.next();
postBody.append(param.getKey()).append('=').append(param.getValue());
if(iterator.hasNext()){
postBody.append('&');
}
}
字符串体=postBody.toString();
byte[]bytes=body.getBytes();
HttpURLConnection conn=null;
试一试{
conn=(HttpURLConnection)url.openConnection();
连接设置输出(真);
conn.SETUSECHACHES(假);
conn.setFixedLengthStreamingMode(bytes.length);
conn.setRequestMethod(“POST”);
conn.setRequestProperty(“内容类型”,“应用程序/x-www-form-urlencoded;字符集=UTF-8”);
OutputStream out=conn.getOutputStream();
out.write(字节);
out.close();
字符串响应=”;
InputStream=null;
试一试{
is=conn.getInputStream();
int-ch;
StringBuffer sb=新的StringBuffer();
而((ch=is.read())!=-1){
某人附加((字符)ch);
}
response=sb.toString();
}捕获(IOE异常){
投掷e;
}最后{
如果(is!=null){
is.close();
}
}
int status=conn.getResponseCode();
如果(状态==200){
如果(响应等于(“1”)){
SharedReferences.edit().putString(Constants.PREF_GCM_REG_ID,newRegID).apply();
Intent registrationComplete=新的Intent(Constants.SERVER\u SUCCESS);
LocalBroadcastManager.getInstance(RegistrationInputService.this)
.发送广播(注册完成);
}
}否则{
抛出新IOException(“请求失败,错误代码“+状态”);
}
}捕获(协议异常pe){
pe.printStackTrace();
SharedReferences.edit().putString(Constants.PREF_GCM_REG_ID,”).apply();
}捕获(io异常){
io.printStackTrace();
SharedReferences.edit().putString(Constants.PREF_GCM_REG_ID,”).apply();
}最后{
如果(conn!=null){
连接断开();
}
}
返回null;
}
}
}
My class `RegistrationIntentService`
public class RegistrationIntentService extends IntentService {
private static final String TAG = "RegServicePush";
String newRegID = "";
String GetEmail = "";
public RegistrationIntentService() {
super(TAG);
}
@Override
protected void onHandleIntent(Intent intent) {
try {
InstanceID instanceID = InstanceID.getInstance(this);
String token = instanceID.getToken(Constants.GCM_SENDER_ID, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
// TODO: Implement this method to send any registration to your
// app's servers.
sendRegistrationToServer(token, email);
} catch (Exception e) {
Log.d(TAG, "Failed to complete token refresh", e);
}
// Notify UI that registration has completed, so the progress indicator
// can be hidden.
}
private void sendRegistrationToServer(String token, String email) {
//MainActivity.newRegID = token;
WebServerRegistrationTask webServer = new WebServerRegistrationTask();
webServer.execute();
}
public class WebServerRegistrationTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(RegistrationIntentService.this);
URL url = null;
try {
url = new URL(Constants.WEB_SERVER_URL);
} catch (MalformedURLException e) {
e.printStackTrace();
sharedPreferences.edit().putString(Constants.PREF_GCM_REG_ID, "").apply();
}
Map<String, String> dataMap = new HashMap<String, String>();
dataMap.put("regID", newRegID);
dataMap.put("appID", Constants.APP_ID);
StringBuilder postBody = new StringBuilder();
Iterator<Map.Entry<String, String>> iterator = dataMap.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, String> param = (Entry<String, String>) iterator.next();
postBody.append(param.getKey()).append('=').append(param.getValue());
if (iterator.hasNext()) {
postBody.append('&');
}
}
String body = postBody.toString();
byte[] bytes = body.getBytes();
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setFixedLengthStreamingMode(bytes.length);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
OutputStream out = conn.getOutputStream();
out.write(bytes);
out.close();
String response = "";
InputStream is = null;
try {
is = conn.getInputStream();
int ch;
StringBuffer sb = new StringBuffer();
while ((ch = is.read()) != -1) {
sb.append((char) ch);
}
response = sb.toString();
} catch (IOException e) {
throw e;
} finally {
if (is != null) {
is.close();
}
}
int status = conn.getResponseCode();
if (status == 200) {
if (response.equals("1")) {
sharedPreferences.edit().putString(Constants.PREF_GCM_REG_ID, newRegID).apply();
Intent registrationComplete = new Intent(Constants.SERVER_SUCCESS);
LocalBroadcastManager.getInstance(RegistrationIntentService.this)
.sendBroadcast(registrationComplete);
}
} else {
throw new IOException("Request failed with error code " + status);
}
} catch (ProtocolException pe) {
pe.printStackTrace();
sharedPreferences.edit().putString(Constants.PREF_GCM_REG_ID, "").apply();
} catch (IOException io) {
io.printStackTrace();
sharedPreferences.edit().putString(Constants.PREF_GCM_REG_ID, "").apply();
} finally {
if (conn != null) {
conn.disconnect();
}
}
return null;
}
}
}