GCM:我应该在Android中自己启动InstanceIDListenerService吗?

时间:2016-05-23 19:58:50

标签: android google-cloud-messaging

从官方website开始,我下载了一个GCM示例,有一项服务可以检测到刷新令牌,代码如下:

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 个答案:

答案 0 :(得分:1)

没有。 Google Play服务在必要时启动此服务。只需确保在AndroidManifest.xml文件中定义服务即可。此外,如果您刚刚开始,您可能希望看一下更容易实现FCM

查看FCM here的示例。

相关问题