Có ai có thể giúp tôi làm ví dụ làm việc để nhận tin nhắn từ gcm bằng Google Cloud Messaging dành cho Android hay không. Tôi đã thử cả hai cách (thư viện trợ giúp và lớp GoogleCloudMessaging) và không có gì có vẻ hiệu quả. Tôi đang sử dụng một kịch bản PHP cho thấy như sau:Không thể nhận tin nhắn bằng Google Cloud Messaging dành cho Android (không phải là thư viện trợ giúp)
Multicast ID: 5.2108110103215E + 18 Tổng số thông điệp xử lý thành công: 1 Tổng số thông điệp với các lỗi xử lý: 0 ID Canonical: 0
Vì vậy, dường như everithing là OK. Tôi có thể đăng ký thiết bị theo cả hai cách, sử dụng thư viện trợ giúp (gcm.jar) và sử dụng lớp GoogleCloudMessaging. Vấn đề là không có cách nào thông điệp tôi gửi qua PHP đến, hoặc ít nhất tôi không biết cách xử lý nó một cách chính xác. Dưới đây là các điều khoản và người nhận từ biểu hiện của tôi:
<permission android:name="com.example.gcm.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<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="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.READ_OWNER_DATA" />
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.example.gcm" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
Cuối cùng đây là lớp dịch vụ
public class GCMIntentService extends GCMBaseIntentService {
private static final String PROJECT_ID = "49XXXXXXXX6";
private static final String TAG = "GCM Intent Service";
public GCMIntentService()
{
super(PROJECT_ID);
Log.d(TAG, "GCMIntentService init");
}
@Override
protected void onError(Context ctx, String sError) {
Log.d(TAG, "Error: " + sError);
}
@Override
protected void onMessage(Context ctx, Intent intent) {
Log.d(TAG, "Message Received");
String message = intent.getStringExtra("message");
sendGCMIntent(ctx, message);
}
private void sendGCMIntent(Context ctx, String message) {
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("GCM_RECEIVED_ACTION");
broadcastIntent.putExtra("gcm", message);
ctx.sendBroadcast(broadcastIntent);
}
@Override
protected void onRegistered(Context ctx, String regId) {
Log.d(TAG, regId);
// Notify main UI to update registration status
Intent registrationIntent = new Intent();
registrationIntent.setAction("registered");
registrationIntent.putExtra("regId", regId);
sendBroadcast(registrationIntent);
}
@Override
protected void onUnregistered(Context ctx, String regId) {
//...
}
}
đây là mã khi sử dụng lớp GoogleCloudMessaging (Tôi đã thay đổi biểu hiện sử dụng máy thu tùy chỉnh):
public class GCMBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "GCM Receiver";
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
private Context ctx;
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Message received");
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
ctx = context;
String messageType = gcm.getMessageType(intent);
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification("Send error: " + intent.getExtras().toString());
}
else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
.equals(messageType)) {
sendNotification("Deleted messages on server: "
+ intent.getExtras().toString());
}
else {
sendNotification("Received: " + intent.getExtras().toString());
}
setResultCode(Activity.RESULT_OK);
}
// Put the GCM message into a notification and post it.
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
new Intent(ctx, MainActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
ctx).setSmallIcon(R.drawable.ic_launcher_temp)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
Cái này là everythin g có vẻ là ok, nhưng tin nhắn không bao giờ đến. Bất kỳ ý tưởng ?? Cảm ơn trước.
hướng dẫn rất hay tại đây http://tech-papers.org/google-cloud-messaging-gcm-for-android-and-push-notifications-2/ –