java无法解析符号“GoogleCloudMessaging”GCM
我试图让GCM
在我的应用程序中工作(当我们的时间改变时,或者当我们有任何促销活动时,通知用户),但是当我试图使用谷歌云消息API时,我一直收到错误Cannot resolve symbol 'GoogleCloudMessaging'
我正在使用最新发布的Android studio IDE来编写此代码
这是我的GcmBroadcastReciever。java代码:
import 安卓.R;
import 安卓.app.Activity;
import 安卓.app.NotificationManager;
import 安卓.app.PendingIntent;
import 安卓.content.BroadcastReceiver;
import 安卓.content.Context;
import 安卓.content.Intent;
import 安卓.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();
}
}
# 1 楼答案
你知道吗
你必须:
<android-sdk>/extras/google/google_play_services/libproject/google-play-services_lib/
库项目李># 2 楼答案
尝试清理你的项目。为我工作
# 3 楼答案
确保在生成时添加了依赖项。格拉德尔>;同步>;构建-清理项目
为我工作:)
# 4 楼答案
如果您使用的是Android Studio:
1)下载了Google Play SDK(使用SDK管理器):
2)别忘了点击“与Gradle文件同步项目”按钮
这对我来说是个好办法
# 5 楼答案
您可能正在使用旧教程,但GCMRegistar是一个不推荐使用的API类
请改用GoogleCloudMessagingAPI
检查此项以获取完整的教程push notification using gcm
# 6 楼答案
如果您在Android Studio中,请确保在
build.gradle
中您有:然后运行
build
这对我很管用