파이어베이스를 연동해서 sns앱을 만들고 있습니다.
상대방이 나의 게시물에 댓글을 달면 Notification으로 알림을 받고 싶습니다.
작동이 정상적으로 하는지 확인하려고 작성한 코드입니다.
앱을 백그라운드로 이동키시면 Log.e메시지가 성공이라고 나옵니다.
@Override
protected void onStop() {
super.onStop();
FcmPush.fcmPush.sendMessage("W02TxxCZp0UNpzhLtkPLdzI7hGw2","hi","hello");
}
제가 하고싶은것은 log를 찍는것이 아니라 Notification으로 알림을 받고 싶습니다.
그런데 어떻게 해야 하는지 모르겠습니다...
그냥 메인에서는 버튼 누르면 Notification알림을 발생시키는 것은 가능한데 onResponse에서 Notification알림은 어떻게 하는지 모르겠습니다...
도와주세요.....
package com.psw.pswinstagram.navigation.util;
import android.util.Log;
import androidx.annotation.NonNull;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.gson.Gson;
import com.psw.pswinstagram.navigation.model.PushDTO;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class FcmPush {
private MediaType JSON = MediaType.parse("application/json; charset=utf-8");
private String url = "https://fcm.googleapis.com/fcm/send";
private String serverKey = "AIzaSyBzf2ainfRVjomSeGAKAiMZplLrJKbrZNk";
private Gson gson;
private OkHttpClient okHttpClient;
// private NotificationManager mNotificationManager;
// public static NotificationManager manager;
// public static NotificationCompat.Builder builder;
// public static String CHANNEL_ID = "channel1";
// public static String CHANEL_NAME = "Channel1";
public static final FcmPush fcmPush = new FcmPush();
public FcmPush(){
gson = new Gson();
okHttpClient = new OkHttpClient();
}
public void sendMessage(String destinationUid,String title,String message){
FirebaseFirestore.getInstance()
.collection("pushTokens")
.document(destinationUid)
.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if(task.isSuccessful()){
String token = task.getResult().get("pushToken").toString();
PushDTO pushDTO = new PushDTO();
pushDTO.setTo(token);
pushDTO.setTitle(title);
pushDTO.setBady(message);
RequestBody body = RequestBody.create(JSON,gson.toJson(pushDTO));
Request request = new Request.Builder()
.addHeader("Content-Type","application/json")
.addHeader("Authorization","key="+serverKey)
.url(url)
.post(body)
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("메세지TAG", "onResponse: 실패");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.e("메세지TAG", "onResponse: 성공");
}
});
}
}
});
}
}