项目作者: OneAfricaMedia

项目描述 :
Android notifications component
高级语言: Java
项目地址: git://github.com/OneAfricaMedia/android-notifications.git
创建时间: 2016-09-30T09:57:11Z
项目社区:https://github.com/OneAfricaMedia/android-notifications

开源协议:

下载


Build Status on Travis:
Release

Notifications

A helper library for use with the OAM cloud messaging microservice

Installation

To use this library in your android project, just simply add the following dependency into your build.gradle

  1. dependencies {
  2. compile 'com.oneafricamedia.android:notifications:0.0.1'
  3. }

Usage

First of all you should have a look at the sourcecode, an example app is provided.

You need to register with Firebase and follow their instructions on setting up your application.

Instead of implementing your own services, you include this library as stated above and extend the ones included:

  1. public class MyAppFcmInstanceIDListenerService extends FcmInstanceIDListenerService {…}
  2. public class MyAppFcmMessageListenerService extends FcmMessageListenerService {…}

Then you register them in your Manifest:

  1. <service android:name=".services.MyFcmInstanceIDListenerService">
  2. <intent-filter>
  3. <action android:name="com.google.firebase.INSTANCE_ID_EVENT" ></action>
  4. </intent-filter>
  5. </service>
  6. <service android:name=".services.MyFcmMessageListenerService">
  7. <intent-filter>
  8. <action android:name="com.google.firebase.MESSAGING_EVENT" ></action>
  9. </intent-filter>
  10. </service>

You can (and should) override the default methods then and use the appropriate methods in the super class:

  1. @Override
  2. public void onTokenRefresh() {
  3. super.onTokenRefresh(new BackendBundle("http://www.site.com/GCM/", "auth", "http://www.site.com/API/", "auth", "0"));
  4. }
  5. @Override
  6. public void onMessageReceived(RemoteMessage message) {
  7. Map<String, Object> map = new HashMap();
  8. Map<String, Class> activityMap = new HashMap<>();
  9. Intent intent = new Intent();
  10. // This is where we prepare our Notification
  11. // Please see the example app within the component sources
  12. // This is where we show it and maybe handle the callback
  13. //super.onMessageReceived(message, map);
  14. super.onMessageReceived(message, map, new FcmNotification.OnCompletionListener() {
  15. @Override
  16. public void onSuccess() {
  17. Log.d("LogTag", "This thing came back OK.");
  18. }
  19. @Override
  20. public void onFailure() {
  21. Log.e("LogTag", "Everything went down the drain!!!");
  22. }
  23. });
  24. }

How to handle events sent by the component

Include EventBus, like so:

  1. dependencies {
  2. compile compile "org.greenrobot:eventbus:3.0.0"
  3. }

Register to the default EventBus channel, like so:

  1. EventBus.getDefault().register(this);

Listen for the corresponding events, like so:

  1. @Subscribe(threadMode = ThreadMode.MAIN)
  2. public void onEvent(MarketingMessageReceived marketingMessageReceived) {
  3. TextView t = (TextView) findViewById(R.id.textViewMain);
  4. t.setText(marketingMessageReceived.message);
  5. }

How to work with a callback

The component comes with a simple callback that you can use to figure out wether a notification was shown correctly, a system message triggered the correct stuff and wether it succeeded or not.
You have to implement FcmNotification.OnCompletionListener in case of Firebase, it works just like an OnClickListener().

Change Logs

v0.0.1

Initial version

License

Apache 2.0