안녕하세요 ^^
GCM으로 notification 받는데 까지 성공했는데 클릭시 원래 앱이 실행되야되는데 이게 에뮬에서는 정상적으로 돌아가는데 실제 폰에서는 작동이 안됩니다.(받는것까진됩니다) 원래 폰에서도 됬는데 기능을 추가하려고 코드를 자꾸 추가하다보니 어느샌가 이게 안되더라고요;(에뮬에선 여전히 됩니다;;) 대체 왜그런건가요?? 아래는 코드입니다
브로드캐스트리시버 액티비티
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
public static boolean bAppRunned = false;
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
if (bAppRunned) {
} else {
Intent intent_ = new Intent(context, PopupActivity.class);
intent_.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); // 이거 안해주면 안됨
context.startActivity(intent_);
}
}
}
GcmIntentService 액티비티
public class GcmIntentService extends IntentService {
public static final String TAG = "icelancer";
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public GcmIntentService() {
// Used to name the worker thread, important only for debugging.
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.
MESSAGE_TYPE_MESSAGE.equals(messageType)) {
// This loop represents the service doing some work.
for (int i=0; i<5; i++) {
Log.i(TAG, "Working... " + (i + 1)
+ "/5 @ " + SystemClock.elapsedRealtime());
try {
Thread.sleep(5000);
} catch (InterruptedException ignored) {
}
}
Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
// Post notification of received message.
sendNotification ( extras.getString("msg"));
Log.i(TAG, "Received: " + extras.toString());
}
}
// Release the wake lock provided by the WakefulBroadcastReceiver.
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
// Put the message into a notification and post it.
// This is just one simple example of what you might choose to do with
// a GCM message.
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.gcm)
.setContentTitle("본재팬")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg)
.setVibrate(new long[]{0, 500});
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}