私はこのトピックについてサーフィンをしてきましたが、この新しい GCM にはまだスタックしています。
このリンクに従ってGCM Pushを使用した私の最後のプロジェクト
ht**://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/
しかし現在、最後の GCM (C2DM) は廃止されているため、GCMRegistrar はもう使用していません。
ここで、この h**p://developer.android.com/google/gcm/c2dm.html を見てください。「Android Cloud to Device Messaging (C2DM) is deprecated. C2DM サービスは引き続き短期的には維持されますが、C2DM は新しいユーザーを受け入れず、新しいクォータを付与しません」
私は概念を学ぶためにこのリンクを使用します
そして、私が使用する例: https://code.google.com/p/gcm/source/browse/#git
必要なすべての手順を実行したと思います
プロジェクトを Google Play Service lib に参照しました
また、送信者 ID を Google プロジェクト番号に変更しました
私もGCMからRegIDを受け取りました
しかし問題は、送信ボタンをクリックするたびに
それは私に何も見せない
Googleドキュメントの例から、デバイスにエコーバックする必要があると言われていますが、決してそうしません
ドキュメントでチェックしたところ、gcmbroadcastreceiver はデバイスにプッシュされた GCM メッセージを受信するという概念がありました。
そのブロードキャスターの「OnReceive」にログインしようとしましたが、何も表示されません
また、このドキュメントhttp://developer.android.com/google/gcm/http.htmlを使用して HTTP GCM サーバーを実装しようとしました
私はこのような応答を取得します}
しかし、私のログには何も表示されません
ここに私の放送受信機があります
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.v("HAHA BANGET","masuk ke broadcastReceiver");
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
ここに私のインテントサービスがあります
public class GcmIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public GcmIntentService() {
super("7134XXXXX");
}
public static final String TAG = "HAHA";
@Override
protected void onHandleIntent(Intent intent) {
Log.v(TAG,"MASUK INTENT NIH");
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) { // has effect of unparcelling Bundle
/*
* Filter messages based on message type. Since it is likely that GCM will be
* extended in the future with new message types, just ignore any message types you're
* not interested in, or that you don't recognize.
*/
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
sendNotification("Deleted messages on server: " + extras.toString());
// If it's a regular GCM message, do some work.
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
// This loop represents the service doing some work.
for (int i = 0; i < 5; i++) {
Log.i(TAG, "Working... " + (i + 1)
+ "/5 @ " + SystemClock.elapsedRealtime());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
}
Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
// Post notification of received message.
sendNotification("Received: " + extras.toString());
Log.i(TAG, "Received: " + extras.toString());
}
}
// Release the wake lock provided by the WakefulBroadcastReceiver.
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
// Put the message into a notification and post it.
// This is just one simple example of what you might choose to do with
// a GCM message.
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, PushBaruLagi.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.common_signin_btn_icon_dark)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
ここに私のマニフェストがあります
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<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.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<permission android:name="com.coba.pushgcmbaru.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.coba.pushgcmbaru.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver
android:name="com.coba.pushgcmbaru.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.coba.pushgcmbaru" />
</intent-filter>
</receiver>
<service android:name="com.coba.pushgcmbaru.GcmIntentService" />
<activity
android:name="com.coba.pushgcmbaru.PushBaruLagi"
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>
私は2.2 FROYOを使用しています
Google Play サービスを最新のものに更新しました
みんな、もし誰かが私を助けてくれるなら、どうもありがとう:)