Commit 2e3e5977 authored by Paweł Rąbel's avatar Paweł Rąbel 💬

Merge branch 'add-function-documentation' into 'master'

Add function documentation



See merge request !2
parents 5f4d5f42 8d4f1b69
Pipeline #2025 skipped
TODO:
\ No newline at end of file
## AppuniteGCM
Take advantage of RxJava in handling GCM notifications.
## Download
Coming soon.
## Inital configuration
At first configuration file has to be downloaded and added to the project. Please read https://developers.google.com/cloud-messaging/android/client for more informations.
## Examples
##### Basic configuration
Android application has to be registered with GCM in order to receive messages. This can be done by performing initialize method which is presented below.
```java
final AppuniteGcm appuniteGcm = AppuniteGcm.initialize(this, getString(R.string.gcm_defaultSenderId));
```
Once the initalization succeed registration token will be returned and now we can make use of it.
```java
appuniteGcm
.registerGcmTokenObservable()
.subscribe(new Action1<String>() {
@Override
public void call(String token) {
// Do something with token.
}
});
```
Now we can simply process notification data by extracting it from Bundle.
```java
appuniteGcm
.getPushBundleObservable()
.map(new Func1<Bundle, String>() {
@Override
public String call(Bundle bundle) {
return bundle.getStri@[]()ng("message");
}
})
.subscribe(NotificationHelper.sendNotificationAction(this));
```
##### Test configuration
In the sample application there is a button which allows us to send exemplary push notification to the server. In return server will respond with the push notification addressed to our device.
```java
gcmTestButton = (GcmTestButton) findViewById(R.id.gcm_test_button);
gcmTestButton.registerGcmPushHandler(new GcmPushHandler() {
@Override
public String getServerKey() {
return "yOuR_SErVer_KEy";
}
@Override
public String getGcmPushData() {
return "{\n" +
" \"message\": \"My Message\",\n" +
" \"time\": \"15:10\"\n" +
" }";
}
});
```
If the reference to listener is no needed anymore it can be removed by performing following method.
```java
gcmTestButton.unregisterGcmPushHandler();
```
License
--------
Copyright [2015] [Paweł Rąbel <pawel.rable@appunite.com>]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
[comment]: <> (Documentation prepared by Robert Strzępka <robert.strzepka@gmail.com>)
......@@ -26,6 +26,9 @@ import rx.schedulers.Schedulers;
import rx.subjects.BehaviorSubject;
import rx.subjects.PublishSubject;
/**
* Class used to register application to Cloud Messaging and handling push notification.
*/
public class AppuniteGcm {
private static final String KEY_PUSH_TOKEN_PREFERENCES = "appuniteGcmPushTokenKey";
private static final String TAG = "AppuniteGCM";
......@@ -46,6 +49,10 @@ public class AppuniteGcm {
return instance;
}
/**
* Obtain instance of AppuniteGcm class
* @return Instance of a AppuniteGcm
*/
@NonNull
public static AppuniteGcm getInstance() {
if (instance == null) {
......@@ -109,7 +116,7 @@ public class AppuniteGcm {
subscriber.onError(new NullPointerException("push token is null"));
return;
}
final String gcmDate = buildGcmDate(pushToken, gcmPushData);
final String gcmDate = buildGcmData(pushToken, gcmPushData);
final RequestBody body = RequestBody.create(MediaType.parse("application/json"),
gcmDate);
final Request request = new Request.Builder()
......@@ -147,7 +154,7 @@ public class AppuniteGcm {
* Toggle whether debug logging is enabled.
* <p>
* <b>WARNING:</b> Enabling this will result in excessive object allocation. This should be only
* be used for debugging purposes.
* used for debugging purposes.
*/
public AppuniteGcm loggingEnabled(boolean enabled) {
LoggingHelper.setLoggingIsEnabled(enabled);
......@@ -162,18 +169,30 @@ public class AppuniteGcm {
return sharedPreferences.getString(KEY_PUSH_TOKEN_PREFERENCES, null);
}
/**
* Preparing a GCM message in JSON form
* @param pushToken token retrieved after initialization.
* @param pushData content of the message
* @return JSON String with the properly formatted message.
*/
@NonNull
private String buildGcmDate(@NonNull String pushToken, @NonNull String pushData) {
private String buildGcmData(@NonNull String pushToken, @NonNull String pushData) {
return String.format("{ \"data\": %s,\n" +
" \"to\" : \"%s\"\n" +
"}\n", pushData, pushToken);
}
/**
* Returns PublishSubject that trigger send action
*/
@NonNull
PublishSubject<GcmPushDataWithServerKey> getSendPushObserver() {
return sendPushSubject;
}
/**
* Returns Observer triggered when notification data arrives
*/
@NonNull
Observer<Bundle> getBundlePublishObserver() {
return bundlePublishSubject;
......
......@@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0-alpha1'
classpath 'com.android.tools.build:gradle:2.0.0-alpha3'
classpath 'com.google.gms:google-services:1.5.0-beta2'
// NOTE: Do not place your application dependencies here; they belong
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment