com.google.android.gms.gcm.GcmPubSub Java Examples
The following examples show how to use
com.google.android.gms.gcm.GcmPubSub.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GcmService.java From BackPackTrackII with GNU General Public License v3.0 | 6 votes |
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); }
Example #2
Source File: RegistrationIntentService.java From newsApp with Apache License 2.0 | 5 votes |
/** * 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); } }
Example #3
Source File: PubSubHelper.java From gcm with Apache License 2.0 | 5 votes |
/** * * @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(); }
Example #4
Source File: PubSubHelper.java From gcm with Apache License 2.0 | 5 votes |
/** * * @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(); }
Example #5
Source File: RegistrationIntentService.java From friendlyping with Apache License 2.0 | 5 votes |
@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); }
Example #6
Source File: GcmService.java From BackPackTrackII with GNU General Public License v3.0 | 5 votes |
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); }
Example #7
Source File: RegistrationIntentService.java From google-services with Apache License 2.0 | 5 votes |
/** * 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); } }
Example #8
Source File: GCMModule.java From gcmpush with Apache License 2.0 | 5 votes |
@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(); } }
Example #9
Source File: GCMRegistration.java From redalert-android with Apache License 2.0 | 5 votes |
public static void registerForPushNotifications(Context context) throws Exception { // Make sure we have Google Play Services if (!GooglePlayServices.isAvailable(context)) { // Throw exception throw new Exception(context.getString(R.string.noGooglePlayServices)); } // Get instance ID API InstanceID instanceID = InstanceID.getInstance(context); // Get a GCM registration token String token = instanceID.getToken(GCMGateway.SENDER_ID, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); // Log to logcat Log.d(Logging.TAG, "GCM registration success: " + token); // Get GCM PubSub handler GcmPubSub pubSub = GcmPubSub.getInstance(context); // Subscribe to alerts topic // (limited to 1M subscriptions app-wide - think about how to scale this when the time comes) pubSub.subscribe(token, GCMGateway.ALERTS_TOPIC, null); // Log it Log.d(Logging.TAG, "GCM subscription success: " + GCMGateway.ALERTS_TOPIC); // Prepare an object to store and send the registration token to our API RegistrationRequest register = new RegistrationRequest(token, API.PLATFORM_IDENTIFIER); // Send the request to our API HTTP.post(API.API_ENDPOINT + "/register", Singleton.getJackson().writeValueAsString(register)); // Persist it locally saveRegistrationToken(context, token); }
Example #10
Source File: PubSubService.java From easygoogle with Apache License 2.0 | 4 votes |
@Override public void onCreate() { super.onCreate(); mGcmPubSub = GcmPubSub.getInstance(this); }
Example #11
Source File: GCMPubSubHelper.java From OPFPush with Apache License 2.0 | 4 votes |
private GCMPubSubHelper(@NonNull final Context context) { this.gcmPubSub = GcmPubSub.getInstance(context); }
Example #12
Source File: RegistrationIntentService.java From gcm-android-client with Apache License 2.0 | 3 votes |
/** * Subscribe to any GCM topics of interest, as defined by the TOPICS constant. * * @param token GCM token * @param topics a collection of topics which the app wants to subscribe * @throws IOException if unable to reach the GCM PubSub service */ private void subscribeTopics(String token, final String [] topics) throws IOException { GcmPubSub pubSub = GcmPubSub.getInstance(this); for (String topic : topics) { pubSub.subscribe(token, topic, null); } }
Example #13
Source File: RegistrationIntentService.java From gcm-android-client with Apache License 2.0 | 3 votes |
/** * Un-Subscribe to any GCM topics of interest. * * @param token GCM token * @param topics The list of topics to un-subscribe from * @throws IOException if unable to reach the GCM PubSub service */ private void unsubscribeTopics(String token, final String[] topics) throws IOException{ GcmPubSub pubSub = GcmPubSub.getInstance(this); for (String topic : topics) { pubSub.unsubscribe(token, topic); } }