无法解析符号“GoogleCloudMessaging” GCM

30

我正在尝试在我的应用程序中使用GCM(当我们的营业时间发生变化或者有促销活动时通知用户),但是当我尝试使用Google Cloud Messaging API时,我一直收到错误信息Cannot resolve symbol 'GoogleCloudMessaging'

我正在使用新发布的Android Studio IDE编写此代码。

这是我的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();
    }
}
6个回答

35
以下内容将指导您完成GCM实现的设置过程。在开始之前,请确保设置了Google Play服务SDK。您需要此SDK才能使用GoogleCloudMessaging方法。严格来说,您绝对需要此API的唯一目的是上行(设备到云)消息,但它还提供了一个推荐的简化注册API。
您是否已经设置了Google Play服务SDK
您需要执行以下步骤:
  1. 安装Google Play服务SDK
  2. 在Android项目中引用<android-sdk>/extras/google/google_play_services/libproject/google-play-services_lib/库项目。

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.

4
我已经安装了它,但没有将其作为依赖项导入到我的项目中。 导入它解决了我的问题,谢谢。 - DillonRegi
我不确定如何完成第二步,但是在阅读了unify的答案后,我尝试了在这里完成第一步后进行“同步gradle”的操作。这个组合对我很有效。 - eselk

10
如果您使用的是Android Studio:
1)下载了Google Play SDK(使用SDK Manager): SDK Manager 2)别忘了点击“Synch Project with Gradle Files”按钮 Synch Project with Gradle Files 这对我起了很好的作用。

6

如果你在使用Android Studio,确保在你的build.gradle文件中有以下内容:

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

然后运行build命令。

这对我来说起作用了。


4
请确保在build.gradle中已添加依赖项,然后进行“同步”和“构建-清理项目”的操作。
这对我很有效 :)

3

尝试清理你的项目,这对我有用。


2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接