
以前は、Androidはプッシュ通知をデバイスに配信するためのサービスとしてC2DM (クラウドからデバイスへのメッセージング)を使用していました。 しかし、6月26日にGoogleによって公式にキャンセルされました。 その代わりに、新しいGCM (Google Cloud Messaging)が登場しました。
同様の名前。 同じ役割。 違いは何ですか?
- GCMを使用するには、Google APIコンソールでSimple APIキーを取得する必要があります。
- GCMの場合、送信者IDを取得する必要があります。 これは、C2DMの電子メールに相当します。 Google APIコンソールから、またはURLから再度取得できます。
code.google.com/apis/console/#project:{SENDER_ID}
- GCM通知は、プレーンテキストとともにJSON形式です。
- GCMは、一度に複数のデバイスに通知を送信できます。
- 1つの登録識別子を持つ1つのデバイスが、複数のサーバーから一度に通知を受信できるようになりました。
- 現在、通知の有効期間は最大4週間です。 GCMは有効期限までそれらを保存します。
- ペイロードを使用して最大4Kの通知を送信できるようになりました。 これは、さまざまなチャットのリアルタイムにとって非常に有益です。 ただし、この方法はデバイスのバッテリーをより強力に消費します。
- 1つのデバイスの登録が繰り返されるのを避けるために、デバイス識別子をサーバーに転送する必要はなくなりました。 正規の登録識別子は、デバイスの最後の登録からGCMによって決定されます。 サーバーが古い識別子を使用して通知を送信すると、GCMは正規の(最後の)識別子を返します。これは、古い識別子に置き換える必要があります。
GCMを構成する
Androidマニフェストから始めましょう
まず、許可を登録する必要があります。
<uses-permission android:name="android.permission.WAKE_LOCK"/> <permission android:name="{package}.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="{package}.permission.C2D_MESSAGE" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
次に、受信者とサービス:
<receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> <category android:name="{package}" /> </intent-filter> </receiver> <service android:name=".GCMIntentService" />
* {package}をパッケージに置き換えます(com.habrahabr.gcmがあります)
次に、パッケージのルートディレクトリで、 GCMBaseIntentServiceから継承したGCMIntentServiceクラスを作成します。
package {package}; import android.app.Activity; import android.app.IntentService; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; import com.google.android.gcm.GCMBaseIntentService; public class GCMIntentService extends GCMBaseIntentService { private static final String TAG = "GCMIntentService"; public GCMIntentService() { super(GCMConfig.SENDER_ID); } @Override protected void onRegistered(Context context, String registrationId) { Log.i(TAG, "Device registered"); // registrationId , } @Override protected void onUnregistered(Context context, String registrationId) { Log.i(TAG, "Device unregistered"); } @Override protected void onMessage(Context context, Intent intent) { Log.i(TAG, "Received new message"); } @Override protected void onDeletedMessages(Context context, int total) { Log.i(TAG, "Received deleted messages notification"); } @Override public void onError(Context context, String errorId) { Log.i(TAG, "Received error: " + errorId); } @Override protected boolean onRecoverableError(Context context, String errorId) { Log.i(TAG, "Received recoverable error: " + errorId); return super.onRecoverableError(context, errorId); } }
そしてその後、主なアクティビティで規定します:
// GCMRegistrar.checkDevice(this); GCMRegistrar.checkManifest(this); // final String regId = GCMRegistrar.getRegistrationId(this); if (regId.equals("")) { // , GCMRegistrar.register(this, GCMConfig.SENDER_ID); } else { Log.v("GCM", "Already registered: " + regId); }
これで、サーバーから通知自体を送信することを除いて、すべての準備が整いましたが、今はこれで十分だと思います。
結果のアプリケーションのソースコード
GCMアーキテクチャの概要
GCMの高度なトピック