有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法解析符号“GoogleCloudMessaging”GCM

我试图让GCM在我的应用程序中工作(当我们的时间改变时,或者当我们有任何促销活动时,通知用户),但是当我试图使用谷歌云消息API时,我一直收到错误Cannot resolve symbol 'GoogleCloudMessaging'

我正在使用最新发布的Android studio IDE来编写此代码

这是我的GcmBroadcastReciever。java代码:

import 安卓.R;
import 安卓.app.Activity;
import 安卓.app.NotificationManager;
import 安卓.app.PendingIntent;
import 安卓.content.BroadcastReceiver;
import 安卓.content.Context;
import 安卓.content.Intent;
import 安卓.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) 个答案

  1. # 1 楼答案

    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.

    你知道吗

    你必须:

    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.
    
  2. # 2 楼答案

    尝试清理你的项目。为我工作

  3. # 3 楼答案

    确保在生成时添加了依赖项。格拉德尔>;同步>;构建-清理项目

    为我工作:)

  4. # 4 楼答案

    如果您使用的是Android Studio:

    1)下载了Google Play SDK(使用SDK管理器):

    SDK Manager

    2)别忘了点击“与Gradle文件同步项目”按钮

    Synch Project with Gradle Files

    这对我来说是个好办法

  5. # 6 楼答案

    如果您在Android Studio中,请确保在build.gradle中您有:

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

    然后运行build

    这对我很管用