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


Java InstanceID.getToken方法代碼示例

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


在下文中一共展示了InstanceID.getToken方法的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: 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

示例5: getNewToken

import com.google.android.gms.iid.InstanceID; //導入方法依賴的package包/類
@NonNull
protected String getNewToken() throws Exception {
    final InstanceID instanceId = InstanceID.getInstance(mAppContext);
    Log.d(LOGTAG, "GCM is refreshing token... instanceId=" + instanceId.getId());

    // TODO why is this needed?
    GoogleCloudMessaging.getInstance(mAppContext).close();

    try {
        final String registrationToken = instanceId.getToken(getSenderId(), GoogleCloudMessaging.INSTANCE_ID_SCOPE);
        Log.i(LOGTAG, "GCM has a new token: instanceId=" + instanceId.getId() + ", token=" + registrationToken);
        return registrationToken;
    } catch (Exception e) {
        throw new Exception("FATAL: Failed to fetch a fresh new token, instanceId=" + instanceId.getId(), e);
    }
}
 
開發者ID:wix,項目名稱:react-native-notifications,代碼行數:17,代碼來源:GcmToken.java

示例6: onHandleIntent

import com.google.android.gms.iid.InstanceID; //導入方法依賴的package包/類
@Override
protected void onHandleIntent(Intent intent) {
    // Make a call to Instance API
    InstanceID instanceID = InstanceID.getInstance(this);
    String senderId = getResources().getString(R.string.gcm_defaultSenderId);
    try {
        // request token that will be used by the server to send push notifications
        String token = instanceID.getToken(senderId, GoogleCloudMessaging.INSTANCE_ID_SCOPE);

        // pass along this data
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        sharedPreferences.edit().putString(GCM_TOKEN, token).apply();

        sendRegistrationToServer(token);

    } catch (IOException e) {}
}
 
開發者ID:tamcgoey,項目名稱:dasher-app-android,代碼行數:18,代碼來源:RegistrationIntentService.java

示例7: 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

示例8: 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

示例9: 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

示例10: onHandleIntent

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

        InstanceID instanceID = InstanceID.getInstance(this);
        String senderId = mSharedPreferences.getString(PushKeys.SENDER_ID, null);
        String token = instanceID.getToken(senderId,
                GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

        sendRegistrationToServer(token);

        subscribeTopics(token);


        mSharedPreferences.edit().putString(PushKeys.TOKEN, token).apply();

    } catch (Exception e) {
        Log.d(TAG, "Failed to complete token refresh", e);
        mSharedPreferences.edit().putBoolean(PushKeys.SENT_TOKEN_TO_SERVER, false).apply();
    }
}
 
開發者ID:lukamarin,項目名稱:Rocket.Chat-android,代碼行數:24,代碼來源:RocketRegistrationIntentService.java

示例11: 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

示例12: 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

示例13: 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

示例14: 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

示例15: onHandleIntent

import com.google.android.gms.iid.InstanceID; //導入方法依賴的package包/類
@Override
protected void onHandleIntent(Intent intent) {
       // We haven't yet received the key; let's exit here
       String senderKey = State.getInstance().getGcmSender(getApplicationContext());
       if (senderKey == null || senderKey == "") {
           return;
       }

       SharedPreferences sp = SharedPreferencesHelper.getInstance().getSharedPreferences(getApplicationContext());

       InstanceID instanceID = InstanceID.getInstance(this);
	try {
		String token = instanceID.getToken(senderKey, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
		DeviceApiHandler.register(token, sp);
	} catch (IOException e) {}
}
 
開發者ID:appwoodoo,項目名稱:appwoodoo-android-sdk,代碼行數:17,代碼來源:WoodooRegistrationIntentService.java


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