本文整理汇总了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);
}
}
示例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: 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();
}
}
示例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);
}
}
示例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) {}
}
示例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);
}
示例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);
}
示例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();
}
}
示例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();
}
}
示例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);
}
示例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));
}
}
示例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);
}
示例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);
}
}
示例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) {}
}