當前位置: 首頁>>代碼示例>>Java>>正文


Java InstanceID.getInstance方法代碼示例

本文整理匯總了Java中com.google.android.gms.iid.InstanceID.getInstance方法的典型用法代碼示例。如果您正苦於以下問題:Java InstanceID.getInstance方法的具體用法?Java InstanceID.getInstance怎麽用?Java InstanceID.getInstance使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.google.android.gms.iid.InstanceID的用法示例。


在下文中一共展示了InstanceID.getInstance方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: onHandleIntent

import com.google.android.gms.iid.InstanceID; //導入方法依賴的package包/類
@Override
protected void onHandleIntent(Intent intent) {
    Log.d(TAG,"GCMRegistrationService: onHandleIntent()");
    try {
        InstanceID instanceID = InstanceID.getInstance(this);
        int gcmSenderIDIdentifier = getResources().getIdentifier("gcm_sender_id", "string", getPackageName());
        String gcmSenderId = getString(gcmSenderIDIdentifier);
        Log.d(TAG, "GCM Sender ID: " + gcmSenderId);
        String token = instanceID.getToken(gcmSenderId,
                GoogleCloudMessaging.INSTANCE_ID_SCOPE,
                null);
        Log.d(TAG, "Retrieved GCM Token: " + token);
        sendGCMTokenToActivity(token);
    } catch (Exception e) {
        /*
         * If we are unable to retrieve the GCM token we notify the Plugin
         * letting the user know this step failed.
         */
        Log.e(TAG, "Failed to retrieve GCM token", e);
        sendGCMTokenToActivity(null);
    }
}
 
開發者ID:jefflinwood,項目名稱:twilio-voice-phonegap-plugin,代碼行數:23,代碼來源:GCMRegistrationService.java

示例2: onHandleIntent

