Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Firebase Push Notification and Remote Config fo...

Firebase Push Notification and Remote Config for Android

This shows how to get notifications from Firebase at a faster rate and at no cost. Also to change the look and feel of an app without updating the whole app.

Damilola Omoyiwola

October 03, 2017
Tweet

More Decks by Damilola Omoyiwola

Other Decks in Programming

Transcript

  1. build gradle Project Level buildscript { repositories { jcenter() mavenLocal()

    maven { url 'https://maven.google.com' } } dependencies { classpath 'com.google.gms:google-services:3.1.0' } Module: App level dependencies { //Firebase compile 'com.google.firebase:firebase-config:11.2.0' compile 'com.google.firebase:firebase-messaging:11.2.0' } apply plugin: 'com.google.gms.google-services’
  2. AndroidManifest.xml <service android:name=".MyFirebaseMessagingService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service>

    <service android:name=".MyFirebaseInstanceIdService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> </intent-filter> </service>
  3. FirebaseInstanceIdService class public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService { @Override public

    void onTokenRefresh() { String token = FirebaseInstanceId.getInstance().getToken(); Log.d("Token", "Refreshed token: " + token); } }
  4. FirebaseMessagingService class public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public

    void onMessageReceived(RemoteMessage remoteMessage) { super.onMessageReceived(remoteMessage); //Create Notification 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); NotificationCompat.Builder notificationBuilder =(NotificationCompat.Builder) new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.images) .setContentTitle("Message") .setContentText(remoteMessage.getNotification().getBody()) .setAutoCancel(true) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0, notificationBuilder.build()); } }
  5. Default Remote Config Parameters: remote_config_param.xml <defaultsMap> <entry> <key>welcome_text</key> <value>Welcome to

    Firebase RemoteConfig</value> </entry> <entry> <key>welcome_text_color</key> <value>#FFDF1507</value> </entry> <entry> <key>bg_color</key> <value>#000000</value> </entry> </defaultsMap>
  6. Firebase Remote Config Instance Variables //Loading the Config Parameters or

    instance variables firebaseRemoteConfig = FirebaseRemoteConfig.getInstance(); firebaseRemoteConfig.setDefaults(R.xml.remote_config_params); firebaseRemoteConfig.setConfigSettings( new FirebaseRemoteConfigSettings.Builder() .setDeveloperModeEnabled(true) .build() );
  7. Apply Remote Config from Firebase: MainActivity.java private void applyConfig() {

    //Get the widget from XML layout ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.layout); TextView textview = (TextView) findViewById(R.id.textView); //Get the values form Firebase remote configuration String welcomeText = firebaseRemoteConfig.getString("welcome_text"); String welcomeTextColor = firebaseRemoteConfig.getString("welcome_text_color"); String layoutColor = firebaseRemoteConfig.getString("bg_color"); // Set the properties from firebase remote configuration // If any value not set in firebase remote configuration then it gets from default set layout.setBackgroundColor(Color.parseColor(layoutColor)); textview.setText(welcomeText); textview.setTextColor(Color.parseColor(welcomeTextColor)); }