728x90

안드로이드 어플 사용자에게 PUSH 메시지를 발송하기 위해서는 PHP 서버에서 관련 코드를 구현해야 한다.

앱이 백그라운드에서 실행중이거나 앱이 죽어있을 때에도 동작하게 하려면 FCM API를 직접 호출해주는 방식을 사용해야 한다.


api url :: https://fcm.googleapis.com/fcm/send


https://console.firebase.google.com/ 에 접속한다.





서버키 4번을 복사해서 아래 코드에 붙여넣기를 한다.


=== fcmpush.php ====

 <?php
require_once 'dbconnect.php'; // db접속
//데이터베이스에 접속해서 토큰들을 가져와서 FCM에 발신요청
$sql = "select tokenID From member_data where LENGTH(tokenID)>63";
$result = mysqli_query($dbconn,$sql);
$tokens = array();
if(mysqli_num_rows($result) > 0 ){
    while ($row = mysqli_fetch_assoc($result)) {
        $tokens[] = $row['tokenID'];
    }
} else {
    echo 'There are no Transfer Datas';
    exit;
}

mysqli_close($dbconn);

$title = isset($_POST['title'])? $_POST['title'] : "PUSH TEST";
$message = isset($_POST['message'])? $_POST['message'] : "새글이 등록되었습니다";

$arr = array();
$arr['title'] = $title;
$arr['message'] = $message;

$message_status = Android_Push($tokens, $arr);
//echo $message_status;
// 푸시 전송 결과 반환.
$obj = json_decode($message_status);

// 푸시 전송시 성공 수량 반환.
$cnt = $obj->{"success"};

echo $cnt;

function Android_Push($tokens, $message) {
    $url = 'https://fcm.googleapis.com/fcm/send';
    $apiKey = "서버키";

    $fields = array('registration_ids' => $tokens,'data' => $message);
    $headers = array('Authorization:key='.$apiKey,'Content-Type: application/json');

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
    $result = curl_exec($ch);
    if ($result === FALSE) {
        die('Curl failed: ' . curl_error($ch));
    }
    curl_close($ch);
    return $result;
}
?>


=== fcmsender.php 샘플 파일 ===

<!DOCTYPE html>
<head>
<title>FCM Sender</title>
    <meta charset="utf-8" />
</head>
<body>
    <form action="fcmpush.php" method="post">
        <table style="width:100;padding:0;margin:0px;border:none">
        <tr>
            <th style="width:20%">제목</th>
            <td style="width:80%"><input type="text" name="title" value="" style="width:100%"></td>
        </tr>
        <tr>
            <th>내용</th>
            <td>
            <textarea name="message" rows=5 cols=42></textarea>
            </td>
        </tr>
        <tr>
            <td colspan=2 style="text-align: center;"><input type="submit" value="Send Notification" /></td>
        </tr>
        </table>
    </form>
</body>
</html>


FCM PUSH PHP 서버 샘플 소스를 첨부한다.


FCM_Push_Sample.zip


블로그 이미지

Link2Me

,
728x90

last Update : 2019.9.2

사용자 기기의 고유 토큰 정보를 획득하는 방법이 변경되었다.

// 사용자 기기의 고유 토큰 정보를 획득
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( Login.this,  new OnSuccessListener<InstanceIdResult>() {
    @Override
    public void onSuccess(InstanceIdResult instanceIdResult) {
        newToken = instanceIdResult.getToken();
        Log.e("newToken",newToken);

    }
});

Button submit = (Button) findViewById(R.id.login_btn);
submit.setOnClickListener(new Button.OnClickListener() {
    @Override
    public void onClick(View view) {
        loginID = etId.getText().toString().trim();
        loginPW = etPw.getText().toString().trim();

        if (loginID != null && !loginID.isEmpty() && loginPW != null && !loginPW.isEmpty()) {
            Uri.Builder builder = new Uri.Builder()
                    .appendQueryParameter("loginID", Value.encrypt(loginID))
                    .appendQueryParameter("loginPW", Value.encrypt(loginPW))
                    .appendQueryParameter("uID", Value.encrypt(Value.getPhoneUID(context)))
                    .appendQueryParameter("AppVersion", Value.VERSION)
                    .appendQueryParameter("phoneVersion", Build.VERSION.RELEASE)
                    .appendQueryParameter("phoneBrand", Build.BRAND)
                    .appendQueryParameter("phoneModel", Build.MODEL)
                    .appendQueryParameter("mfoneNo", Value.encrypt(getPhoneNumber()))
                    .appendQueryParameter("tokenID", newToken)
                    .appendQueryParameter("keyword", Value.encrypt(Value.URLkey()));
            String urlParameters = builder.build().getEncodedQuery();

            new AsyncLogin().execute(Value.IPADDRESS + "/loginChk.php", urlParameters);
        }
    }
});


