-1

Après une recherche de 2 jours appliquant des solutions trouvées sur stackoverflow pas en mesure de résoudre le problème dans mon application. Je ne reçois pas de notifications push dans l'application. J'envoie un message de wordpress en utilisant GCM wp obtenir réponse du serveur GCM:ne pas recevoir de notification push

{"multicast_id":8254312412899412709,"success":2,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1439464061399302%7e2c8c8ef9fd7ecd"},{"message_id":"0:1439464061399862%50b5570df9fd7ecd"}]} 

en logcat il montre

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 

Ci-dessous mon code:

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

J'ai trouvé même problème dans d'autres questions sur SO mais la solution qu'ils ont donné ne fonctionne pas pour moi. Comme ils ont donné que les fichiers devraient être dans le même paquet mais j'ai seulement un paquet. S'il vous plaît aider. Merci d'avance.

Répondre

1

Google recommande de ne plus utiliser WakefulBroadcastReceiver mais GCMReceiver et GcmListenerService sur Android. Cette modification peut résoudre votre problème

source here

+0

grâce ça marche –