Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot resolve symbol 'GoogleCloudMessaging' GCM

I am trying to get GCM working in my app (to notify users when our hours change, or when we have any promos going on), but I keep getting the error Cannot resolve symbol 'GoogleCloudMessaging' when trying to use the Google Cloud Messaging API.

I am using the newly released Android studio IDE to code this.

Here is my GcmBroadcastReciever.java code :

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();
    }
}
like image 431
DillonRegi Avatar asked May 18 '13 00:05

DillonRegi


5 Answers

The sections below guide you through the process of setting up a GCM implementation. Before you start, make sure to set up the Google Play Services SDK. You need this SDK to use the GoogleCloudMessaging methods. Strictly speaking, the only thing you absolutely need this API for is upstream (device-to-cloud) messaging, but it also offers a streamlined registration API that is recommended.

Did you set up the Google Play Services SDK?

You have to :

  1. Install the Google Play services SDK
  2. Reference the <android-sdk>/extras/google/google_play_services/libproject/google-play-services_lib/ library project in your Android project.

To install the Google Play services SDK for development:

 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.
like image 112
Eran Avatar answered Nov 13 '22 10:11

Eran


If you are using Android Studio:

1) Downloaded the Google Play SDK (using the SDK Manager):

SDK Manager

2) Don't forget to click on the "Synch Project with Gradle Files" button

Synch Project with Gradle Files

That did the trick for me.

like image 31
unify Avatar answered Nov 13 '22 10:11

unify


If you are in Android Studio, make sure that in your build.gradle you have:

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

and then run build.

That worked for me.

like image 6
hlopezvg Avatar answered Nov 13 '22 10:11

hlopezvg


Make sure you have add dependencies on build.gradle > Sync > Build - Clean Project.

Worked for me :)

like image 4
rischan Avatar answered Nov 13 '22 09:11

rischan


Try cleaning your project. Worked for me.

like image 3
Wise Shepherd Avatar answered Nov 13 '22 08:11

Wise Shepherd