我必须实现推送通知的代码。当我从服务器发送详细信息时,它不会进入移动设备。这里onMessgae()方法在我从服务器推送细节时没有被调用。请告诉我这是什么问题。
这是我的代码
GCMIntentService.java
private static final String TAG = "srinu";
public static final String SENDER_ID = "834017698652";
public static final String DISPLAY_MESSAGE_ACTION = "com.hilo.goldbutton.Home";
public static final String EXTRA_MESSAGE = "message";
public static final String PUSH_TOKEN_SERVICE = "xxxxxxxxxxxxxxxxxxx";
public static final String READ_TOKEN_SERVICE = "xxxxxxxxxxxxxxxxxxx";
Context ctx;
AsyncTask<Void, Void, Void> mRegisterTask;
public GCMIntentService() {
super(GCMIntentService.SENDER_ID);
try {
Class.forName("android.os.AsyncTask");
} catch (ClassNotFoundException e) {
}
}
@Override
protected void onRegistered(Context context, final String registrationId) {
mRegisterTask = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
final String tmDevice, tmSerial, androidId;
tmDevice = "" + tm.getDeviceId();
tmSerial = "" + tm.getSimSerialNumber();
androidId = "" + android.provider.Settings.Secure.getString(getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());
String deviceId = deviceUuid.toString();
String responsePushToken=Helper.getSourceCode(PUSH_TOKEN_SERVICE+"token="+deviceId+"&email="+RegistrationActivity.mail);
Log.v("srinu", "-----respushtoken:"+responsePushToken);
return null;
}
@Override
protected void onPostExecute(Void result) {
mRegisterTask = null;
}
};
mRegisterTask.execute(null, null, null);
}
@Override
protected void onUnregistered(Context context, String registrationId) {
Log.v("srinu", "Device unregistered");
}
@Override
protected void onMessage(final Context context, final Intent intent) {
Log.v("srinu", "--------------------------------------------------" + intent.getExtras().getString("message"));
ctx = context;
String other_key = intent.getExtras().getString("message");
generateNotification(context, other_key);
handleMessage(intent,other_key);
}
@Override
protected void onDeletedMessages(Context context, int total) {
Log.v(TAG, "Received deleted messages notification");
}
@Override
public void onError(Context context, String errorId) {
Log.v(TAG, "Received error: " + errorId);
}
@Override
protected boolean onRecoverableError(Context context, String errorId) {
Log.v(TAG, "Received recoverable error: " + errorId);
return super.onRecoverableError(context, errorId);
}
@SuppressWarnings("deprecation")
private static void generateNotification(Context context, String message) {
Intent notificationIntent;
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
notificationIntent = new Intent(context, Notifications.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, title + " sent you a message",
message, intent);
notification.defaults = Notification.DEFAULT_ALL;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
private void handleMessage( Intent intent,final String message)
{
// final String message = intent.getExtras().getString("message");
Thread t = new Thread()
{
public void run()
{
Message myMessage = new Message();
Bundle resBundle = new Bundle();
resBundle.putString( "message", message );
myMessage.setData( resBundle );
handler.sendMessage( myMessage );
}
};
t.start();
}
private Handler handler = new Handler()
{
public void handleMessage( Message msg )
{
Toast.makeText( getBaseContext(), msg.getData().getString( "message" ), Toast.LENGTH_LONG ).show();
}
};
}
Home.java:
public class Home extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
try {
Class.forName("android.os.AsyncTask");
} catch (ClassNotFoundException e) {
}
gcm(this);
}
private void gcm(final Context context) {
GCMRegistrar.checkDevice(context);
GCMRegistrar.checkManifest(context);
registerReceiver(mHandleMessageReceiver, new IntentFilter(
GCMIntentService.DISPLAY_MESSAGE_ACTION));
if (Helper.RegistrationId.equals("empty")) {
GCMRegistrar.register(context, GCMIntentService.SENDER_ID);
} else {
if (GCMRegistrar.isRegisteredOnServer(context)) {
Log.v("srinu", "GCM Display Message : "
+ "Already registered");
}
}
}
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String newMessage = intent.getExtras().getString(
GCMIntentService.EXTRA_MESSAGE);
Log.d("Reva", "GCM Display Message : " + newMessage);
}
};
@Override
protected void onDestroy() {
super.onDestroy();
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ECLAIR_MR1
&& !isKindleFire()) {
if (mRegisterTask != null)
mRegisterTask.cancel(true);
unregisterReceiver(mHandleMessageReceiver);
GCMRegistrar.onDestroy(this);
}
}
public static boolean isKindleFire() {
return android.os.Build.MANUFACTURER.equals("Amazon")
&& (android.os.Build.MODEL.equals("Kindle Fire") || android.os.Build.MODEL
.startsWith("KF"));
}
}
manifest.xml
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<permission
android:name="com.hilo.goldbutton.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.hilo.goldbutton.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<permission
android:name="com.hilo.goldbutton.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.hilo.goldbutton.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_lanucher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Light.NoTitleBar" >
<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="com.hilo.goldbutton" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
<activity
android:name="com.hilo.goldbutton.Home"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>