-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" /> 

我发现了同样的问题在于是一些其他的问题,但他们所给出的解决方案是不是为我工作。正如他们已经提供的文件应该在同一个包中,但我只有一个包。请帮忙。提前致谢。

回答

1

谷歌推荐不再使用WakefulBroadcastReceiver,而是Android上的GCMReceiver和GcmListenerService。此更改可能会解决你的问题

source here

+0

感谢它的工作原理 –