마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

GCM php로 짠 센드메세지 소스 질문이요..

0 추천
php로 짜여진 센드메세지 툴  이있는 소스입니다.

A를 입력하고 센드메세지를 누르면 A라는 푸쉬 메세지가 와야하는데

안드로이드 소스 내에있는 푸쉬 메세지와

토스트 들이 떠요.. 그냥 센드메세지만 눌러도 똑같은 현상이 나타나구요..

친구들 디바이스로 해봤는데 다똑같이 나오네요...

그리고 reg id를 지금은 reg_id1, 2, 3이런식으로 하고있는데

어떤방식으로 하는게 좋을까요 ?
김똥똥 (520 포인트) 님이 2014년 5월 27일 질문

1개의 답변

0 추천
 
채택된 답변
푸쉬메세지가 GCMIntentService부분에 혹시 코딩으로 박혀있는거 아닌가요;;
YeonMin (17,860 포인트) 님이 2014년 5월 27일 답변
김똥똥님이 2014년 6월 5일 채택됨
예맞습니다...지우고 봤는데 어떤방식으로 해보면 되나요 고수님 ㅠㅠ
 protected void onMessage(Context context, Intent intent) {
        Log.d(TAG, "onMessage()");
        
        Bundle bundle = intent.getExtras();
        
        if(bundle != null)
        {
            Log.d(TAG, "onMessage(), bundle: " + bundle);
            Intent newintent = new Intent(BasicInfo.GCM_INTENT_FILLTER);
            newintent.putExtra("message", bundle);
            context.sendBroadcast(newintent);
            String msg = intent.getStringExtra("msg");

            Log.e("getmessage", "getmessage:" + msg);
      
             // 푸쉬왔을때 메세지내용
           generateNotification(context, ".");
        }
    }  이 부분에서 맨마지막에 generateNotification(context, ".");
이부분에입력하는 푸쉬메세지가 옵니다...
php에서 센드메세지창에 보낸 것이 와야하는데....
전에도 비슷한 문제로 답을 달아드린 기억이 나는데...일단 GCM이라는 것 자체를 데이터 전달로 생각하시면 됩니다. PHP에서 JSON으로 만든 데이터를 GoogleGCMServer를 통해 폰으로 전달 하는 것인데요 JSON 키 값을 이용해서 Intent putExtraString을 한다 생각하고 받는 쪽에서 getIntent 한다 생각하면 됩니다. 저기 msg변수에 JSON 키값이 msg인 변수를 받죠. 그 내용을 Notification할 때 뿌려주면 될겁니다. Notification할 때 ticker, msg, title 보통 이 세가지 내용을 뿌려주므로 php에서도 이 세가지를 넘겨줘서 notification할 때 값을 입력하면 됩니다. generateNotificataion함수가 커스텀 함수인거 같은데 그 내용에서 Notification을 쓰는 부분을 보여주시면 더 자세하게 알려드릴 수 있을거 같네요..
/*package com.example.gcmgenius;
 
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gcm.GCMBaseIntentService;
 
 
/**
 * GCMIntentService는 해당 라이브러리에 클래스 이름이 hardCoding되어 있다.
 * 따라서 절대 클래스 이름을 달리하면 안되고 같게 해줘야 한다.
 * 그리고 이 클래스는 Application의 root package에 위치해 있어야 한다.
 *
 *
 *
 
public class GCMIntentService extends GCMBaseIntentService {
    
    private static final String TAG = "GCMIntentService";
 
    /**
     * 에러 발생시 action
     
    @Override
    protected void onError(Context context, String errorId) {
        Log.d(TAG, "onError()");
        
        Intent intent = new Intent(BasicInfo.GCM_INTENT_FILLTER);
        intent.putExtra("message", "에러 발생");
        context.sendBroadcast(intent);
        
    }
 
    
    /**
     * 해당 project에 registrationId 등록
    
    register를 요청하면 GCMIntentService. onRegistered로 registrationId가 들어옴

    @Override //수정해야될 부분인듯
    
    protected void onRegistered(Context context, String registrationId) {
        Log.d(TAG, "onRegistered()");
        
        BasicInfo.RegistrationId = registrationId; //reg아이디 저장 !
        
        SharedPreferences pref = context.getSharedPreferences("myInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString("registrationId", registrationId);
        editor.commit();
    }
 
    
    /**
     * 해당 project에 registrationId 등록 해제
     
    @Override
    protected void onUnregistered(Context context, String registrationId) {
        Log.d(TAG, "onUnregistered()");
        
        Intent intent = new Intent(BasicInfo.GCM_INTENT_FILLTER);
        intent.putExtra("message", "등록 해제");
        context.sendBroadcast(intent);
    }
    
    
    /**
     * 메세지 수신이 됐을 때 호출되는 method
     * intent parameter를 통해서 송신측 message를 받을 수 있다.
     
    @Override
    protected void onMessage(Context context, Intent intent) {
        Log.d(TAG, "onMessage()");
        
       /* Bundle bundle = intent.getExtras();
        
        if(bundle != null)   //노티피케이션 창 ? 아니면 gcm으로 요청을 눌렀을때 푸쉬뜨는
                             //send메세지를 누르거나 php파일을 키면 바로 이쪽으로 오나봄
        {
            Log.d(TAG, "onMessage(), bundle: " + bundle);
            Intent newintent = new Intent(BasicInfo.GCM_INTENT_FILLTER);
            newintent.putExtra("message", bundle);
            context.sendBroadcast(newintent);
            String msg = intent.getStringExtra("msg");

            Log.e("getmessage", "getmessage:" + msg);
      
             // 푸쉬왔을때 메세지내용
           generateNotification(context, null);
        }
    
       
    
    
    /**
     * Issues a notification to inform the user that server has sent a message.
     
    @SuppressWarnings("deprecation")
    private static void generateNotification(Context context, String message) {
        int icon = R.drawable.ic_launcher;
        long when = System.currentTimeMillis();  //
        
        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification (icon, message, when);
         
        String title = context.getString(R.string.app_name);
         
        Intent notificationIntent = new Intent(context, MainActivity.class);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
        //pendingintent - 인텐트 처리후 처리해라~?
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
         
        // 푸쉬오면 소리내주는 소스
        notification.defaults |= Notification.DEFAULT_SOUND;
         
        // 푸쉬오면 진동으로 알려주는 소스
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(0, notification);      
 
    }
    
}
*/
전체 소스입니당... 노티피케이션 쓰는부분..이라고말씀해주셨는데 너무애매한거같아서 다 올려봐여 ㅠㅠ
일단.........generateNotification(context, msg); 해보시길...저기 notification.setLatestEventInfo(context,title,message,intent);부분에서
title이 푸쉬 메시지 제목부분에 나타나는 내용이고 message부분이 질문자가 원하는 메세지 내용입니다. 지금 generateNotification함수 msg부분이 저쪽 message에 대입되고 있으니..함수 매개변수로 title이랑 ticker도 넣어더 쫌 더 notification기능을 완전히 활용할 수 도 있겠습니다. 참고로 intent부분은 푸쉬메시지를 클릭했을 때 수행할 intent로 PendingIntent를 써서 원하는 어플리케이션 activity로 이동시킬 수 있습니다. gcm을 바로 하는 것 보다 notification의 기능을 좀 더 숙지하고 GCM에서 받아온 내용을 적용시키는게 개념잡는데 더 좋을 것 같습니다~
...