30

Tôi đang cố gắng để có được GCM làm việc trong ứng dụng của mình (để thông báo cho người dùng khi giờ của chúng tôi thay đổi hoặc khi chúng tôi có bất kỳ khuyến mại nào), nhưng tôi vẫn gặp lỗi Cannot resolve symbol 'GoogleCloudMessaging' khi cố gắng sử dụng API Nhắn tin qua đám mây của Google.Không thể giải quyết biểu tượng 'GoogleCloudMessaging' GCM

Tôi đang sử dụng IDE studio Android mới được phát hành để viết mã này.

Đây là mã GcmBroadcastReciever.java tôi:

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

Bạn có thể tìm giải pháp này không? Tôi đang phải đối mặt với vấn đề tương tự. – Geek

+0

Nhìn vào câu trả lời. Nhập khẩu là giải pháp vì vậy chỉ cần làm theo các bước của Eran – dillonr

Trả lời

35

Các phần dưới đây hướng dẫn bạn qua các quá trình thiết lập một GCM thực hiện. Trước khi bắt đầu, hãy đảm bảo thiết lập Google Play SDK dịch vụ. Bạn cần SDK này để sử dụng các phương pháp GoogleCloudMessaging. Nói đúng ra, điều duy nhất bạn hoàn toàn cần API này là nhắn tin ngược dòng (thiết bị đến đám mây) nhưng cũng cung cấp API đăng ký hợp lý được đề xuất.

Bạn có set up the Google Play Services SDK không?

Bạn cần phải:

  1. Cài đặt Google Play dịch vụ SDK
  2. tham khảo dự án <android-sdk>/extras/google/google_play_services/libproject/google-play-services_lib/ thư viện trong dự án Android của bạn.

Để cài đặt Google Play dịch vụ SDK để phát triển:

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. 
+4

tôi đã cài đặt nó, nhưng không được nhập vào dự án của tôi như là một sự phụ thuộc. nhập nó cố định vấn đề của tôi, cổ vũ – dillonr

+0

Tôi đã không chắc chắn làm thế nào để làm # 2, nhưng sau đó tôi đọc câu trả lời của thống nhất và cố gắng "đồng bộ gradle" sau khi bướC# 1 ở đây. Đó là kết hợp làm việc cho tôi. – eselk

+0

Yup chỉ cần thêm nó làm phụ thuộc và nó sẽ hoạt động. –

10

Nếu bạn đang sử dụng Android Studio:

1) Downloaded Google Play SDK (sử dụng quản lý SDK):

SDK Manager

2) Đừng quên click vào "Dự án Synch với Gradle Files" nút

Synch Project with Gradle Files

đó đã làm các trick cho tôi.

+0

Đồng bộ hóa đã giải quyết được sự cố của tôi, cảm ơn! – Sloy

6

Nếu bạn đang ở trong Android Studio, hãy chắc chắn rằng trong bạn build.gradle bạn có:

dependencies { 
    compile 'com.google.android.gms:play-services:7.8.0' 
} 

và sau đó chạy build.

Điều đó phù hợp với tôi.

2

Có thể bạn đang sử dụng hướng dẫn cũ nhưng GCMRegistrar là một lớp API không dùng nữa.

Vui lòng sử dụng API GoogleCloudMessaging thay thế.

Kiểm tra này cho hoàn chỉnh hướng dẫn push notification using gcm

3

Hãy thử làm sạch dự án của bạn. Đã làm cho tôi.

4

Đảm bảo bạn đã thêm phụ thuộc vào build.gradle> Sync> Build - Clean Project.

Làm việc cho tôi :)

+0

Cảm ơn, @rischan nó hoạt động .. –

Các vấn đề liên quan