========================================================================

Update : 2017.9.2


안드로이드 스튜디오에서 FCM 관련 코드를 구현했으면 이제 구글에서 생성해주는 토큰을 PHP 서버에 등록해야 한다.


토큰(Token)을 등록하는 방법은 로그인 정보에 같이 포함해서 보내는 것이 가장 편리하다.


// 단말기의 ID 정보를 얻기 위해서는 READ_PHONE_STATE 권한이 필요
TelephonyManager mTelephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (mTelephony.getDeviceId() != null){
    uID = mTelephony.getDeviceId();  // 스마트폰 기기 정보
} else {
    uID = Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
}

// 사용자 기기의 고유 토큰 정보를 획득
String getToken = FirebaseInstanceId.getInstance().getToken();

System.out.println("DeviceID : " + uID);
System.out.println("Intro Token : " + getToken);

// 전달할 인자들
String keyword = Value.URLkey();
Uri.Builder builder = new Uri.Builder()
        .appendQueryParameter("loginID", Value.encrypt(params[0]))
        .appendQueryParameter("loginPW", Value.encrypt(params[1]))
        .appendQueryParameter("uID", Value.encrypt(uID))
        .appendQueryParameter("phoneVersion", Build.VERSION.RELEASE)
        .appendQueryParameter("phoneBrand", Build.BRAND)
        .appendQueryParameter("phoneModel", Build.MODEL)
        .appendQueryParameter("tokenID", getToken)
        .appendQueryParameter("keyword", Value.encrypt(Value.URLkey()));
String urlParameters = builder.build().getEncodedQuery();
 


토큰에 대한 정보를 획득하여 로그인하면서 서버로 전송하면 된다.


이제 PHP 서버에서는 이 정보를 사용자 DB에 저장해야 한다.


loginChk.php 파일에서 안드로이드 접속 인증 체크를 한다고 가정하자.

loginID, loginPW 인증에 성공하면

// 토큰 등록 및 갱신
if(isset($tokenID) && strlen($tokenID) > 63){
    $c->registerToken($tokenID,$loginID,$deviceID); // DB에 토큰 저장
}


함수를 포함시켜서 DB에 토큰 정보가 저장되고 Update 되도록 한다.

아래 코드는 토큰 저장에 필요한 함수만 발췌했다.

<?php
class TokenClass {
    // 토큰 등록 및 갱신
    function registerToken($token,$userID,$deviceID){
        global $dbconn;
        $rs = $this->getDeviceChk($userID,$deviceID); // memberuid 또는 0
        if($rs > 0 ){
            $gettokenID = $this->getTokenIDChk($rs); // 0 또는 DB 등록 토큰 반환
            if(($gettokenID == '0') || ($gettokenID !== $token)){ // 등록안되었거나 토큰 불일치
                $sql ="UPDATE member_data SET tokenID='".$token."'";
                $sql.=" where memberuid='".$rs."'";
                if($result = mysqli_query($dbconn,$sql)){
                    return 1;
                } else {
                    return 0;
                }
            }
        }
    }

    function getTokenIDChk($rs){
        global $dbconn;
        $sql ="select tokenID from member_data where memberuid='".$rs."'";
        if($result = mysqli_query($dbconn,$sql)){
            $row = mysqli_fetch_row($result);
            if($row[0] == NULL){
                return 0;
            } else {
                return $row[0];
            }
        } else {
            return 0;
        }
    }

    function getDeviceChk($userID,$deviceID){
        global $dbconn;
        // 사용자 기준 장치 정보 검사
        $sql ="select count(phoneID),memberuid from member_data";
        $sql.=" where memberuid=(select uid from member_id where id='".$userID."') and phoneID='".$deviceID."'";
        if($result = mysqli_query($dbconn,$sql)){
            $row = mysqli_fetch_row($result);
            if($row[0] == 1){
                return $row[1]; // 있으면 memberuid 반환
            } else {
                return $row[0];
            }
        } else {
            return 0;
        }
    }

}//end class
?>
 



