package com.dbs.mplus.fatima.service;

import java.io.IOException;

import com.dbs.mplus.fatima.framework.util.MPlusGCMUtil;
import com.dbs.mplus.fatima.framework.util.MPlusLogUtil;
import com.dbs.mplus.fatima.framework.util.MPlusPreferenceUtil;
import com.google.android.gms.iid.InstanceIDListenerService;
/**
 * 토큰변경 받아오기 
 * 
 * @author aple
 *
 */
public class DeviceTokenStatusListeningService extends InstanceIDListenerService {
	@Override 
	public void onTokenRefresh() { 
		MPlusLogUtil.INFO(DeviceTokenStatusListeningService.class, "onTokenRefresh");
		
		String token = null; 
		
		try { 
			// token 다시 가져오고 
			token = MPlusGCMUtil.refreshDeviceToken(this.getApplicationContext());
		}
		catch(final IOException ex) { 
			MPlusLogUtil.INFO(this.getClass(), "fail to refresh device token");
			return; 
		}
		
		// 저장하고 
		final MPlusPreferenceUtil p = MPlusPreferenceUtil.getInstance(this.getApplicationContext());
		p.setDeviceToken(token); 
	}
}
