2016-05-23 4 views
0

、私はGCMの例をダウンロードし、リフレッシュトークンを検出することができ、サービスがあり、次のように、コードは次のとおりです。GCM:AndroidでInstanceIDListenerServiceを自分で開始する必要がありますか?公式<a href="https://developers.google.com/cloud-messaging/android/start" rel="nofollow">website</a>から

public class MyInstanceIDListenerService extends InstanceIDListenerService { 

private static final String TAG = "MyInstanceIDLS"; 

/** 
* Called if InstanceID token is updated. This may occur if the security of 
* the previous token had been compromised. This call is initiated by the 
* InstanceID provider. 
*/ 
// [START refresh_token] 
@Override 
public void onTokenRefresh() { 
    // Fetch updated Instance ID token and notify our app's server of any changes (if applicable). 
    Intent intent = new Intent(this, RegistrationIntentService.class); 
    startService(intent); 
} 
// [END refresh_token] 
} 

私の質問は:私が開始するコードを書く必要があります自分でサービス? と同じように:

startService(new Intent(getActivity(), MyInstanceIDListenerService.class)); 

答えて

1

いいえ。このサービスは、Google Playサービスで必要に応じて開始されます。 AndroidManifest.xmlファイルにサービスを定義するようにしてください。また、あなたが始めているのであれば、より簡単に実装することができます。FCM

FCM hereのサンプルを参照してください。

+0

ありがとうございます、Google Playのサービス、開始方法(どのクラスから始めるか)を詳しく教えてください。 「GcmListenerService」が「com.google.android.gms.gcm.GcmReceiver」によって開始されたのと同じように、InstanceIDListenerServiceの起動方法を教えてください。 – fhlkm

+0

Google Playサービスは常に稼働しています。 InstanceIDListenerServiceはGcmReceiverによっても開始されます。 –

+0

sendRegistrationIdToサーバコールに失敗した場合はどうなりますか? – Javanator

関連する問題