我开发了一个基本的聊天应用程序。我无法接收消息。谁可以帮我这个事??我尝试在Logcat中检查这个GCMBroadcastReceiver是否正常工作......在LogCat中没有这样的日志消息。
任何帮助都是值得的。
GCMBroadcastReceiver.java
package com.chaitukadivella.friendschat.client;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.text.TextUtils;
import android.util.Log;
import com.chaitukadivella.friendschat.Common;
import com.chaitukadivella.friendschat.DataProvider;
import com.chaitukadivella.friendschat.MainActivity;
import com.chaitukadivella.friendschat.R;
import com.google.android.gms.gcm.GoogleCloudMessaging;
public class GcmBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "GcmBroadcastReceiver";
private Context ctx;
@Override
public void onReceive(Context context, Intent intent) {
ctx = context;
Log.v(TAG,"Entered Broadcast Receiver");
PowerManager mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
WakeLock mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
mWakeLock.acquire();
try {
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
String messageType = gcm.getMessageType(intent);
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification("Send error", false);
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
sendNotification("Deleted messages on server", false);
} else {
String msg = intent.getStringExtra(DataProvider.COL_MSG);
String email = intent.getStringExtra(DataProvider.COL_FROM);
ContentValues values = new ContentValues(2);
values.put(DataProvider.COL_MSG, msg);
values.put(DataProvider.COL_FROM, email);
context.getContentResolver().insert(DataProvider.CONTENT_URI_MESSAGES, values);
if (Common.isNotify()) {
sendNotification("New message", true);
}
}
setResultCode(Activity.RESULT_OK);
} finally {
mWakeLock.release();
}
}
private void sendNotification(String text, boolean launchApp) {
Log.v(TAG,"Entered Send Notification");
NotificationManager mNotificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder mBuilder = new Notification.Builder(ctx)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(ctx.getString(R.string.app_name))
.setContentText(text);
if (!TextUtils.isEmpty(Common.getRingtone())) {
mBuilder.setSound(Uri.parse(Common.getRingtone()));
}
if (launchApp) {
Intent intent = new Intent(ctx, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pi = PendingIntent.getActivity(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pi);
}
//mNotificationManager.notify(1, mBuilder.getNotification());
mNotificationManager.notify(1, mBuilder.build());
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.chaitukadivella.friendschat"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="18" />
<permission
android:name="com.chaitukadivella.friendschat.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.chaitukadivella.friendschat.permission.C2D_MESSAGE" />
<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.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.chaitukadivella.friendschat.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>
<activity
android:name="com.chaitukadivella.friendschat.ChatActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.CHATACTIVITY" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.chaitukadivella.friendschat.SettingsActivity"
android:label="@string/title_activity_settings" >
<intent-filter>
<action android:name="android.intent.action.SETTINGSACTIVITY" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.chaitukadivella.friendschat.EditContactDialog"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.EDITCONTACTDIALOG" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.chaitukadivella.friendschat.client.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="com.chaitukadivella.friendschat" />
</intent-filter>
</receiver>
<provider
android:name="com.chaitukadivella.friendschat.DataProvider"
android:authorities="com.chaitukadivella.friendschat.provider"
android:exported="false" >
</provider>
</application>
</manifest>