@Override protected String[] doInBackground(String... topics) { final GcmPubSub pubSub = GcmPubSub.getInstance(getContext()); try { SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext()); final String token = sharedPreferences.getString(AufzugswaechterPreferences.TOKEN, null); if (token != null) { for (String topic : topics) { toggleSubscription(pubSub, topic, token); } return topics; } else { return null; } } catch (IOException ioex) { // TODO error reporting ioex.printStackTrace(); return null; } catch (Exception ex) { ex.printStackTrace(); return null; } }
/** * 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(); } }
@Override protected void onHandleIntent(Intent intent) { String token = intent.getStringExtra(TOKEN); if (TextUtils.isEmpty(token)) { Logs.gcm("Empty token."); return; } if (!TextUtils.isEmpty(DEFAULT_TOPIC)) { Logs.gcm("subscribing to topic: " + DEFAULT_TOPIC + " with token " + token); try { GcmPubSub.getInstance(this).subscribe(token, "/topics/" + DEFAULT_TOPIC, null); } catch (IOException e) { e.printStackTrace(); } } }
public static void subscribeWeatherUpdates(Context context) throws IOException { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); String token = prefs.getString(SettingsFragment.PREF_GCM_TOKEN, null); boolean subscribe = prefs.getBoolean(SettingsFragment.PREF_WEATHER_GCM, false); if (token == null) { Log.i(TAG, "Subscribe weather updates: no token"); return; } String topic = "/topics/weather"; GcmPubSub pubSub = GcmPubSub.getInstance(context); if (subscribe) pubSub.subscribe(token, topic, null); else pubSub.unsubscribe(token, topic); Log.i(TAG, "Subcribe " + topic + "=" + subscribe); }
public void registerToStoryTopic(final User user, final Story story) { new AsyncTask<Void,Void,Void>() { @Override protected Void doInBackground(Void... params) { try { if(user.getPushIdentity() != null) { GcmPubSub gcmPubStub = GcmPubSub.getInstance(context); gcmPubStub.subscribe(user.getPushIdentity(), getTopicName(STORY_TOPICS_PATH, story.getKey()), null); } } catch(Exception exception) { Log.e(TAG, "registerToChatRoomTopic ", exception); } return null; } }.execute(); }
public void registerToChatRoomTopic(final User user, final String chatRoomKey) { new AsyncTask<Void,Void,Void>() { @Override protected Void doInBackground(Void... params) { try { if(user.getPushIdentity() != null) { GcmPubSub gcmPubStub = GcmPubSub.getInstance(context); gcmPubStub.subscribe(user.getPushIdentity(), getTopicName(CHAT_TOPICS_PATH, chatRoomKey), null); } } catch(Exception exception) { Log.e(TAG, "registerToChatRoomTopic ", exception); } return null; } }.execute(); }
public void unregisterToChatRoomTopic(final User user, final String chatRoomKey) { new AsyncTask<Void,Void,Void>() { @Override protected Void doInBackground(Void... params) { try { if (user.getPushIdentity() != null) { GcmPubSub gcmPubStub = GcmPubSub.getInstance(context); gcmPubStub.unsubscribe(user.getPushIdentity(), getTopicName(CHAT_TOPICS_PATH, chatRoomKey)); } } catch (Exception exception) { Log.e(TAG, "unregisterToChatRoomTopic ", exception); } return null; } }.execute(); }
private void subscribeToTopics(JSONArray topics, String registrationToken) { if (topics != null) { String topic = null; for (int i=0; i<topics.length(); i++) { try { topic = topics.optString(i, null); if (topic != null) { Log.d(LOG_TAG, "Subscribing to topic: " + topic); GcmPubSub.getInstance(getApplicationContext()).subscribe(registrationToken, "/topics/" + topic, null); } } catch (IOException e) { Log.e(LOG_TAG, "Failed to subscribe to topic: " + topic, e); } } } }
private void unsubscribeFromTopics(JSONArray topics, String registrationToken) { if (topics != null) { String topic = null; for (int i=0; i<topics.length(); i++) { try { topic = topics.optString(i, null); if (topic != null) { Log.d(LOG_TAG, "Unsubscribing to topic: " + topic); GcmPubSub.getInstance(getApplicationContext()).unsubscribe(registrationToken, "/topics/" + topic); } } catch (IOException e) { Log.e(LOG_TAG, "Failed to unsubscribe to topic: " + topic, e); } } } }
/** * Subscribes the client to a specific topic. * /topics/ prefix is not necessary. * * @param topic The topic to subscribe to * * @return A task that can resolved upon subscribing. */ public Task<Void> subscribeToTopic(final String topic) { final GcmPubSub pubSub = GcmPubSub.getInstance(getContext()); final String topicKey = String.format("/topics/%s", topic); final InstanceID instanceId = InstanceID.getInstance(getContext()); final TaskCompletionSource<Void> future = new TaskCompletionSource<>(); getRegistrationToken(instanceId, _info.getSenderId()).addOnCompleteListener(new OnCompleteListener<String>() { @Override public void onComplete(@NonNull final Task<String> task) { if (!task.isSuccessful()) { future.setException(task.getException()); } new AsyncTask<Object, Integer, String>() { @Override protected String doInBackground(final Object[] ignored) { try { pubSub.subscribe(task.getResult(), topicKey, null); } catch (final IOException e) { Log.e(TAG, "Error subscribing to " + topicKey, e); future.setException(e); return null; } future.setResult(null); return null; } }.execute(); } }); return future.getTask(); }
/** * Subscribes the client to a specific topic. * /topics/ prefix is not necessary. * * @param topic The topic to unsubscribe from * * @return A task that can resolved upon subscribing. */ public Task<Void> unsubscribeFromTopic(final String topic) { final GcmPubSub pubSub = GcmPubSub.getInstance(getContext()); final String topicKey = String.format("/topics/%s", topic); final InstanceID instanceId = InstanceID.getInstance(getContext()); final TaskCompletionSource<Void> future = new TaskCompletionSource<>(); getRegistrationToken(instanceId, _info.getSenderId()).addOnCompleteListener(new OnCompleteListener<String>() { @Override public void onComplete(@NonNull final Task<String> task) { if (!task.isSuccessful()) { future.setException(task.getException()); } new AsyncTask<Object, Integer, String>() { @Override protected String doInBackground(final Object[] ignored) { try { pubSub.unsubscribe(task.getResult(), topicKey); } catch (final IOException e) { Log.e(TAG, "Error unsubscribing from " + topicKey, e); future.setException(e); return null; } future.setResult(null); return null; } }.execute(); } }); return future.getTask(); }
private void subscribeTopics(String token){ GcmPubSub pubSub = GcmPubSub.getInstance(this); try{ Log.i(TAG, "Subscribing to " + TOPIC_NAME); pubSub.subscribe(token, "/topics/" + TOPIC_NAME, null); Log.i(TAG, "Subscribed to " + TOPIC_NAME + " with success"); } catch (Exception e){ Log.e(TAG,"Failed to subscribe to " + TOPIC_NAME,e); } }
/** * Subscribe to any GCM topics of interest, as defined by the TOPICS constant. * * @param token GCM token * @throws IOException if unable to reach the GCM PubSub service */ // [START subscribe_topics] private void subscribeTopics(String token) throws IOException { GcmPubSub pubSub = GcmPubSub.getInstance(this); for (String topic : TOPICS) { pubSub.subscribe(token, "/topics/" + topic, null); } }
/** * Subscribe to any GCM topics of interest, as defined by the TOPICS constant. * * @param token GCM token * @throws IOException if unable to reach the GCM PubSub service */ // [START subscribe_topics] private void subscribeTopics(String token) throws IOException { GcmPubSub pubSub = GcmPubSub.getInstance(this); for (String topic : TOPICS) { pubSub.subscribe(token, TOPICS_SERVICE + topic, null); } }
/** * Subscribe to any GCM topics of interest, as defined by the TOPICS constant. * * @param token GCM token * @throws IOException if unable to reach the GCM PubSub service */ private void subscribeTopics(Context context, String token) throws IOException { GcmPubSub pubSub = GcmPubSub.getInstance(context); for (String topic : TOPICS) { pubSub.subscribe(token, "/topics/" + topic, null); } }
/** * subscribe user to topics in gcm * * @param token */ private void subscribeTopics(String token){ GcmPubSub pubSub = GcmPubSub.getInstance(this); for(String topic : TOPICS){ try { pubSub.subscribe(token, "/topics/" + topic, null); } catch (IOException e) { FirebaseCrash.report(e); } } }
/** * Subscribe to any GCM topics of interest, as defined by the TOPICS constant. * * @param token GCM token * @throws IOException if unable to reach the GCM PubSub service */ // [START subscribe_topics] private void subscribeTopics(String token) throws IOException { for (String topic : TOPICS) { GcmPubSub pubSub = GcmPubSub.getInstance(this); pubSub.subscribe(token, "/topics/" + topic, null); } }
/** * Subscribe to any GCM topics of interest, as defined by the TOPICS constant. * * @param token GCM token * @throws IOException if unable to reach the GCM PubSub service */ private void subscribeTopics(String token) throws IOException { GcmPubSub pubSub = GcmPubSub.getInstance(this); for (String topic : TOPICS) { pubSub.subscribe(token, "/topics/" + topic, null); } }
/** * * @param senderId the project id used by the app's server * @param gcmToken the registration token obtained by registering * @param topic the topic to subscribe to * @param extras bundle with extra parameters */ public void subscribeTopic(final String senderId, final String gcmToken, final String topic, final Bundle extras) { new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(Void... params) { try { GcmPubSub.getInstance(mContext).subscribe(gcmToken, topic, extras); mLogger.log(Log.INFO, "topic subscription succeeded." + "\ngcmToken: " + gcmToken + "\ntopic: " + topic + "\nextras: " + extras); // Save the token in the address book Sender entry = mSenders.getSender(senderId); if (entry == null) { mLogger.log(Log.ERROR, "Could not subscribe to topic, missing sender id"); return null; } entry.topics.put(topic, true); mSenders.updateSender(entry); } catch (IOException | IllegalArgumentException e) { mLogger.log(Log.INFO, "topic subscription failed." + "\nerror: " + e.getMessage() + "\ngcmToken: " + gcmToken + "\ntopic: " + topic + "\nextras: " + extras); } return null; } }.execute(); }
/** * * @param senderId the project id used by the app's server * @param gcmToken the registration token obtained by registering * @param topic the topic to unsubscribe from */ public void unsubscribeTopic(final String senderId, final String gcmToken, final String topic) { new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(Void... params) { try { GcmPubSub.getInstance(mContext).unsubscribe(gcmToken, topic); mLogger.log(Log.INFO, "topic unsubscription succeeded." + "\ngcmToken: " + gcmToken + "\ntopic: " + topic); // Save the token in the address book Sender entry = mSenders.getSender(senderId); if (entry == null) { mLogger.log(Log.ERROR, "Could not save token, missing sender id"); return null; } entry.topics.put(topic, false); mSenders.updateSender(entry); } catch (IOException | IllegalArgumentException e) { mLogger.log(Log.INFO, "topic unsubscription failed." + "\nerror: " + e.getMessage() + "\ngcmToken: " + gcmToken + "\ntopic: " + topic); } return null; } }.execute(); }
@Override protected void onHandleIntent(Intent intent) { SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences(this); try { // Just in case that onHandleIntent has been triggered several times in short // succession. synchronized (TAG) { InstanceID instanceID = InstanceID.getInstance(this); String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); Log.d(TAG, "GCM registration token: " + token); // Register to the server and subscribe to the topic of interest. sendRegistrationToServer(token); // The list of topics we can subscribe to is being implemented within the server. GcmPubSub.getInstance(this).subscribe(token, "/topics/newclient", null); final SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putBoolean(RegistrationConstants.SENT_TOKEN_TO_SERVER, true); editor.putString(RegistrationConstants.TOKEN, token); editor.apply(); } } catch (IOException e) { Log.d(TAG, "Failed to complete token refresh", e); sharedPreferences.edit().putBoolean(RegistrationConstants. SENT_TOKEN_TO_SERVER, false).apply(); } Intent registrationComplete = new Intent(RegistrationConstants.REGISTRATION_COMPLETE); LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete); }
public static void subscribeBroadcasts(Context context) throws IOException { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); String token = prefs.getString(SettingsFragment.PREF_GCM_TOKEN, null); if (token == null) { Log.i(TAG, "Subscribe broadcasts: no token"); return; } String topic = "/topics/broadcasts"; GcmPubSub pubSub = GcmPubSub.getInstance(context); pubSub.subscribe(token, topic, null); Log.i(TAG, "Subscribed to " + topic); }
/** * Subscribe to any GCM topics of interest, as defined by the TOPICS constant. * * @param token GCM token * @throws IOException if unable to reach the GCM PubSub service */ // [START subscribe_topics] private void unsubscribeTopics(String token) throws IOException { GcmPubSub pubSub = GcmPubSub.getInstance(this); for (String topic : TOPICS) { pubSub.unsubscribe(token, "/topics/" + topic); Toast.makeText(getApplicationContext(),"Unregistered "+topic,5).show(); } }
public void registerToChatRoomTopics(String pushIdentity, User user) { try { Set<String> chatRooms = user.getChatRooms().keySet(); for (String chatRoom : chatRooms) { GcmPubSub gcmPubSub = GcmPubSub.getInstance(context); gcmPubSub.subscribe(pushIdentity, getTopicName(CHAT_TOPICS_PATH, chatRoom), null); } } catch (Exception exception) { Log.e(TAG, "registerToChatRoomTopics ", exception); } }
@Kroll.method public void unsubscribe(final HashMap options) { // unsubscripe from a topic final String senderId = (String) options.get("senderId"); final String topic = (String) options.get("topic"); final KrollFunction callback = (KrollFunction) options.get("callback"); if (topic == null || !topic.startsWith("/topics/")) { Log.e(LCAT, "No or invalid topic specified, should start with /topics/"); } if (senderId != null) { new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(Void... params) { try { String token = getToken(senderId); if (token != null) { GcmPubSub.getInstance(TiApplication.getInstance()).unsubscribe(token, topic); if (callback != null) { // send success callback HashMap<String, Object> data = new HashMap<String, Object>(); data.put("success", true); data.put("topic", topic); data.put("token", token); callback.callAsync(getKrollObject(), data); } } else { sendError(callback, "Cannot unsubscribe from topic " + topic); } } catch (Exception ex) { sendError(callback, "Cannot unsubscribe from topic " + topic + ": " + ex.getMessage()); } return null; } }.execute(); } }