DB 테이블 구조

테이블 구조는 참조하여 필요한 것만 발췌하여 이용하면 된다.

member_id.uid = member_data.memberuid


CREATE TABLE IF NOT EXISTS `member_data` (
  `memberuid` int(11) NOT NULL,
  `site` int(11) NOT NULL DEFAULT '0',
  `auth` tinyint(4) NOT NULL DEFAULT '0',
  `level` int(11) NOT NULL DEFAULT '0',
  `admin` tinyint(4) NOT NULL DEFAULT '0',
  `email` varchar(50) NOT NULL DEFAULT '',
  `name` varchar(30) NOT NULL DEFAULT '',
  `nic` varchar(50) NOT NULL DEFAULT '',
  `grade` varchar(20) NOT NULL DEFAULT '',
  `photo` varchar(200) NOT NULL DEFAULT '',
  `sex` tinyint(4) NOT NULL DEFAULT '0',
  `officeNO` varchar(14) NOT NULL DEFAULT '',
  `mobileNO` varchar(14) NOT NULL DEFAULT '',
  `num_login` int(11) NOT NULL DEFAULT '0',
  `d_regis` varchar(14) NOT NULL DEFAULT '',
  `tokenID` varchar(200) DEFAULT NULL,
  `phoneID` varchar(60) DEFAULT NULL,
  `phoneVersion` varchar(20) DEFAULT NULL,
  `phoneBrand` varchar(20) DEFAULT NULL,
  `phoneModel` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`memberuid`),
  KEY `site` (`site`),
  KEY `auth` (`auth`),
  KEY `level` (`level`),
  KEY `admin` (`admin`),
  KEY `email` (`email`),
  KEY `name` (`name`),
  KEY `nic` (`nic`),
  KEY `sex` (`sex`),
  KEY `d_regis` (`d_regis`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


CREATE TABLE IF NOT EXISTS `member_id` (
  `uid` int(11) NOT NULL AUTO_INCREMENT,
  `site` int(11) NOT NULL DEFAULT '0',
  `id` varchar(50) NOT NULL DEFAULT '',
  `pw` varchar(50) NOT NULL DEFAULT '',
  `code` int(6) NOT NULL DEFAULT '0',
  `admin` int(2) NOT NULL DEFAULT '0',
  PRIMARY KEY (`uid`),
  UNIQUE KEY `id` (`id`),
  KEY `code` (`code`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;


블로그 이미지

Link2Me

,
728x90

안드로이드에서 구현할 사항은 newToken 생성하여 DB에 저장되도록 하는 것, PUSH 메시지를 수신하면 팝업된 메시지 창을 누르면 해당 게시글로 이동하여 해당 게시글을 읽어서 내용을 확인하도록 하는 것이 주요 목적이다.


last Update : 2019.8.31

https://firebase.google.com/docs/cloud-messaging/android/client?authuser=0 를 참고하여 파일을 구현한다.


https://medium.com/android-school/firebaseinstanceidservice-is-deprecated-50651f17a148

FirebaseInstanceIdService is deprecated 내용이 나온다.

이걸 참조해서 newToken 생성하는 코드를 추가한다. (다음 게시글에 코드 추가했음)


AndroidManifest.xml 파일 수정사항

<service
    android:name=".FCMListenerService"
    android:stopWithTask="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>
 


FCMListenerService.java 파일

public class FCMListenerService extends FirebaseMessagingService {
    private static final String TAG = "FCM";

    @Override
    public void onNewToken(String s) {
        super.onNewToken(s);
        Log.d(TAG, "token[" + s + "]" );
        /*
         * 기존의 FirebaseInstanceIdService에서 수행하던 토큰 생성, 갱신 등의 역할은 이제부터
         * FirebaseMessaging에 새롭게 추가된 위 메소드를 사용하면 된다.
         */
    }

    // [START receive_message]
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // 메시지를 받았을 때 동작하는 메소드
        String title = remoteMessage.getData().get("title");
        String message = remoteMessage.getData().get("message");
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Title: " + title);
        Log.d(TAG, "Message: " + message);

        NotificationHelper notificationHelper = new NotificationHelper(getApplicationContext());
        notificationHelper.createNotification(title,message);
    }
}


Android 8.0(API 수준 26) 이상부터는 알림 채널이 지원 및 권장된다. FCM은 기본적인 설정으로 기본 알림 채널을 제공한다.


NotificationHelper 는 https://link2me.tistory.com/1514 코드를 참조하면 된다.


===========================================================================

Update : 2017.9.2

FCM(Firebase Clouding Messaging) 을 서버(PHP)에서 PUSH 메시지를 보내면 안드로이드폰에서 받기 위해서는 관련 코드를 구현해야 한다.


GCM 에서 FCM 으로 Migrate 하는 방법에 대해서는 https://developers.google.com/cloud-messaging/android/android-migrate-fcm 에 자세히 설명되어 있다.


1. 토큰 등록 코드

파일명은 본인이 지정하고 싶은데로 지정하면 된다.

이 코드에 추가해서 member id 에 연계하여 토큰정보를 수집하도록 해도 되고, 로그인 코드에 추가해도 된다.


public class FCMInstanceIDListenerService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIDService";

@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String token = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + token);
}


