AndroidのGCMを使いプッシュ通知を実装したいと考えていますが、表題のErrorで止まっております。
止まってる箇所は
private void registerInBackground() {
AsyncTask<Void, Void, String> execute = new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
String msg = "";
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context);
}
String regid = gcm.register(PROJECT_ID);
Log.d("MESSAGE", regid);
msg = "Device registered, registration ID=" + regid;
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
}
Log.d("MESSAGE", msg);
return msg;
}
@Override
protected void onPostExecute(String msg) {
}
}.execute(null, null, null);
}
のString regid = gcm.register(PROJECT_ID);のところです。
Google Could Platformでプロジェクトを作り、GCMを有効にしたのですが、うまくいかずに困っております
ひとつ疑問なのですが、
String regid = gcm.register(PROJECT_ID);
こちらのプロジェクトIDですが、
https://console.developers.google.com/project/forward-theorem-XXX
の
プロジェクト ID: forward-theorem-XXX プロジェクト番号: 99XXXXXXXXXX
の99で始まる数字の方であってますか?
いろいろ調べてみてはいるものの、これという回答が見つかってないので、質問させていただきました。
関係ありそうなコードは晒しておきます。
package shiratsu.ch.co.jp.gcm;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import java.io.IOException;
public class MainActivity extends ActionBarActivity
{
private final String PROJECT_ID = "99XXXXXXXXXXX";
/** Google Cloud Messagingオブジェクト */
private GoogleCloudMessaging gcm;
ViewPagerAdapter mCustomPagerAdapter;
ViewPager mViewPager;
/** コンテキスト */
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCustomPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mCustomPagerAdapter);
context = getApplicationContext();
gcm = GoogleCloudMessaging.getInstance(this);
registerInBackground();
}
private void registerInBackground() {
AsyncTask<Void, Void, String> execute = new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
String msg = "";
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context);
}
String regid = gcm.register("990089809533");
Log.d("MESSAGE", regid);
msg = "Device registered, registration ID=" + regid;
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
}
Log.d("MESSAGE", msg);
return msg;
}
@Override
protected void onPostExecute(String msg) {
}
}.execute(null, null, null);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="shiratsu.ch.co.jp.gcm" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<permission
android:name="shiratsu.ch.co.jp.gcm.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="shiratsu.ch.co.jp.gcm.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver
android:name="shiratsu.ch.co.jp.gcm.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="shiratsu.ch.co.jp.gcm" />
</intent-filter>
</receiver>
<service
android:name="shiratsu.ch.co.jp.gcm.GcmIntentService"
android:exported="false" />
<activity
android:launchMode="singleTop"
android:name="shiratsu.ch.co.jp.gcm.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>