package com.krGCM_PHONE_LOCK;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.google.android.gcm.GCMRegistrar;
import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.Result;
import com.google.android.gcm.server.Sender;
import java.io.IOException;
import java.util.logging.Handler;
public class MyActivity extends Activity {
final static String SENDER_ID = "1017539908336"; // GCM 프로젝트 생성 시 발급 받은 Project_number
final static String SERVER_API_KEY = "AIzaSyAKuZ6ZM3_JfHS4y3tlv9O1vOvdjb6eq0U"; // API_ACCESS 페이지에서 받은 서버 API KEY
Button button;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// GCMRegistrar 클래스는 static 클래스로써 클라이언트 등록에 관한 정보를 가지고 있다.
// 클라이언트 ID 가 필요하다면 여기서 가져다 쓰면 됨.
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, SENDER_ID);
} else {
Log.d("MyActivity", "oncreated regId = "+regId);
Log.d("MyActivity", "already registered.");
}
button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread(new Runnable() {
@Override
public void run() {
// 서버 API_KEY 입력
Sender sender = new Sender(SERVER_API_KEY);
Message message = new Message.Builder()
.collapseKey(
String.valueOf(Math.random() % 100 + 1))
.delayWhileIdle(true).timeToLive(3)
.addData("no", "1").addData("title", "Hello")
.addData("msg", "GCM TEST").build();
try {
// 클라이언트 아이디를 가지고 온다.
String regId = GCMRegistrar.getRegistrationId(MyActivity.this);
Log.d("GCMIntentService", "Send : " + regId);
Result result = sender.send(message, regId, 5);
Log.v("GCMIntentService", "result : " + result.getCanonicalRegistrationId());
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
});
}
@Override
protected void onPause(){
super.onPause();
if (!GCMRegistrar.getRegistrationId(this).equals("")){
GCMRegistrar.unregister(this);
}
}
}
package com.krGCM_PHONE_LOCK;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gcm.GCMBaseIntentService;
import java.util.Iterator;
/**
* Created with IntelliJ IDEA.
* User: Parkjeehwan
* Date: 13. 6. 8
* Time: 오후 6:16
* To change this template use File | Settings | File Templates.
*/
public class GCMIntentService extends GCMBaseIntentService {
private static final String SENDER_ID = "1017539908336";
private static final String TAG = "GCMIntentService";
public GCMIntentService(){
this(SENDER_ID);
}
public GCMIntentService(String senderId) {
super(senderId);
}
@Override
protected void onMessage(Context context, Intent intent) {
//To change body of implemented methods use File | Settings | File Templates.
Vibrator vibrator = (Vibrator)getSystemService(context.VIBRATOR_SERVICE);
vibrator.vibrate(1000);
Toast.makeText(this, "HelloWorld", 1000 );
Bundle b = intent.getExtras();
Iterator<String> iterator = b.keySet().iterator();
while(iterator.hasNext()) {
String key = iterator.next();
String value = b.get(key).toString();
Log.d(TAG, "onMessage. "+key+" : "+value);
}
}
@Override
protected void onError(Context context, String s) {
//To change body of implemented methods use File | Settings | File Templates.
Log.d("ERROR", "errorId : "+s);
}
@Override
protected void onRegistered(Context context, String s) {
//To change body of implemented methods use File | Settings | File Templates.
Log.d("onRegistered", "onRegistered - regId : "+s);
}
@Override
protected void onUnregistered(Context context, String s) {
//To change body of implemented methods use File | Settings | File Templates.
}
}
일단 잘 몰라서 A -> GCM -> A 형태로 짰습니다.
저는 A -> GCM -> B 로 짜고 싶은데 잘 안되네요. A 휴대폰에서 버튼을 누르면 B 휴대폰에서 진동을 울리게 하는 식으로 하고 싶은데 이거 참 ;;;
답변 부탁드려요...