当前位置: 首页>>代码示例>>Java>>正文


Java InstanceID类代码示例

本文整理汇总了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);
    }
}
 
开发者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: 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();
}
 
开发者ID:mongodb,项目名称:stitch-android-sdk,代码行数:29,代码来源:GCMPushClient.java

示例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();
}
 
开发者ID:mongodb,项目名称:stitch-android-sdk,代码行数:30,代码来源:GCMPushClient.java

示例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();

}
 
开发者ID:syntafin,项目名称:TenguChat,代码行数:18,代码来源:MaintenanceReceiver.java

示例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();
    }

}
 
开发者ID:Learn2Crack,项目名称:android-gcm-client-demo,代码行数:17,代码来源:RegistrationIntentService.java

示例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);
    }
}
 
开发者ID:brunogabriel,项目名称:gcm-android,代码行数:27,代码来源:RegistrationIntentService.java

示例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();
    }
}
 
开发者ID:saikiapriyam,项目名称:PushEZ,代码行数:23,代码来源:GcmIntentService.java

示例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();
    }
}
 
开发者ID:shymmq,项目名称:librus-client,代码行数:23,代码来源:RegistrationIntentService.java

示例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);
}
 
开发者ID:Hepolise,项目名称:VKAudioSave,代码行数:20,代码来源:RegistrationIntentService.java

示例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);
  }
}
 
开发者ID:kickstarter,项目名称:android-oss,代码行数:13,代码来源:UnregisterService.java

示例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);
  }
}
 
开发者ID:kickstarter,项目名称:android-oss,代码行数:20,代码来源:RegisterService.java


注:本文中的com.google.android.gms.iid.InstanceID类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。