我正在尝试使用GCM发送推送通知消息。
我能够成功注册我的设备,我正在生成的设备注册ID是APA91bEHUpBYhB6yT_WO3PMgZ7p1_3_2QFzPARSgtrwk43BNlSR8t-KKi0wfIo7vWEpELS-7wndJk4D3TC2mZXivuv5SgWCGoQSRZjesNcohmCJvsWdaTBHUiT_wx8pVFtj9rtK5IFgF5KS5a5LI-X8Gv8r3YuAhjA"
当我发送推送消息时,我从服务器获得的响应是
{"multicast_id":6991147966445820714,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1438192656514830%358381d5f9fd7ecd"}]}
这是GCMBroadcastReceiver的内容
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@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);
}
}
这是GCMNotification Intent Service的代码
public class GCMNotificationIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public GCMNotificationIntentService() {
super("GcmIntentService");
}
public static final String TAG = "GCMNotificationIntentService";
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
String message = extras.getString("message");
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)) {
for (int i = 0; i < 3; i++) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
}
sendNotification(message);
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg) {
// Log.d(TAG, "Preparing to send notification...: " + msg);
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
//Log.d(TAG, "Notification sent successfully.");
}
}
这是我的明白
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kmi_dev.fbloginsample"
android:sharedUserId="triggers.dummy.loader" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- For vibrating device -->
<uses-permission android:name="android.permission.VIBRATE" />
<!-- Creates a custom permission so only this app can receive its messages. -->
<permission
android:name="com.example.kmi_dev.fbloginsample.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.kmi_dev.fbloginsample.C2D_MESSAGE" />
<!-- For receiving GCM messages -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- For protecting GCM messages so that only your app can receive them -->
<receiver
android:name=".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.example.kmi_dev.fbloginsample" />
</intent-filter>
</receiver>
<!-- make sure to add google-play-services_lib from project properties->android->library -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
为什么我无法接收推送通知?我在做一些愚蠢的错误吗?请帮忙
P.S。我只发布了清单的一小部分,我认为这与该问题有关。