30

Sto cercando di ottenere GCM lavorando nella mia app (per avvisare gli utenti quando cambiano i nostri orari, o quando abbiamo qualche promozione in corso), ma continuo a ricevere l'errore Cannot resolve symbol 'GoogleCloudMessaging' quando provo ad usare l'API di Google Cloud Messaging.Impossibile risolvere il simbolo 'GoogleCloudMessaging' GCM

Sto usando l'IDE Android Studio appena rilasciato per codificarlo.

Ecco il mio codice 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(); 
    } 
} 
+1

Potrebbe trovare la soluzione? Sto affrontando lo stesso problema. – Geek

+0

Guarda la risposta. L'importazione era la soluzione, quindi segui i passaggi di Eran – dillonr

risposta

35

Le sezioni che seguono guiderà attraverso il processo di creazione di un GCM implementazione. Prima di iniziare, assicurati di configurare l'SDK dei servizi di Google Play . È necessario questo SDK per utilizzare i metodi di GoogleCloudMessaging. A rigor di termini, l'unica cosa che è assolutamente necessaria per questa API è la messaggistica upstream (device-to-cloud), ma offre anche un'API di registrazione semplificata consigliata.

Hai set up the Google Play Services SDK?

Dovete:

  1. installare Google Play Services SDK
  2. di riferimento il progetto <android-sdk>/extras/google/google_play_services/libproject/google-play-services_lib/ libreria nel progetto Android.

Per installare il Google Play Services SDK per lo sviluppo:

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. 
+4

che ho installato, ma non importato nel mio progetto come dipendenza. importando risolto i miei problemi, evviva – dillonr

+0

Non ero sicuro di come fare # 2, ma poi ho letto la risposta di unify e ho provato il "sync gradle" dopo aver fatto il punto 1 qui. Quella combinazione ha funzionato per me. – eselk

+0

Sì, aggiungilo come dipendenza e funzionerà. –

10

Se si utilizza Android Studio:

1) hanno scaricato Google Play SDK (utilizzando l'SDK manager):

SDK Manager

2) Non dimenticare di cliccare sul "Progetto Synch con Gradle file" pulsante

Synch Project with Gradle Files

che ha fatto il trucco per me.

+0

La sincronizzazione ha risolto il mio problema, grazie! – Sloy

6

Se siete in Android Studio, assicurarsi che nel vostro build.gradle avete:

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

e quindi eseguire build.

Questo ha funzionato per me.

3

Provare a pulire il vostro progetto. Ha funzionato per me

4

Assicurarsi di aggiungere dipendenze su build.gradle> Sincronizza> Crea - Pulisci progetto.

Ha funzionato per me :)

+0

Grazie, @rischan funziona .. –

Problemi correlati