无法解析的符号“GoogleCloudMessaging”GCM [英] Cannot resolve symbol 'GoogleCloudMessaging' GCM

查看:382
本文介绍了无法解析的符号“GoogleCloudMessaging”GCM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的应用程序 GCM 工作(通知用户,当我们的时间的变化,或者当我们进行的任何电视节目预告),但我不断收到误差无法解析符号GoogleCloudMessaging尝试使用谷歌云消息传递API时。

我使用的是最新发布的Andr​​oid Studio IDE中以$ C C此$。

下面是我的GcmBroadcastReciever.java code:

 进口android.R;
进口android.app.Activity;
进口android.app.NotificationManager;
进口android.app.PendingIntent;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.widget.Toast;

公共类GcmBroadcastReceiver扩展的BroadcastReceiver
{
    static final的字符串变量=GCMDemo;
    公共静态最终诠释NOTIFICATION_ID = 1;
    私人NotificationManager mNotificationManager;
    上下文CTX;
    GoogleCloudMessaging GCM; //我这里得到的错误

    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        GoogleCloudMessaging GCM = GoogleCloudMessaging.getInstance(上下文); //错误
        CTX =背景;
        字符串为messageType = gcm.getMessageType(意向); //不能在这里解决方法
        如果(GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(为messageType)){//错误
            sendNotification的(发送错误:+ intent.getExtras()的toString());
        }否则,如果(GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(为messageType)){//错误
            sendNotification的(在服务器上删除的邮件:+
                    。intent.getExtras()的toString());
        } 其他 {
            sendNotification的(收到:+ intent.getExtras()的toString());
        }
        的setResult code(Activity.RESULT_OK);
    }

    //把GCM消息到一个通知,并张贴。
    私人无效sendNotification的(弦乐味精){
        mNotificationManager =(NotificationManager)
                ctx.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(CTX,0,
                新的意图(CTX,Activity.class),0);

        Toast.makeText(CTX,味精,Toast.LENGTH_SHORT).show();
    }
}
 

解决方案
  

通过建立一个GCM的过程的带领下,你的节   实现。在开始之前,请确保设置了谷歌播放   服务SDK。 您需要这个SDK使用GoogleCloudMessaging方法。严格来说,唯一你绝对需要这种   对于API是上游(设备到云)的消息,但它也提供了一个   该建议简化注册API。

您是否成立了谷歌播放服务SDK

您必须:

  1. 安装谷歌播放服务SDK
  2. 引用的<$c$c><android-sdk>/extras/google/google_play_services/libproject/google-play-services_lib/库项目在你的Andr​​oid项目。
  

要安装谷歌Play业务SDK的开发:

  1.启动SDK管理器。
      - 在Eclipse(与ADT),选择Window&GT; Android的SDK管理器。
      - 在Windows上,在Android的根双击该SDK的manager.exe文件
       SDK目录。
      - 在Mac或Linux上,打开一个终端并导航到工具/目录
       Android SDK中,然后执行Android SDK中。
 2.安装谷歌播放服务SDK。
    滚动到包列表的底部,展开附加,选择谷歌播放
    服务,并安装它。
    在谷歌Play业务SDK是在保存在你的Andr​​oid SDK环境
    &LT; Android的SDK&GT; /演员/谷歌/ google_play_services /。
 3.安装谷歌的API平台的兼容版本。
    如果你想测试在仿真器中的应用程序,展开目录
    安卓4.2.2(API 17)或更高版本,选择谷歌的API,以及
    安装。然后创建一个新的AVD与谷歌的API为平台的目标。
    注意:只有安卓4.2.2及以上的谷歌的API平台的版本
    包括谷歌Play业务。
 

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();
    }
}

解决方案

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.

这篇关于无法解析的符号“GoogleCloudMessaging”GCM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