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

FCM Token을 받은 상태에서 어떻게 저장하나요

0 추천

따로 빠져나와있는 FCM class 에서 따로 토큰을 부여받는데요..

 

이걸 가지고 preferences 에 바로 저장하고 싶은데요

 

fcm token을 받는 클래스는 context도 없고.. 그래서 어떻게 바로 저장하는지 모르겠습니다..

 

브로드캐스트를 써야하나유..

 

도움좀 주세요..

 

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

    private static final String TAG = "TAG";

    /**
     * Called if InstanceID token is updated. This may occur if the security of
     * the previous token had been compromised. Note that this is called when the InstanceID token
     * is initially generated so this is where you would retrieve the token.
     */
    // [START refresh_token]
    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        NsokLog.d(TAG, "Refreshed token: " + refreshedToken);
        
        // TODO 어떻게 저장하나요..

        // If you want to send messages to this application instance or
        // manage this apps subscriptions on the server side, send the
        // Instance ID token to your app server.
        sendRegistrationToServer(refreshedToken);
    }
    // [END refresh_token]

    /**
     * Persist token to third-party servers.;
     *
     * Modify this method to associate the user's FCM InstanceID token with any server-side account
     * maintained by your application.
     *
     * @param token The new token.
     */
    private void sendRegistrationToServer(String token) {
        // TODO: Implement this method to send token to your app server.
        NsokLog.d(TAG , "sendRegistrationToServer : " + token);
    }
}
윤둥이 (4,560 포인트) 님이 2017년 10월 31일 질문
윤둥이님이 2017년 10월 31일 수정

2개의 답변

0 추천
Activity내에서 FirebaseInstanceId.getInstance().getToken(); 하시면됩니다
qweqweq (4,220 포인트) 님이 2017년 10월 31일 답변
0 추천

https://developer.android.com/reference/android/content/SharedPreferences.html

context의 getSharedPreference 함수를 사용하여 프리퍼런스 객체를 가져온 다음

SharedPreference 함수인 edit()를 호출하여 editor를 생성 합니다 .

이후 putString 함수를 사용하여 토큰을 설정한후

commit 하여 저장 합니다

정리 하자면 아래와 같습니다.

SharedPreference pref = getSharedPreferences("SAVE", Context.PRIVATE_MODE) ;

Editor edit = pref.edit();

edit.putString("TOKEN", tokenvalue);

edit.commit();

익명사용자 님이 2017년 11월 1일 답변
...