今回の記事では、前回から引き続き、Androidアプリでプッシュ通知を実装するやり方についてです。
○アプリを作り、アプリから[レジストレーションID]を取得する
アプリを作成し、前回の記事でGoogleAPIコンソールから引っ張ってきたプロジェクトIDを入れ込み、サーバーからの通知を受け取ります。
アプリ構築にあたりこちらの記事をかなり参考にしました。
GCMを使用してAndroid-PHPでPUSH通知を実装する [Google Play services対応]
プッシュ通知に対応したアプリを作るだけならば、↑の記事の冒頭に書いてあるGCMSampleというサンプルアプリをダウンロードすれば簡単です。
2か所ほど間違いがあるので、それを修正すれば動きます。
[AndroidManifest.xml]39行目
android:name=".GCMBroadcastReceiver"
↓
android:name=".GcmBroadcastReceiver"
[MainActivity.java]111行目
if (regid.equals("")) {
↓
if (registrationId.equals("")) {
こちらのサンプルを参考に、自分なりに作り直しました。
それが下記のアプリです。
【GCM_test_application2】
「2」になっているのに特に意味はありません。私が2通りのパターンで試して、うまくいったのが2の方だっただけです。
まずはアプリのMainActivityを作っていきます。
画面はこの一画面だけで完結するようにしました。
AndroidManifest.xmlに、パーミッションを記述してください。
[AndroidManifest.xml]
<!-- プッシュ通知を受け取った際にバイブレーション機能を使うためのパーミッション --> <uses-permission android:name="android.permission.VIBRATE" /> <!-- GCMメッセージ受信のためのパーミッション --> <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" /> <permission android:name="○○○(パッケージ名).permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="○○○(パッケージ名).permission.C2D_MESSAGE" />
同じくAndroidManifest.xmlに、プッシュ通知を受け取るためのレシーバーを登録してください。MainActivityはアプリ生成時に自動で記述されているので省いています。
[AndroidManifest.xml]
<!-- GCMを受け取るレシーバー --> <receiver android:name="○○○(パッケージ名).GcmBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="○○○(パッケージ名)" /> </intent-filter> </receiver>
次に、MainActivityのレイアウトxmlを作ります。
[activity_main.xml]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:orientation="vertical" > <LinearLayout android:gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/btn_regist" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登録" android:onClick="btREGIST" /> <Button android:id="@+id/btn_unregist" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="削除" android:onClick="btDELETE"/> </LinearLayout> <LinearLayout android:gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登録ID:" /> <TextView android:id="@+id/text_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=""/> </LinearLayout> <LinearLayout android:gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="受け取ったメッセージ:" /> <TextView android:id="@+id/text_mess" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=""/> </LinearLayout> </LinearLayout>
そして、いよいよMainActivityの中身です。
[MainActivity.java]
public class MainActivity extends Activity { private Context context; private String regid; private TextView tv_id; private TextView tv_mess; // 通信用ダイアログ private ProgressDialog mProgress; // 保存していた値を取得 SharedPreferences prefs; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context = MainActivity.this; // 保存するプリファレンス prefs = getSharedPreferences(MainActivity.class.getSimpleName(), Context.MODE_PRIVATE); tv_id = (TextView) findViewById(R.id.text_id); tv_mess = (TextView) findViewById(R.id.text_mess); // レジストレーションIDの取得 regid = getRegistrationId(); tv_id.setText(regid); // もし通知メッセージからアプリを起動したらここでGcmBroadcastReceiverからの値を受け取る Intent intent = getIntent(); String mess = intent.getStringExtra("MESS"); tv_mess.setText(mess); } public void btREGIST(View v) { // すでにレジストレーションIDを取得ずみなら if (!regid.equals("")) { // 処理を中断 return; } // デバイスにPlayサービスAPKが入っているか検証する if (!checkPlayServices()) { showToast("Google Play開発者サービスをインストールしてください"); Intent intent = new Intent( Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.google.android.gms&hl=ja")); startActivity(intent); // 処理を中段 return; } // レジストレーションIDを取得 mProgress = null; Handler mHandler = new Handler(); mProgress = new ProgressDialog(context); mProgress.setMessage(RegisterTask.message); mProgress.show(); RegisterTask t = new RegisterTask(context, mHandler, mProgress, regid, prefs, tv_id); t.start(); } public void btDELETE(View v) { } //PlayサービスのAPKチェック private boolean checkPlayServices() { int resultCode = GooglePlayServicesUtil .isGooglePlayServicesAvailable(this); if (resultCode != ConnectionResult.SUCCESS) { return false; } return true; } //レジストレーションIDの取得 private String getRegistrationId() { // ID String registrationId = prefs.getString(Const.PROPERTY_REG_ID, ""); // アプリのバージョン int registeredVersion = prefs.getInt(Const.PROPERTY_APP_VERSION, 0); // 今のバージョン int currentVersion = 0; try { PackageInfo packageInfo = context.getPackageManager() .getPackageInfo(context.getPackageName(), 0); currentVersion = packageInfo.versionCode; } catch (NameNotFoundException e) { } // アプリケーションがバージョンアップされていたときは再生成 if (registeredVersion != currentVersion) { registrationId = ""; } // IDを生成しなくてはいけない場合は空で返却 return registrationId; } private void showToast(String text) { Toast.makeText(this, text, Toast.LENGTH_LONG).show(); } }
想定するアプリの動作の流れは、
・「登録」ボタンを押して、GoogleCloudMessagingインスタンスでレジストレーションIDを取得する
・取得したレジストレーションIDをサーバーにPOSTして、サーバーの方でDBに保存する
・送られてきた通知をレシーバーで受け取って、MainActivity画面に反映させる
といったものです。
次回の記事では、POST処理とレシーバー処理について書きます。