Permalink
Browse files

add registration and receive token

  • Loading branch information...
1 parent a77d170 commit 546320655ee107cff6279c4376c0d8b1b327cb3b @susemi99 committed Jan 26, 2016
@@ -19,6 +19,9 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
+ <service
+ android:name=".services.RegistrationIntentService"
+ android:exported="false"/>
</application>
</manifest>
@@ -15,6 +15,7 @@
import com.google.android.gms.common.GoogleApiAvailability;
import kr.susemi99.gcm.managers.PreferenceManager;
+import kr.susemi99.gcm.services.RegistrationIntentService;
public class MainActivity extends AppCompatActivity
{
@@ -31,6 +32,9 @@ protected void onCreate(Bundle savedInstanceState)
setSupportActionBar(toolbar);
textHello = (TextView) findViewById(R.id.text_hello);
+
+ if (checkPlayServices())
+ startService(new Intent(this, RegistrationIntentService.class));
}
@Override
@@ -0,0 +1,43 @@
+package kr.susemi99.gcm.services;
+
+import android.app.IntentService;
+import android.content.Intent;
+import android.support.v4.content.LocalBroadcastManager;
+import android.util.Log;
+
+import com.google.android.gms.gcm.GoogleCloudMessaging;
+import com.google.android.gms.iid.InstanceID;
+
+import java.io.IOException;
+
+import kr.susemi99.gcm.R;
+import kr.susemi99.gcm.managers.PreferenceManager;
+
+/**
+ * Created by susemi99 on 2016. 1. 26..
+ */
+public class RegistrationIntentService extends IntentService
+{
+ public RegistrationIntentService()
+ {
+ super("");
+ }
+
+ @Override
+ protected void onHandleIntent(Intent intent)
+ {
+ InstanceID instanceID = InstanceID.getInstance(this);
+ try
+ {
+ String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
+ Log.i("RegistrationIntentService | onHandleIntent", "|" + token + "|");
+ PreferenceManager.instance(this).sentToken(true);
+
+ Intent registrationCompleteIntent = new Intent(getString(R.string.action_registration_complete));
+ LocalBroadcastManager.getInstance(this).sendBroadcast(registrationCompleteIntent);
+ } catch (IOException e)
+ {
+ e.printStackTrace();
+ }
+ }
+}

0 comments on commit 5463206

Please sign in to comment.