Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法解析符号';谷歌云通讯&x27;GCM_Java_Android_Push Notification_Google Cloud Messaging_Android Studio - Fatal编程技术网

Java 无法解析符号';谷歌云通讯&x27;GCM

Java 无法解析符号';谷歌云通讯&x27;GCM,java,android,push-notification,google-cloud-messaging,android-studio,Java,Android,Push Notification,Google Cloud Messaging,Android Studio,我正在尝试让GCM在我的应用程序中工作(当我们的工作时间改变时,或者当我们有任何促销活动时,通知用户),但我一直收到错误在尝试使用谷歌云消息API时无法解析符号“GoogleCloudMessaging” 我正在使用最新发布的Android studio IDE来编写此代码 以下是我的GcmBroadcastReciever.java代码: import android.R; import android.app.Activity; import android.app.Notification

我正在尝试让
GCM
在我的应用程序中工作(当我们的工作时间改变时,或者当我们有任何促销活动时,通知用户),但我一直收到错误
在尝试使用谷歌云消息API时无法解析符号“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();
    }
}
以下各节将指导您完成设置GCM的过程 实施开始之前,请确保设置Google Play 服务SDK您需要此SDK才能使用GoogleCloudMessaging方法。严格地说,你唯一绝对需要的就是这个 用于上游(设备到云)消息传递的API,但它还提供 建议使用简化的注册API

是吗

你必须:

  • 安装Google Play服务SDK
  • 请在Android项目中参考
    /extras/google/google\u play\u services/libproject/google-play-services\u lib/
    库项目
  • 要安装用于开发的Google Play services 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.
    
    1。启动SDK管理器。
    -从Eclipse(使用ADT)中,选择窗口>Android SDK管理器。
    -在Windows上,双击Android应用程序根目录下的SDK Manager.exe文件
    SDK目录。
    -在Mac或Linux上,打开终端并导航到中的工具/目录
    安装Android SDK,然后执行Android SDK。
    2.安装Google Play services SDK。
    滚动至软件包列表底部,展开Extras,选择Google Play
    服务,并安装它。
    Google Play services SDK保存在您的Android SDK环境中
    /附加服务/google/google\u play\u services/。
    3.安装Google API平台的兼容版本。
    如果要在emulator上测试应用程序,请展开
    Android 4.2.2(API 17)或更高版本,选择Google API,然后
    安装它。然后创建一个新的AVD,使用GoogleAPI作为平台目标。
    注意:只有Android 4.2.2和更高版本的Google API平台
    包括谷歌播放服务。
    

    如果您使用的是Android Studio:

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

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


    这对我来说是个好办法。

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

    dependencies {
        compile 'com.google.android.gms:play-services:7.8.0'
    }
    
    然后运行
    build


    这对我很有用。

    您可能正在使用一个旧的教程,但gcmregistar是一个不推荐使用的API类

    请改用API


    检查本教程是否完整

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

    确保您在build.gradle>Sync>build-Clean项目上添加了依赖项


    为我工作:)

    我安装了它,但没有作为依赖项导入到我的项目中。导入它解决了我的问题,cheersI不确定如何执行#2,但随后我阅读了unify的答案,并在执行步骤#1后尝试了“同步渐变”。那个组合对我有用。