Search in sources :

Example 6 with InstanceID

use of com.google.android.gms.iid.InstanceID in project IITB-App by wncc.

the class RegistrationIntentService method onHandleIntent.

@RequiresApi(api = Build.VERSION_CODES.GINGERBREAD)
@Override
protected void onHandleIntent(@Nullable Intent intent) {
    String token = null;
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    try {
        InstanceID instanceID = InstanceID.getInstance(this);
        token = instanceID.getToken("306601329049", GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
        Log.i(TAG, "GCM Registration Token: " + token);
        Toast.makeText(this, "GCM Registration Token: " + token, Toast.LENGTH_SHORT).show();
        sharedPreferences.edit().putBoolean(SENT_TOKEN_TO_SERVER, true).apply();
    } catch (Exception e) {
        Log.d(TAG, "Failed to complete token refresh", e);
        sharedPreferences.edit().putBoolean(SENT_TOKEN_TO_SERVER, false).apply();
    }
    // Notify UI that registration is complete
    Intent registrationComplete = new Intent(REGISTRATION_COMPLETE);
    registrationComplete.putExtra("Token", token);
    LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}
Also used : SharedPreferences(android.content.SharedPreferences) InstanceID(com.google.android.gms.iid.InstanceID) Intent(android.content.Intent) RequiresApi(android.support.annotation.RequiresApi)

Example 7 with InstanceID

use of com.google.android.gms.iid.InstanceID in project android-oss by kickstarter.

the class RegisterService method onHandleIntent.

@Override
protected void onHandleIntent(@NonNull final 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);
    }
}
Also used : InstanceID(com.google.android.gms.iid.InstanceID) IOException(java.io.IOException)

Example 8 with InstanceID

use of com.google.android.gms.iid.InstanceID in project android-oss by kickstarter.

the class UnregisterService method onHandleIntent.

@Override
protected void onHandleIntent(@NonNull final 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);
    }
}
Also used : InstanceID(com.google.android.gms.iid.InstanceID)

Example 9 with InstanceID

use of com.google.android.gms.iid.InstanceID in project FastGCM by iammert.

the class RegistrationService method onHandleIntent.

/**
     * This Intent is responsible for get token from GCM server and send
     * broadcast. After we get token, we save this token to shared preferences.
     * @param intent
     */
@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);
        sharedPreferences.edit().putBoolean(Constants.SHARED_KEY_HAS_TOKEN, true).apply();
        sharedPreferences.edit().putString(Constants.SHARED_KEY_TOKEN, token).apply();
    } catch (Exception e) {
        sharedPreferences.edit().putBoolean(Constants.SHARED_KEY_HAS_TOKEN, false).apply();
    }
    LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(Constants.INTENT_REGISTRATION_COMPLETE));
}
Also used : SharedPreferences(android.content.SharedPreferences) InstanceID(com.google.android.gms.iid.InstanceID) Intent(android.content.Intent)

Example 10 with InstanceID

use of com.google.android.gms.iid.InstanceID in project phonegap-plugin-push by phonegap.

the class RegistrationIntentService method onHandleIntent.

@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);
        PushPlugin.setRegistrationID(token);
        Log.i(LOG_TAG, "new GCM Registration Token: " + token);
    } catch (Exception e) {
        Log.d(LOG_TAG, "Failed to complete token refresh", e);
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) InstanceID(com.google.android.gms.iid.InstanceID) IOException(java.io.IOException)

Aggregations

InstanceID (com.google.android.gms.iid.InstanceID)12 IOException (java.io.IOException)7 SharedPreferences (android.content.SharedPreferences)5 Intent (android.content.Intent)4 RequiresApi (android.support.annotation.RequiresApi)1 InvalidJidException (eu.siacs.conversations.xmpp.jid.InvalidJidException)1