web-dev-qa-db-fra.com

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 nous avons des promotions en cours), mais je continue à recevoir l'erreur Cannot resolve symbol 'GoogleCloudMessaging' lorsque vous essayez d'utiliser l'API Google Cloud Messaging.

J'utilise le nouveau Android studio IDE pour coder cela).

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 String 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;
        String messageType = gcm.getMessageType(intent); //cannot resolve method here
        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) { //error
            sendNotification("Send error: " + intent.getExtras().toString());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) { //error
            sendNotification("Deleted messages on server: " +
                    intent.getExtras().toString());
        } else {
            sendNotification("Received: " + intent.getExtras().toString());
        }
        setResultCode(Activity.RESULT_OK);
    }

    // Put the GCM message into a notification and post it.
    private void sendNotification(String 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();
    }
}
30
DillonRegi

Les sections ci-dessous vous guident tout au long du processus de configuration d'une implémentation GCM. Avant de commencer, assurez-vous de configurer le SDK des services Google Play. Vous avez besoin de ce SDK pour utiliser les méthodes GoogleCloudMessaging . À strictement parler, la seule chose pour laquelle vous avez absolument besoin de cette API est la messagerie en amont (appareil vers cloud), mais elle propose également une API d'enregistrement simplifiée qui est recommandée.

Avez-vous configuré le SDK des services Google Play ?

Vous devez :

  1. Installer le SDK des services Google Play
  2. Référencez le <Android-sdk>/extras/google/google_play_services/libproject/google-play-services_lib/ projet de bibliothèque dans votre Android.

Pour installer le SDK des 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
    <Android-sdk>/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.
35
Eran

Si vous utilisez Android Studio:

1) Téléchargé le SDK Google Play (à l'aide du SDK Manager):

SDK Manager

2) N'oubliez pas de cliquer sur le bouton "Projet de synchronisation avec les fichiers Gradle"

Synch Project with Gradle Files

Cela a fait l'affaire pour moi.

10
unify

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

dependencies {
    compile 'com.google.Android.gms:play-services:7.8.0'
}

puis exécutez build.

Cela a fonctionné pour moi.

6
hlopezvg

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

A travaillé pour moi :)

4
rischan

Essayez de nettoyer votre projet. A travaillé pour moi.

3
Wise Shepherd

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

Veuillez plutôt utiliser l'API GoogleCloudMessaging .

Vérifiez ceci pour un tutoriel complet Notification push en utilisant gcm

2
kundan roy