当前位置: 首页>>代码示例>>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;未经允许,请勿转载。