-1
搜索了2天后,應用在stackoverflow上找到的解決方案無法修復我的應用程序中的問題。我沒有在應用中收到推送通知。我是從WordPress的使用WP GCM正從GCM服務器響應發送消息:沒有得到推送通知
{"multicast_id":8254312412899412709,"success":2,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1439464061399302%7e2c8c8ef9fd7ecd"},{"message_id":"0:1439464061399862%50b5570df9fd7ecd"}]}
在logcat中它顯示
08-13 15:36:50.752: V/GCMBroadcastReceiver(11517): onReceive: com.google.android.c2dm.intent.RECEIVE
08-13 15:36:50.752: V/GCMBroadcastReceiver(11517): GCM IntentService class: com.itcombine.saidlist.GCMIntentService
08-13 15:36:50.762: V/GCMBaseIntentService(11517): Acquiring wakelock
下面是我的代碼:
GcmBroadcastReceiver: -
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
private static final String TAG = "GcmBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
Log.d(TAG, "Got message");
setResultCode(Activity.RESULT_OK);
}
}
GcmIntentService:
public class GcmIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
String TAG = "Px GCM IntentService";
String newms;
public GcmIntentService() {
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
Log.d(TAG, "Got message: " + messageType + ", gcm=" + gcm.toString());
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());
// If it's a regular GCM message, do some work.
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
// Post notification of received message.
newms = extras.getString("message").toString();
sendNotification("Received: " + newms);;
Log.i(TAG, "Received: " + extras.toString() + ", message=" + newms);
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
Log.d(TAG, "Done handling message");
}
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("alert", "1");
intent.putExtra("msg",newms);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, 0);
Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("GCM Notification")
.setSound(uri)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
的AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- GCM connects to Internet Services. -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- Creates a custom permission so only this app can receive its messages. -->
<permission
android:name="com.itcombine.saidlist.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.itcombine.saidlist.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- Network State Permissions to detect Internet status -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Permission to vibrate -->
<uses-permission android:name="android.permission.VIBRATE" />
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.itcombine.saidlist" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
我發現了同樣的問題在於是一些其他的問題,但他們所給出的解決方案是不是爲我工作。正如他們已經提供的文件應該在同一個包中,但我只有一個包。請幫忙。提前致謝。
感謝它的工作原理 –