Impossible de résoudre le symbole GCM ‘GoogleCloudMessaging’

J’essaie de faire fonctionner GCM dans mon application (pour informer les utilisateurs lorsque nos heures changent ou lorsque des promotions sont en cours), mais je continue à recevoir l’erreur Cannot resolve symbol 'GoogleCloudMessaging' lorsque j’utilise Google API Messaging .

J’utilise le studio IDE Android récemment publié pour le coder.

Voici mon code GcmBroadcastReciever.java:

 import android.R; import android.app.Activity; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class GcmBroadcastReceiver extends BroadcastReceiver { static final Ssortingng TAG = "GCMDemo"; public static final int NOTIFICATION_ID = 1; private NotificationManager mNotificationManager; Context ctx; GoogleCloudMessaging gcm; // I get the error here @Override public void onReceive(Context context, Intent intent) { GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context); //error ctx = context; Ssortingng messageType = gcm.getMessageType(intent); //cannot resolve method here if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) { //error sendNotification("Send error: " + intent.getExtras().toSsortingng()); } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) { //error sendNotification("Deleted messages on server: " + intent.getExtras().toSsortingng()); } else { sendNotification("Received: " + intent.getExtras().toSsortingng()); } setResultCode(Activity.RESULT_OK); } // Put the GCM message into a notification and post it. private void sendNotification(Ssortingng msg) { mNotificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE); PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, new Intent(ctx, Activity.class), 0); Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show(); } } 

Les sections ci-dessous vous guident tout au long du processus de mise en place d’une implémentation GCM. Avant de commencer, assurez-vous de configurer le SDK Google Play Services. Vous avez besoin de ce SDK pour utiliser les méthodes GoogleCloudMessaging . À proprement parler, la seule chose dont vous avez absolument besoin de cette API est la messagerie en amont (appareil à cloud), mais elle offre également une API d’enregistrement simplifiée recommandée.

Avez-vous configuré le SDK Google Play Services ?

Vous devez :

  1. Installer le SDK des services Google Play
  2. Référencez le projet /extras/google/google_play_services/libproject/google-play-services_lib/ library dans votre projet Android.

Pour installer le SDK de services Google Play pour le développement:

  1. Launch the SDK Manager. - From Eclipse (with ADT), select Window > Android SDK Manager. - On Windows, double-click the SDK Manager.exe file at the root of the Android SDK directory. - On Mac or Linux, open a terminal and navigate to the tools/ directory in the Android SDK, then execute android sdk. 2. Install the Google Play services SDK. Scroll to the bottom of the package list, expand Extras, select Google Play services, and install it. The Google Play services SDK is saved in your Android SDK environment at /extras/google/google_play_services/. 3. Install a compatible version of the Google APIs platform. If you want to test your app on the emulator, expand the directory for Android 4.2.2 (API 17) or a higher version, select Google APIs, and install it. Then create a new AVD with Google APIs as the platform target. Note: Only Android 4.2.2 and higher versions of the Google APIs platform include Google Play services. 

Si vous utilisez Android Studio:

1) Téléchargé le SDK Google Play (en utilisant le SDK Manager):

Gestionnaire de SDK

2) N’oubliez pas de cliquer sur le bouton “Synchroniser le projet avec les fichiers Gradle”.

Projet de synchronisation avec les fichiers Gradle

Cela a fait le tour pour moi.

Si vous êtes dans Android Studio, assurez-vous que dans votre build.gradle vous avez:

 dependencies { comstack 'com.google.android.gms:play-services:7.8.0' } 

puis lancez build .

Cela a fonctionné pour moi.

Assurez-vous que vous avez ajouté des dépendances sur build.gradle> Sync> Build – Clean Project.

Travaillé pour moi 🙂

Essayez de nettoyer votre projet. Travaillé pour moi

Vous utilisez probablement un ancien tutoriel, mais GCMRegistrar est une classe d’API obsolète.

Veuillez utiliser l’API GoogleCloudMessaging à la place.

Cochez cette case pour obtenir une notification poussée du didacticiel à l’ aide de gcm