GCM GcmListenerService和InstanceIDListenerService

时间:2016-06-18 22:59:44

标签: java android push-notification google-cloud-messaging

我关注了这个链接https://developers.google.com/cloud-messaging/android/start,但我并不了解一些事情。

  1. 我从哪里开始GcmListenerServiceInstanceIDListenerService(服务),因为在示例中,他们不会在代码中的任何位置启动它。这些课程是自动启动的吗?
  2. 我的GcmListenerService课程:

    public class GcmMessageReceiver extends GcmListenerService {
    
    public GcmMessageReceiver() {
        Log.i("TEST", "GcmMessageReceiver");
    }
    
    @Override
    public void onMessageReceived(String from, Bundle data) {
        String message = data.getString("message");
    
        Log.i("TEST", data.toString());
        Log.i("TEST", "Success!");
    
    }
    

    GcmListenerService清单声明:

    <service
            android:name=".GcmMessageReceiver"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
    

    我的InstanceIDListenerService类:

    public class GcmOnTokenRefresh extends InstanceIDListenerService {
    
    public GcmOnTokenRefresh(){
        Log.i("TEST", "GcmOnTokenRefresh");
    }
    
    
    @Override
    public void onTokenRefresh() {
        startService(new Intent(this, RegisterTokenService.class));
        super.onTokenRefresh();
    }
    }
    

    我的InstanceIDListenerService清单声明:

        <service
            android:name=".GcmOnTokenRefresh"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID" />
            </intent-filter>
        </service>
    

1 个答案:

答案 0 :(得分:0)

是的,你是对的,如果你将它们写入AndroidManifest.xml文件,它们会自动启动。就像后台的所有其他服务一样。