Skip to content

Commit

Permalink
add registration and receive token
Browse files Browse the repository at this point in the history
  • Loading branch information
susemi99 committed Jan 26, 2016
1 parent a77d170 commit 5463206
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Expand Up @@ -19,6 +19,9 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service
android:name=".services.RegistrationIntentService"
android:exported="false"/>
</application>

</manifest>
4 changes: 4 additions & 0 deletions app/src/main/java/kr/susemi99/gcm/MainActivity.java
Expand Up @@ -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
{
Expand All @@ -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
Expand Down
@@ -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.