本文整理汇总了Java中com.google.android.gms.iid.InstanceID类的典型用法代码示例。如果您正苦于以下问题:Java InstanceID类的具体用法?Java InstanceID怎么用?Java InstanceID使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
InstanceID类属于com.google.android.gms.iid包,在下文中一共展示了InstanceID类的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);
}
}
示例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);
}
示例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);
}
示例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());
}
});
}
示例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();
}
});
}
示例6: deleteRegistrationToken
import com.google.android.gms.iid.InstanceID; //导入依赖的package包/类
/**
* Deletes the current registration token.
*
* @param instanceId The instance ID which generated the registration token.
* @param senderId The sender ID to revoke the registration token from.
* @return A task that can be resolved upon deletion of the token.
*/
private Task<Void> deleteRegistrationToken(final InstanceID instanceId, final String senderId) {
final TaskCompletionSource<Void> future = new TaskCompletionSource<>();
new AsyncTask<Object, Integer, String>() {
@Override
protected String doInBackground(final Object[] ignored) {
try {
instanceId.deleteToken(senderId, GoogleCloudMessaging.INSTANCE_ID_SCOPE);
} catch (final IOException e) {
Log.e(TAG, "Error deleting GCM registration token", e);
future.setException(e);
return null;
}
future.setResult(null);
return null;
}
}.execute();
return future.getTask();
}
示例7: getRegistrationToken
import com.google.android.gms.iid.InstanceID; //导入依赖的package包/类
/**
* Gets or creates a registration token.
*
* @param instanceId The instance ID which should generate the registration token.
* @param senderId The sender ID to generate the registration token for.
* @return A task that can be resolved upon creating/retrieval of the token.
*/
private Task<String> getRegistrationToken(final InstanceID instanceId, final String senderId) {
final TaskCompletionSource<String> future = new TaskCompletionSource<>();
new AsyncTask<Object, Integer, String>() {
@Override
protected String doInBackground(final Object[] ignored) {
final String registrationToken;
try {
registrationToken = instanceId.getToken(senderId, GoogleCloudMessaging.INSTANCE_ID_SCOPE);
} catch (final IOException e) {
Log.e(TAG, "Error getting GCM registration token", e);
future.setException(e);
return null;
}
future.setResult(registrationToken);
return null;
}
}.execute();
return future.getTask();
}
示例8: renewInstanceToken
import com.google.android.gms.iid.InstanceID; //导入依赖的package包/类
private void renewInstanceToken(final Context context) {
new Thread(new Runnable() {
@Override
public void run() {
InstanceID instanceID = InstanceID.getInstance(context);
try {
instanceID.deleteInstanceID();
Intent intent = new Intent(context, XmppConnectionService.class);
intent.setAction(XmppConnectionService.ACTION_GCM_TOKEN_REFRESH);
context.startService(intent);
} catch (IOException e) {
Log.d(Config.LOGTAG,"unable to renew instance token",e);
}
}
}).start();
}
示例9: 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();
}
}
示例10: onHandleIntent
import com.google.android.gms.iid.InstanceID; //导入依赖的package包/类
@Override
protected void onHandleIntent(Intent intent) {
// Preferences to get registration id from device
SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String mNextId = mSharedPreferences.getString(REGISTRATION_ID_PREFERENCE, null);
try {
InstanceID mInstanceID = InstanceID.getInstance(this);
String mToken = mInstanceID.getToken(getString(R.string.gcm_default_sender_id),
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
executeBusinessRule(mToken);
if(mNextId!=null && mNextId!=mToken) {
mSharedPreferences.edit().putString(REGISTRATION_ID_PREFERENCE, mToken).apply();
mNextId = mToken;
}
} catch (Exception e) {
Log.d(REGISTRATION_DEBUG, "Failed to refresh current token: " + e.getMessage());
}
// Complete process
if(mNextId!=null) {
Intent mRegisterIntent = new Intent(REGISTRATION_COMPLETE);
LocalBroadcastManager.getInstance(this).sendBroadcast(mRegisterIntent);
}
}
示例11: 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();
}
}
示例12: onHandleIntent
import com.google.android.gms.iid.InstanceID; //导入依赖的package包/类
@Override
protected void onHandleIntent(Intent intent) {
boolean register = intent.getBooleanExtra(LibrusConstants.REGISTER, false);
try {
if (register) {
String token = InstanceID.getInstance(this)
.getToken(APP_ID, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
LibrusUtils.log("Retrieved GCM token " + token);
new APIClient(this).pushDevices(token)
.subscribe(() -> {
}, LibrusUtils::handleError);
} else {
InstanceID.getInstance(this)
.deleteToken(APP_ID, GoogleCloudMessaging.INSTANCE_ID_SCOPE);
LibrusUtils.log("Unregistered GCM");
}
} catch (IOException e) {
LibrusUtils.logError("Failed to register GCM");
FirebaseCrash.report(e);
e.printStackTrace();
}
}
示例13: 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(getString(R.string.gcm_defaultSenderId),
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
Log.i(TAG, "GCM Registration Token: " + token);
subscribeTopics(token);
sharedPreferences.edit().putString(QuickstartPreferences.TOKEN, token).apply();
sharedPreferences.edit().putBoolean(ml.hepolise.vkaudiosave.QuickstartPreferences.SENT_TOKEN_TO_SERVER, true).apply();
} catch (Exception e) {
Log.d(TAG, "Failed to complete token refresh", e);
sharedPreferences.edit().putBoolean(ml.hepolise.vkaudiosave.QuickstartPreferences.SENT_TOKEN_TO_SERVER, false).apply();
}
Intent registrationComplete = new Intent(ml.hepolise.vkaudiosave.QuickstartPreferences.REGISTRATION_COMPLETE);
LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}
示例14: onHandleIntent
import com.google.android.gms.iid.InstanceID; //导入依赖的package包/类
@Override
protected void onHandleIntent(final @NonNull Intent intent) {
Timber.d("onHandleIntent");
try {
final InstanceID instanceID = InstanceID.getInstance(this);
instanceID.deleteToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE);
Timber.d("Deleted token");
} catch (final Exception e) {
Timber.e("Failed to delete token: %s", e);
}
}
示例15: 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);
}
}