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

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

问题描述

我正在尝试让 GCM 在我的应用程序中运行(在我们的营业时间更改或有任何促销活动时通知用户),但我不断收到错误 无法解决尝试使用 Google Cloud Messaging API 时出现符号GoogleCloudMessaging".

我正在使用新发布的 Android Studio IDE 对此进行编码.

这是我的 GcmBroadcastReciever.java 代码:

import 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{静态最终字符串标记 = "GCMDemo";public static final int NOTIFICATION_ID = 1;私有 NotificationManager mNotificationManager;上下文 ctx;GoogleCloudMessaging gcm;//我在这里得到错误@覆盖public void onReceive(上下文上下文,意图意图){GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);//错误ctx = 上下文;String messageType = gcm.getMessageType(intent);//这里无法解析方法if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {//错误sendNotification("发送错误:" + intent.getExtras().toString());} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {//错误sendNotification("服务器上已删除的消息:" +intent.getExtras().toString());} 别的 {sendNotification("收到:" + intent.getExtras().toString());}setResultCode(Activity.RESULT_OK);}//将 GCM 消息放入通知并发布.私人无效发送通知(字符串味精){mNotificationManager = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,新意图(ctx,Activity.class),0);Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();}}

解决方案

以下部分将指导您完成设置 GCM 的过程执行.在开始之前,请确保设置 Google Play服务 SDK.您需要此 SDK 才能使用 GoogleCloudMessaging 方法.严格来说,你唯一绝对需要的就是这个API for 是上游(设备到云)消息传递,但它也提供了一个推荐的简化注册 API.

您是否设置了 Google Play 服务 SDK?

你必须:

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

<块引用>

要安装用于开发的 Google Play 服务 SDK:

 1. 启动 SDK 管理器.- 从 Eclipse(带有 ADT)中,选择 Window >Android SDK 管理器.- 在 Windows 上,双击 Android 根目录下的 SDK Manager.exe 文件SDK目录.- 在 Mac 或 Linux 上,打开终端并导航到工具/目录Android SDK,然后执行android sdk.2. 安装 Google Play 服务 SDK.滚动到包列表底部,展开 Extras,选择 Google Play服务,并安装它.Google Play 服务 SDK 保存在您的 Android SDK 环境中<android-sdk>/extras/google/google_play_services/.3. 安装兼容版本的 Google APIs 平台.如果您想在模拟器上测试您的应用程序,请展开以下目录Android 4.2.2 (API 17) 或更高版本,选择 Google APIs,然后安装它.然后使用 Google API 作为平台目标创建一个新的 AVD.注意:仅限 Android 4.2.2 及更高版本的 Google APIs 平台包括 Google 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天全站免登陆