本文介绍了无法解析的符号“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();
    }
}
 

解决方案

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

您必须:

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

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();
    }
}
解决方案

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.

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

07-24 12:59