CPN


Source link: https://github.com/Tengio/cpn

CPN

This is a small library that wraps Cloud Messaging solution to make it easier to implement it or just as an example. It also add the ability to consume notification if the app is running at the moment of receiving the notification without additional work.

Version

Current version uses Firebase Cloud Messaging 9.6.0.

Old GCM version can be see at gcm tag.

Version will follows google play services version so that it is going to ve even easier to implement.

Change Log:

9.6.0 :

  • Firebase dependency updated to 9.6.0

9.4.0 :

  • Firebase cloud messaging

9.2.1 is last version using GCM.

9.2.1 :

  • Look like there is there is a new field in the google services json file :
"api_key": [ {
 "current_key": "" 
}
 ] 

without you get "Missing api_key/current key with Google Services 3.0.0"

  • firebase is added by google play services, this add more dependencies which may require you to add multiDex support
defaultConfig {

  multiDexEnabled true 
}
 
  • note play service 3.0.0 force you to use firebase 9.0.0. To avoid duplication with 9.2.1, cpn library manually add firebase 9.2.1.

8.4.0 :

  • 8.4.0 Google Play Services 8.4.0.

HOW TO

Dependencies

dependencies {

  ...
  compile 'com.tengio:cpn:latest_version' 
}
 

You by adding cpn library dependency you will automatically get the following dependencies:

Gradle Plugins

In the root build.gradle file add:

buildscript {
 ...
  dependencies {

...

classpath 'com.google.gms:google-services:3.0.0'
  
}
 
}
 

In the app build.gradle at the very bottom of the file (this it is probably just a temporary bug in google play services plugin) place:

apply plugin: 'com.google.gms.google-services' 

Google configuration file

Go to https://developers.google.com/mobile/add get the json for you add with google messaging service enabled. Add Json to the app root folder.

RegistrationService

Your app need to expose a registration service where it is possible to send the token to your server implementation.

public class RegistrationService extends CpnRegistrationService {

@Override
  protected void onTokenReady(String token) {

//TODO send the token to your server base your logic on alreadyRegistered flag
  
}
 
}

Don't forget to add the registration service to the manifest

<service android:name="com.tengio.notifications.RegistrationService">
  <intent-filter>

<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
  </intent-filter> </service>

NotificationService

If you want to show push notification while the app is not active :

public class NotificationService extends CpnNotificationService<PushNotification> {

@Override
  protected void showNotification(PushNotification pushNotification) {

Intent intent = new Intent(this, MainActivity.class);

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

 Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)

  .setSmallIcon(R.mipmap.ic_launcher)

  .setContentTitle(getString(R.string.app_name))

  .setContentText(pushNotification.getMessage())

  .setAutoCancel(true)

  .setSound(defaultSoundUri)

  .setContentIntent(pendingIntent);

 NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0, notificationBuilder.build());

  
}

@Override
  protected PushNotification getPushObject(RemoteMessage remoteMessage) {

String message = remoteMessage.getData().get("message");

return new PushNotification(message);

  
}
 
}

Also you need to add it to the manifest:

<service android:name="com.tengio.notifications.NotificationService">
  <intent-filter>

<action android:name="com.google.firebase.MESSAGING_EVENT"/>
  </intent-filter> </service>

Consume Notification in while app running

If you want to consume notifications while app is running in an activity you can easily do that:

private CpnInAppReceiver<PushNotification> cpnReceiver = new CpnInAppReceiver<>();
  @Override protected void onResume() {

  super.onResume();

  cpnReceiver.register(this, new CpnInAppListener<PushNotification>() {

@Override

public void onReceived(PushNotification notification) {

 message.setText(notification.getMessage());

}

  
}
);
 
}
  @Override protected void onPause() {

  cpnReceiver.unregister(this);

  super.onPause();
 
}

How to test Firebase Cloud Messaging

From the firebase console you can easily send notification to a specific application.

Library updates

We use bintray to deploy changes to jcenter. To deploy a new version make sure to define BINTRAY_USER and BINTRAY_KEY variables. Then run:

gradle bintrayUpload 

Resources

A smart seek bar for android with multiple junction points at random seek position.

Fastest way to run your gradle tasks.

RubberStamp is an Android library that makes it easy for you to add a watermark to your images.

Features

  • Add a watermark to your images.
  • It can be text or another image.
  • Multiple ways and features to customize how the watermark looks including attributes like font, color, opacity, size, shadow properties, etc.
  • Flexible API that has multiple pre-defined options to get started quickly.
  • Ability to tile your watermark across an image.

This example describes how use Espresso in Android-Kotlin using MVP, Dagger2, Retrofit.

A set of RxJava2 Extensions to get/set values of SharedPreferences in a super simple way.

Tempo is a Kotlin library for Android to get the current time from multiple sources: SNTP, GPS; or your own time source.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes