이미 어느 정도 아시니 예를 들어 설명합니다. 아래 코드는 실제 서비스 중인 코드의 일부입니다.
제가 FCM으로 데이터를 보내는 서버 코드의 일부입니다. (제 서비스 중에 자전거를 발견했을 때, 주인에게 알려주는 코드입니다.)
FCMdata fcmData = new FCMdata();
fcmData.to = theUser.FCM_Token;
FCMdataBody fcmBody = new FCMdataBody();
fcmBody.msg_type = "FOUND";
fcmBody.title = "제목";
fcmBody.msg = "자전거 발견";
fcmBody.data = bikeLost.nickname + "_" + bikeLost.location_lost + "_" +
bikeLost.dt_lost;
fcmData.data = fcmBody;
상기 data 필드에 원하는 데이터를 넣어서 내려 주면,
앱에서 data 필드를 아래 listData로 파싱해서 가지니다.
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
String title = remoteMessage.getData().get("title");
String msgType = remoteMessage.getData().get("msg_type");
String msg = remoteMessage.getData().get("msg");
String data = remoteMessage.getData().get("data");
String[] listData = data.split("_");
if (msgType.equalsIgnoreCase("LOST")) {
if (listData.length > 2) {
// 분실신고에 대한 처리 로직 처리
}
} else if (msgType.equalsIgnoreCase("LOSTCANCEL")) {
if (listData.length > 2) {
// 분실 취소
}
} else if (msgType.equalsIgnoreCase("FOUND")) {
if (listData.length > 2) {
showFoundNotification(title, msg, listData[0]);
}
} else {
// 기타..
}
}
showFoundNotification은 다음과 같습니다.
질문하신 것처럼 PendingIntent를 보내면 됩니다.
private void showFoundNotification(String messageTitle, String messageBody, String nickName) {
Intent intent = new Intent(this, ProtectionLocation.class); // 보내고자 하는 Activity
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("NickName", nickName); // 전달하고자 하는 데이터.
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_app)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}