您需要創建自己的Receiver和Service並像這樣使用它。
接收機:
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
public class GCMMessageReciever extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(),
GCMIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
服務:
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;
import com.appdupe.uberforxserviceprovider.R;
import com.google.android.gcm.GCMBaseIntentService;
import com.uber.driver.MyMainFragmentActivity;
import com.uber.driver.constants.Constants;
import com.uber.driver.gcm.ServerUtilities;
import com.uber.driver.util.Utils;
public class GCMIntentService extends GCMBaseIntentService {
private NotificationManager mNotificationManager;
public GCMIntentService() {
super(Constants.SENDER_ID);
}
@Override
protected void onRegistered(Context context, String registrationId) {
Intent i = new Intent(Constants.PUSHNOTIFICATION);
i.putExtra(Constants.DB_PHONE_GCM_ID, registrationId);
LocalBroadcastManager.getInstance(context).sendBroadcast(i);
ServerUtilities.register(context, "name", "email", registrationId);
}
@Override
protected void onMessage(Context context, Intent intent) {
String jsonString = intent.getExtras().getString("message");
try {
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
Editor editor = preferences.edit();
JSONObject jsonObject = new JSONObject(jsonString);
if (jsonObject.getString("id").equals("1")) {
editor.putFloat(Constants.USER_LATTITUDE,
Float.parseFloat(jsonObject.getString("lattitude")));
editor.putFloat(Constants.USER_LOGITUDE,
Float.parseFloat(jsonObject.getString("logitude")));
editor.putString(Constants.USER_RANDOM_ID,
jsonObject.getString("random_id"));
editor.putBoolean(Constants.IS_USER_SET, true);
editor.putString(Constants.PHONE_CLIENT,
jsonObject.getString("client_contact"));
editor.putString(Constants.PHONE_OPEARTOR,
jsonObject.getString("operator_contact"));
// editor.putBoolean(Constants.IS_USER_SET, true);
editor.putString(Constants.CLIENT_NAME,
jsonObject.getString("client_name"));
sendNotification(jsonString);
} else {
editor.putBoolean(Constants.DB_IS_JOB_DONE, true);
editor.putInt(Constants.FRAGMENT_POSITION,
Constants.FRAGMENT_MAP);
editor.putString(Constants.USER_RANDOM_ID, "");
}
editor.commit();
} catch (JSONException e) {
e.printStackTrace();
}
Utils.log("Received message.. " + jsonString);
Intent i = new Intent(Constants.PUSHNOTIFICATION);
i.putExtra("json", jsonString);
LocalBroadcastManager.getInstance(context).sendBroadcast(i);
}
@Override
protected void onDeletedMessages(Context context, int total) {
}
@Override
public void onError(Context context, String errorId) {
}
@Override
protected boolean onRecoverableError(Context context, String errorId) {
return super.onRecoverableError(context, errorId);
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
}
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MyMainFragmentActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(
getResources().getString(R.string.text_uber_driver))
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText(getResources().getString(
R.string.text_job_assigned)))
.setDefaults(
Notification.DEFAULT_SOUND
| Notification.DEFAULT_VIBRATE);
mBuilder.setContentIntent(contentIntent);
mBuilder.setAutoCancel(true);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager
.notify(Constants.NOTIFICATION_ID, mBuilder.build());
}
}
Android清單:
<application>..
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<!-- GCM -->
<receiver
android:name=".GCMMessageReciever"
android:exported="true"
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" />
<action android:name="android.net.conn." />
<category android:name="<my-pakage-name>" />
</intent-filter>
</receiver>
<service
android:name=".GCMIntentService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name=".notifications.InstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<service
android:name=".notifications.RegistrationIntentService"
android:exported="false"></service>
</application>
你在日誌中看到什麼? –
與GCM無關.. – Mohanish
您是否在註冊時添加了正確的發件人ID? –