无法解析符号“Google云端通讯” GCM(Cannot resolve symbol '

2019-09-04 04:52发布

我试图让GCM在我的应用程序工作(通知用户,当我们的时间变化,或者当我们进行的任何电视节目预告),但我不断收到错误Cannot resolve symbol 'GoogleCloudMessaging'试图使用谷歌云端通讯API时。

我使用的是最新发布的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();
    }
}

Answer 1:

下面的部分指导你完成设置GCM执行的过程。 在开始之前,请务必设置了谷歌Play服务SDK。 你需要这个SDK使用Google云端通讯方法 。 严格地说,你绝对需要该API的唯一事情是上游(设备到云)消息,但它也提供了建议简化注册API。

你设置了谷歌Play服务SDK ?

你必须 :

  1. 安装谷歌Play服务SDK
  2. 引用<android-sdk>/extras/google/google_play_services/libproject/google-play-services_lib/在你的Android项目库项目。

要安装开发谷歌游戏服务SDK:

  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. 


Answer 2:

如果您使用的是Android工作室:

1)下载谷歌播放SDK(使用SDK Manager):

2)不要忘记点击“同步工程与摇篮文件”按钮

这奏效了我。



Answer 3:

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

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

然后运行build

这为我工作。



Answer 4:

请确保您有关于的build.gradle添加依赖>同步>构建 - 清理项目。

为我工作:)



Answer 5:

尝试清洁您的项目。 为我工作。



Answer 6:

您可能正在使用旧的教程,但GCMRegistrar是弃用的API类。

请使用Google云端通讯 API来代替。

检查此为完整的教程使用GCM推送通知



文章来源: Cannot resolve symbol 'GoogleCloudMessaging' GCM