GCM push notificatin无效
1。权限提供如
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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" />
<permission
android:name="com.example.gcm.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" />
2。接收方和服务在清单中注册 像
<receiver
android:name=".gcm.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.tes.fitcompanyuser" />
</intent-filter>
</receiver>
<service android:name="com.driver.pickji.gcm.GCMNotificationIntentService" />
3。接收者代码是
@Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(),
GCMNotificationIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
4。服务代码是:
public class GCMNotificationIntentService extends IntentService {
Bundle extras;
public static int notifyID;
NotificationCompat.Builder builder;
JSONArray notificationdataArr;
JSONObject notificationObData;
private Preference mPrefs;
public GCMNotificationIntentService() {
super("GcmIntentService");
mPrefs = new Preference(ApplicationDriverPickJi.BASE_CONTEXT);
}
@Override
protected void onHandleIntent(Intent intent) {
extras = intent.getExtras();
notifyID = (int) Math.random();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
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());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
if (extras.containsKey("pickUpRequests")) {
sendNotification("Message Received from Google GCM Server:\n\n" + extras.getString("pickUpRequests"));
}
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg) {
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.gun_alert);
mediaPlayer.start();
Intent resultIntent = new Intent(this, ActivityLogin.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, Parcelable.CONTENTS_FILE_DESCRIPTOR);
Notification n;
try {
n = new Notification.Builder(ApplicationDriverPickJi.BASE_CONTEXT)
.setContentTitle("PickJi")
.setContentText("You have a Notification")
.setSmallIcon(R.drawable.ic_pickji)
.setContentIntent(resultPendingIntent)
.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.gun_alert))
.setAutoCancel(true)
.build();
NotificationManager notificationManager = (NotificationManager) ApplicationDriverPickJi.BASE_CONTEXT
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, n);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}