2. FCM 메시지를 수신 받아서 처리하는 코드

    보통 메시지를 수신하면 메시지 제목만 팝업 메시지로 띄우고,

    클릭하면 메시지 내용을 볼 수 있는 UI로 이동하도록 코드를 구현한다.

 public class FCMListenerService extends com.google.firebase.messaging.FirebaseMessagingService {
    private static final String TAG = "FCM";

    int mLastId = 0;
    ArrayList<Integer> mActiveIdList = new ArrayList<Integer>();
    NotificationManager nm;

    // [START receive_message]
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // 메시지를 받았을 때 동작하는 메소드
        String title = remoteMessage.getData().get("title");
        String message = remoteMessage.getData().get("message");
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Title: " + title);
        Log.d(TAG, "Message: " + message);

        sendPushNotification(title,message);
    }

    private void createNotificationId() {
        int id = ++mLastId;
        mActiveIdList.add(id);
    }

    public void sendPushNotification(String title, String message) {
        nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        nm.cancel(mLastId);
        createNotificationId();

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        // 하위 호환성을 위해 NotificationCompat.Builder 사용

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.icon)
                .setContentTitle(title)
                .setSound(defaultSoundUri)
                .setLights(000000255,500,2000)
                .setAutoCancel(true)
                .setWhen(System.currentTimeMillis())
                .setContentText(message);

        Intent popupIntent = new Intent(getApplicationContext(), Popup_Noti.class);
        popupIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        popupIntent.putExtra("msg", title);
        popupIntent.putExtra("LastId", mLastId);
        startActivity(popupIntent); // 메시지 팝업창을 바로 띄운다.

        PendingIntent resultPendingIntent =PendingIntent.getActivity(this, 0, popupIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);

        nm.notify(mLastId, mBuilder.build());
    }
}


getActivity(Context, int, Intent, int) -> Activity를 시작하는 인텐트를 생성함
getBroadcast(Context, int, Intent, int) -> BroadcastReceiver를 시작하는 인텐트를 생성함
getService(Context, int, Intent, int)  -> Service를 시작하는 인텐트를 생성함


사용자가 알림 창의 알림 텍스트를 클릭했을 때 Activity를 시작하려면, setContentIntent()를 호출하여 PendingIntent를 추가한다.


참조 : https://developer.android.com/guide/topics/ui/notifiers/notifications?hl=ko



 public class Popup_Noti extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 키잠금 해제 및 화면 켜기
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

        AlertDialog.Builder builder = new AlertDialog.Builder(Popup_Noti.this);
        builder.setTitle(R.string.app_name);  // 앱의 이름
        builder.setMessage(getIntent().getStringExtra("msg")); // 넘겨받은 메시지 제목
        builder.setCancelable(false);
        builder.setPositiveButton("내용 보기", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Popup_Noti.this, Notice.class); // 공지사항 정보 팝업 및 파싱처리
                startActivityForResult(intent, 0);
                NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                nm.cancel(getIntent().getIntExtra("LastId", -1));
                finish();
            }
        });
        builder.setNegativeButton("닫기", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Popup_Noti.this, Intro.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                        | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                startActivity(intent);
                finish();
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }
}


3. AndroidManifest.xml 파일에 추가할 사항

 <service android:name="com.tistory.link2me.fcm.FCMListenerService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>
<service android:name="com.tistory.link2me.fcm.FCMInstanceIDListenerService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
    </intent-filter>
</service>




블로그 이미지

Link2Me

,