2013-12-10 31 views
2

我試圖使用新Google雲端通訊API實現GCM對應GCMRegistrar.isRegistered()。在新的GCM

我想檢查設備是否在雲上註冊,之前使用GCMRegistrar.isRegistered()進行了檢查。

有沒有什麼辦法可以實現使用新的API這個檢查?
編輯:我知道我能救設好註冊編號在我的應用程序,但我想知道我的設備對雲的狀態 - 無論是註冊與否。

回答

4

棄用的GCMRegistrar只是存儲在本地RegistrationID設備上的助手客戶端類。 GCMRegistrar.isRegistered()從未調用GCM服務器來查找設備是否已註冊(因爲沒有此類API)。它只是檢查如果先前接收到的RegistrationID被本地存儲在設備上的特定應用程式(和無效存儲RegistrationId一些occassions,如該應用版本更改時)。

事實上,你可以看到GCMRegistrar here的代碼:

/** 
* Gets the current registration id for application on GCM service. 
* <p> 
* If result is empty, the registration has failed. 
* 
* @return registration id, or empty string if the registration is not 
*   complete. 
*/ 
public static String getRegistrationId(Context context) { 
    final SharedPreferences prefs = getGCMPreferences(context); 
    String registrationId = prefs.getString(PROPERTY_REG_ID, ""); 
    // check if app was updated; if so, it must clear registration id to 
    // avoid a race condition if GCM sends a message 
    int oldVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE); 
    int newVersion = getAppVersion(context); 
    if (oldVersion != Integer.MIN_VALUE && oldVersion != newVersion) { 
     Log.v(TAG, "App version changed from " + oldVersion + " to " + 
       newVersion + "; resetting registration id"); 
     clearRegistrationId(context); 
     registrationId = ""; 
    } 
    return registrationId; 
} 

/** 
* Checks whether the application was successfully registered on GCM 
* service. 
*/ 
public static boolean isRegistered(Context context) { 
    return getRegistrationId(context).length() > 0; 
} 

因此,如果您存儲的註冊ID在你的應用程序,你會實現,當你使用GCMRegistrar你有完全相同的功能。在客戶端應用程序中確定設備已註冊或未註冊GCM的唯一方法是致電GoogleCloudMessaging.registerGoogleCloudMessaging.unregister

+0

嘿,這是一個非常簡單的答案..謝謝! – Amruta

+0

不客氣! – Eran

-1

的關心U可以檢查註冊狀態以及更多類似這樣的

public class GCMIntentService extends GCMBaseIntentService { 

    private static final String TAG = "GCMIntentService"; 

    public GCMIntentService() { 
     super(SENDER_ID); 
    } 

    /** 
    * Method called on device registered 
    **/ 
    @Override 
    protected void onRegistered(Context context, String registrationId) { 
     Log.i(TAG, "Device registered: regId = " + registrationId); 
     // Toast.makeText(getApplicationContext(), "Your registration ID : " + 
     // registrationId, Toast.LENGTH_LONG).show(); 
     displayMessage(context, "Your device registred with GCM" 
       + registrationId); 
     //Log.d("NAME", LoginActivity.email); 
     ServerUtilities.register(context, LoginActivity.uid, 
       LoginActivity.phone, registrationId); 
    } 

    /** 
    * Method called on device un registred 
    * */ 
    @Override 
    protected void onUnregistered(Context context, String registrationId) { 
     Log.i(TAG, "Device unregistered"); 
     displayMessage(context, getString(R.string.gcm_unregistered)); 
     ServerUtilities.unregister(context, registrationId); 
    } 

    /** 
    * Method called on Receiving a new message 
    * */ 
    @Override 
    protected void onMessage(Context context, Intent intent) { 
     Log.i(TAG, "Received message"); 
     String message = intent.getExtras().getString("price"); 

     displayMessage(context, message); 
     // notifies user 
     generateNotification(context, message); 
    } 

    /** 
    * Method called on receiving a deleted message 
    * */ 
    @Override 
    protected void onDeletedMessages(Context context, int total) { 
     Log.i(TAG, "Received deleted messages notification"); 
     String message = getString(R.string.gcm_deleted, total); 
     displayMessage(context, message); 
     // notifies user 
     generateNotification(context, message); 
    } 

    /** 
    * Method called on Error 
    * */ 
    @Override 
    public void onError(Context context, String errorId) { 
     Log.i(TAG, "Received error: " + errorId); 
     displayMessage(context, getString(R.string.gcm_error, errorId)); 
    } 

    @Override 
    protected boolean onRecoverableError(Context context, String errorId) { 
     // log message 
     Log.i(TAG, "Received recoverable error: " + errorId); 
     displayMessage(context, 
       getString(R.string.gcm_recoverable_error, errorId)); 
     return super.onRecoverableError(context, errorId); 
    } 

    /** 
    * Issues a notification to inform the user that server has sent a message. 
    * This message wil be notified at status bar!!! 
    */ 
    @SuppressWarnings("deprecation") 
    private static void generateNotification(Context context, String message) { 
     int icon = R.drawable.ic_launcher; 
     long when = System.currentTimeMillis(); 
     NotificationManager notificationManager = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 

     Notification notification = new Notification(icon, message, when); 

     String title = context.getString(R.string.app_name); 

     Intent notificationIntent = new Intent(context, MainActivity.class); 
     // set intent so it does not start a new activity 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
       | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     PendingIntent intent = PendingIntent.getActivity(context, 0, 
       notificationIntent, 0); 
     notification.setLatestEventInfo(context, title, message, intent); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 

     // Play default notification sound 
     notification.defaults |= Notification.DEFAULT_SOUND; 

     // notification.sound = Uri.parse("android.resource://" + 
     // context.getPackageName() + "your_sound_file_name.mp3"); 

     // Vibrate if vibrate is enabled 
     notification.defaults |= Notification.DEFAULT_VIBRATE; 
     notificationManager.notify(0, notification); 

    } 

} 
+0

是的..但多數民衆贊成的觀點,其depricated .. – Amruta

+0

這是舊的,不贊成的接口,並在任何情況下列出的方法是所有回調不能直接由用戶調用。 – NickT

0

你需要這樣做

String SENDER_ID = "13312313";//product id will be different for you 
GoogleCloudMessaging gcm; 
Context context =getApplicationContext(); 
String regid; 

String msg = ""; 
       try { 
        if (gcm == null) { 
         gcm = GoogleCloudMessaging.getInstance(context); 
        } 
        regid = gcm.register(SENDER_ID); 
        storeRegistrationId(context, regid); 
        msg = regid; 
       } catch (IOException ex) { 
        msg=null; 
       } 

保存到sharedpreferences

private void storeRegistrationId(Context context, String regId) { 
     final SharedPreferences prefs = getGCMPreferences(context); 

     Log.i(TAG, "Saving regId on app version " + appVersion); 
     SharedPreferences.Editor editor = prefs.edit(); 
     editor.putString(PROPERTY_REG_ID, regId); 

     editor.commit(); 
    } 

現在檢查exixtence

final SharedPreferences prefs = getGCMPreferences(context); 
     String registrationId = prefs.getString(PROPERTY_REG_ID, ""); 
     if (TextUtils.isEmpty(registrationId)) { 
      Log.i(TAG, "Registration not found."); 
      return null; 
     } 

private SharedPreferences getGCMPreferences(Context context) { 
     // This sample app persists the registration ID in shared preferences, but 
     // how you store the regID in your app is up to you. 
     return getSharedPreferences(this.getClass().getSimpleName(), 
       Context.MODE_PRIVATE); 
    }