import com.google.android.gms.iid.InstanceID; //導入方法依賴的package包/類
@Override
protected void onHandleIntent(Intent intent) {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    try {
        InstanceID instanceID = InstanceID.getInstance(this);
        String token = instanceID.getToken(GarageDoorWidgetProvider.GCM_SENDER_ID,
                GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
        Log.i(TAG, "GCM Registration Token: " + token);

        sendRegistrationToServer(token);

        // You should store a boolean that indicates whether the generated token has been
        // sent to your server. If the boolean is false, send the token to your server,
        // otherwise your server should have already received the token.
        sharedPreferences.edit().putBoolean(GarageDoorWidgetProvider.PREF_SENT_TOKEN_TO_SERVER, true).apply();
    } catch (Exception e) {
        Log.d(TAG, "Failed to complete token refresh", e);
        // If an exception happens while fetching the new token or updating our registration data
        // on a third-party server, this ensures that we'll attempt the update at a later time.
        sharedPreferences.edit().putBoolean(GarageDoorWidgetProvider.PREF_SENT_TOKEN_TO_SERVER, false).apply();
    }
    // Notify UI that registration has completed, so the progress indicator can be hidden.
    Intent registrationComplete = new Intent(GarageDoorWidgetProvider.PREF_REGISTRATION_COMPLETE);
    LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}
 
開發者ID:jpuderer,項目名稱:GarageDoor,代碼行數:26,代碼來源:RegistrationIntentService.java

示例3: onHandleIntent

import com.google.android.gms.iid.InstanceID; //導入方法依賴的package包/類
@Override
protected void onHandleIntent(Intent intent) {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

    try {
        synchronized (TAG) {
            InstanceID instanceID = InstanceID.getInstance(this);
            String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
                    GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
            Log.i(TAG, "GCM Registration Token: " + token);
            sharedPreferences.edit().putString(Preferences.TOKEN, token).apply();
        }
    } catch (Exception e) {
        Log.d(TAG, "Failed to complete token refresh", e);
        //sharedPreferences.edit().putBoolean(Preferences.SENT_TOKEN_TO_SERVER, false).apply();
    }
    // Notify UI that registration has completed, so the progress indicator can be hidden.
    Intent registrationComplete = new Intent(Preferences.REGISTRATION_COMPLETE);
    LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}
 
開發者ID:kflauri2312lffds,項目名稱:Android_watch_magpie,代碼行數:21,代碼來源:RegistrationIntentService.java

示例4: register

import com.google.android.gms.iid.InstanceID; //導入方法依賴的package包/類
/**
 * @return A task that can be resolved upon completion of registration to both GCM and Stitch.
 */
@Override
public Task<Void> register() {
    final InstanceID instanceId = InstanceID.getInstance(getContext());

    return getRegistrationToken(instanceId, _info.getSenderId())
            .continueWithTask(new Continuation<String, Task<Void>>() {
                @Override
                public Task<Void> then(@NonNull final Task<String> task) throws Exception {
                    if (!task.isSuccessful()) {
                        throw task.getException();
                    }

                    return registerWithServer(task.getResult());
                }
            });
}
 
開發者ID:mongodb,項目名稱:stitch-android-sdk,代碼行數:20,代碼來源:GCMPushClient.java

示例5: deregister

import com.google.android.gms.iid.InstanceID; //導入方法依賴的package包/類
/**
 * @return A task that can be resolved upon completion of deregistration from both GCM and Stitch.
 */
@Override
public Task<Void> deregister() {
    final InstanceID instanceId = InstanceID.getInstance(getContext());

    return deleteRegistrationToken(instanceId, _info.getSenderId())
            .continueWithTask(new Continuation<Void, Task<Void>>() {
                @Override
                public Task<Void> then(@NonNull final Task<Void> task) throws Exception {
                    if (!task.isSuccessful()) {
                        throw task.getException();
                    }

                    return deregisterWithServer();
                }
            });
}
 
開發者ID:mongodb,項目名稱:stitch-android-sdk,代碼行數:20,代碼來源:GCMPushClient.java

示例6: onHandleIntent

import com.google.android.gms.iid.InstanceID; //導入方法依賴的package包/類
@Override
protected void onHandleIntent(Intent intent) {

    String deviceId = intent.getStringExtra("DEVICE_ID");
    String deviceName = intent.getStringExtra("DEVICE_NAME");

    try {
        InstanceID instanceID = InstanceID.getInstance(this);
        String registrationId = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
        registerDeviceProcess(deviceName,deviceId,registrationId);

    } catch (IOException e) {
        e.printStackTrace();
    }

}
 
開發者ID:Learn2Crack,項目名稱:android-gcm-client-demo,代碼行數:17,代碼來源:RegistrationIntentService.java

示例7: handleIntent

import com.google.android.gms.iid.InstanceID; //導入方法依賴的package包/類
@Override
public void handleIntent(ServiceAction action, Intent intent) {
	if (!tokenHolder.hasValidToken()) {
		DebugLog.i("obtaining new GCM token");
		InstanceID instanceID = InstanceID.getInstance(context);
		try {
			String token = instanceID.getToken(
				context.getString(R.string.gcm_defaultSenderId),
				GoogleCloudMessaging.INSTANCE_ID_SCOPE,
				null
			);
			GcmTokenHolder.create(context).storeToken(token);
			DebugLog.i("GCM token obtained successfully");
		} catch (IOException e) {
			DebugLog.logException(e);
		}
	}
	WorkerService.getLauncher(context).launchService(ServiceAction.SYNC_DATA);
}
 
開發者ID:Microsoft,項目名稱:EmbeddedSocial-Android-SDK,代碼行數:20,代碼來源:GetGcmIdHandler.java

示例8: onHandleIntent

import com.google.android.gms.iid.InstanceID; //導入方法依賴的package包/類
/**
 * This method receives an intent from the system when a GCM token is received.
 *
 * @param intent an Intent containing the GCM token for the application to use
 */
@Override
protected void onHandleIntent(Intent intent) {

    String token = "";
    try {
        // Initially this call goes out to the network to retrieve the token, subsequent calls
        // are local. R.string.gcm_defaultSenderId (the Sender ID) is typically derived from
        // google-services.json.
        InstanceID instanceID = InstanceID.getInstance(this);
        token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
                GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
    } catch (Exception e) {
        Log.d(TAG, "Failed to complete token refresh", e);
    }
    // Broadcast the token to the login activity so the account can be created on the [email protected]
    // server.
    Intent registrationComplete = new Intent(Constants.TOKEN_BROADCAST);
    registrationComplete.putExtra(Constants.TOKEN_EXTRA, token);
    // Unique broadcast ID to prevent broadcast receiver from receiving the same broadcast twice
    registrationComplete.putExtra(Constants.TOKEN_BROADCAST_ID_EXTRA,
            new Random().nextInt(Integer.MAX_VALUE));
    LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}
 
開發者ID:whereuat,項目名稱:whereuat-android,代碼行數:29,代碼來源:RegistrationIntentService.java

示例9: onHandleIntent

import com.google.android.gms.iid.InstanceID; //導入方法依賴的package包/類
@Override
protected void onHandleIntent(Intent intent) {
    SharedPreferences sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(this);
    try {
        synchronized (TAG) {
            InstanceID instanceID = InstanceID.getInstance(this);
            String token = instanceID.getToken(getString(R.string.gcm_sender_id),
                    GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
            Log.d(TAG, "GCM registration token: " + token);

            sharedPreferences.edit().putString("device_id", token).apply();
        }
    } catch (IOException e) {
        Log.d(TAG, "Failed to complete token refresh", e);
    }
    Intent registrationComplete = new Intent(RegistrationConstants.REGISTRATION_COMPLETE);
    LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}
 
開發者ID:mattermost,項目名稱:mattermost-android-classic,代碼行數:20,代碼來源:RegistrationIntentService.java

示例10: subscribeToTopic

import com.google.android.gms.iid.InstanceID; //導入方法依賴的package包/類
/**
 * Subscribe to a topic
 */
public void subscribeToTopic(String topic) {
    GcmPubSub pubSub = GcmPubSub.getInstance(getApplicationContext());
    InstanceID instanceID = InstanceID.getInstance(getApplicationContext());
    String token = null;
    String gcm_server_sender_id = SharedPref.getSenderId(GcmIntentService.this);
    try {
        token = instanceID.getToken(gcm_server_sender_id,
                GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
        if (token != null) {
            pubSub.subscribe(token, "/topics/" + topic, null);
            Log.d(TAG, "Subscribed to topic: " + topic);
        } else {
            Log.d(TAG, "error: gcm registration id is null");
        }
    } catch (IOException e) {
        Log.e(TAG, "Topic subscribe error. Topic: " + topic + ", error: " + e.getMessage());
        Toast.makeText(getApplicationContext(), "Topic subscribe error. Topic: " + topic + ", error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
 
開發者ID:saikiapriyam,項目名稱:PushEZ,代碼行數:23,代碼來源:GcmIntentService.java

示例11: onHandleIntent

import com.google.android.gms.iid.InstanceID; //導入方法依賴的package包/類
@Override
protected void onHandleIntent(final @Nullable Intent intent) {
  Timber.d("onHandleIntent");

  try {
    // This initially hits the network to retrieve the token, subsequent calls are local
    final InstanceID instanceID = InstanceID.getInstance(this);

    // R.string.gcm_defaultSenderId is derived from google-services.json
    final String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
      GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
    Timber.d("Token: %s", token);

    sendTokenToApi(token);
    subscribeToGlobalTopic(token);
  } catch (final Exception e) {
    Timber.e("Failed to complete token refresh: %s", e);
  }
}
 
開發者ID:kickstarter,項目名稱:android-oss,代碼行數:20,代碼來源:RegisterService.java

示例12: onHandleIntent

import com.google.android.gms.iid.InstanceID; //導入方法依賴的package包/類
@Override
protected void onHandleIntent(Intent intent) {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

    try {
        synchronized (TAG) {
            InstanceID instanceID = InstanceID.getInstance(this);
            String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
                    GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

            Log.i(TAG, "GCM Registration Token: " + token);

            sharedPreferences.edit().putString(GCM_TOKEN, token).apply();
        }
    } catch (Exception e) {
        Log.d(TAG, "Failed to complete token refresh", e);

        sharedPreferences.edit().putString(GCM_TOKEN, "").apply();
    }

    // Notify UI that registration has completed, so the progress indicator can be hidden.
    Intent registrationComplete = new Intent(REGISTRATION_COMPLETE);
    LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}
 
開發者ID:xserv,項目名稱:xserv-android,代碼行數:25,代碼來源:RegistrationIntentService.java

示例13: onHandleIntent

import com.google.android.gms.iid.InstanceID; //導入方法依賴的package包/類
@Override
protected void onHandleIntent(Intent intent) {
    SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE);

    try {
        InstanceID instanceID = InstanceID.getInstance(this);
        String senderID = sharedPreferences.getString(SENDER_ID, "");
        String token = instanceID.getToken(senderID,
                GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
        Log.i(LOG_TAG, "new GCM Registration Token: " + token);

        // save new token
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(REGISTRATION_ID, token);
        editor.commit();

    } catch (Exception e) {
        Log.d(LOG_TAG, "Failed to complete token refresh", e);
    }
}
 
開發者ID:trecrts,項目名稱:trecrts-eval,代碼行數:21,代碼來源:RegistrationIntentService.java

示例14: getToken

import com.google.android.gms.iid.InstanceID; //導入方法依賴的package包/類
public static void getToken(Context context, boolean refresh) {
    try {
        // Get token
        InstanceID instanceID = InstanceID.getInstance(context);
        String token = instanceID.getToken(
                context.getString(R.string.gcm_defaultSenderId),
                GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
        Log.i(TAG, "Token=" + token);

        // Store token
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        prefs.edit().putString(SettingsFragment.PREF_GCM_TOKEN, token).apply();

        // Subscribe to topics
        GcmService.subscribeBroadcasts(context);
        GcmService.subscribeWeatherUpdates(context);
    } catch (IOException ex) {
        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
    }
}
 
開發者ID:M66B,項目名稱:BackPackTrackII,代碼行數:21,代碼來源:IIDService.java

示例15: registerToGCM

import com.google.android.gms.iid.InstanceID; //導入方法依賴的package包/類
public void registerToGCM() {

        Intent registrationComplete;
        String token = null;
        try {
            InstanceID instanceID = InstanceID.getInstance(getApplicationContext());
            logger.d("senderId is :" + getString(R.string.senderId));
            token = instanceID.getToken(getString(R.string.senderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
            logger.d("GCMIntentService token : " + token);
            registrationComplete = new Intent(REGISTRATION_SUCESS);
            registrationComplete.putExtra("token", token);

        } catch (Exception e) {
            registrationComplete = new Intent(REGISTRATION_FAILD);
            logger.e("GCMIntentService registration failed " + token);
        }
        //send broadcast
        LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
    }
 
開發者ID:tw-blr-iot-ants,項目名稱:KanJuice,代碼行數:20,代碼來源:GCMRegistrationIntentService.java


注:本文中的com.google.android.gms.iid.InstanceID.getInstance方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。