SDK

Implementation

If you've completed the steps described in the setup guides, you are now ready to implement our Flutter plugin package for Android in your app. Our library supports Android version 4.0 and up, but is only fully functional from version 4.1 (JellyBean) up to Android 9.0 (Pie).

To start please make sure you have followed the Getting started with Flutter tutorial. Once you have met all the requirements and you have prepared your machine for Flutter development you can then start implementing our plugin.

Install the Plugin Package

Assuming that you've already create a Flutter app, open pubspec.yaml, and add our plugin as a dependency:

1
2
3
4
dependencies:
  flutter:
	sdk: flutter
  notificare_push_lib: ^2.0.0

Then in your terminal, run the following command:

1
flutter pub get

Or click Packages get in IntelliJ's Android Studio.

Add dependencies

After installing the plugin the very first thing you should do is to add the dependencies needed to your build.gradle:

1
2
3
4
5
6
7
8
dependencies {
	testImplementation 'junit:junit:4.12'
	implementation "re.notifica:notificare-location:2.1.1" // Comment out if you are not going to use location services
	implementation "re.notifica:notificare-beacon:2.1.1" // Comment out if you are not going to use beacons (needs notificare-location)
	implementation "re.notifica:notificare-scannable:2.1.1" // Comment out if you are not going to use scannables
	androidTestImplementation 'androidx.test:runner:1.2.0'
	androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

Add an Application

To initialize Notificare, you must create an application class (it should extend FlutterApplication) where you will add the following initialization code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import io.flutter.app.FlutterApplication;
import re.notifica.Notificare;
import re.notifica.flutter.NotificareReceiver;

public class MainApplication extends FlutterApplication {
	@Override
	public void onCreate() {
		super.onCreate();
		Notificare.shared().setDebugLogging(true);
		Notificare.shared().launch(this);
		Notificare.shared().createDefaultChannel();
		Notificare.shared().setIntentReceiver(NotificareReceiver.class);
	}
}

In this example we've named it MainApplication.java, so it's important you change your AndroidManifest.xml accordingly:

1
2
3
4
5
6
7
<application
	android:name=".MainApplication"
	android:label="@string/app_name"
	android:icon="@mipmap/ic_launcher">

	...more code
</application>

Add a Notificare Configuration file

In order to connect your app to Notificare, you must create a file /app/src/main/assets/notificareconfig.properties with the following contents:

1
2
3
4
5
developmentApplicationKey = xxx
developmentApplicationSecret = xxx
productionApplicationKey = xxx
productionApplicationSecret = xxx
production = false

It is recommended that you create at least two different apps in Notificare using separated environments for development and production. For each app you will have a different set of keys, these keys should be copied and pasted in the file shown above. The production property should be changed according to the environment you are building for. You can find your app keys by expanding your app's Settings menu item and clicking in App Keys as shown below:

Menu 33ed03f278

Copy both the Application Key and Application Secret:

Keys cb31323be5

Add Firebase Cloud Messaging

Android Studio makes it easy to quickly import the FCM project you've created previously in your app. Simply expand in Tools menu and click in Firebase:

Android studio tools firebase a3c8b8c253

This will open an Assistant window like the one below:

Android studio firebase assistant 74a700f93c

Simply click in the Set up Firebase Cloud Messaging and follow the guides in the following screen:

Android studio firebase setup 8f59465498

Completing point 1 and 2 will prepare your app with everything needed to use the FCM project you've previously created. You can confirm if everything is created correctly by checking if a file /app/google-services.json was created. This file will have the information about the Sender ID of your FCM project.

Android Manifest

It is also necessary to add some configuration in your AndroidManifest.xml file in order so it can handle notifications properly. Locate your Android Manifest file, it should be at /app/source/main/ and add the following as the main activity:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<activity
	android:name=".MainActivity"
	android:label="@string/app_name"
	android:launchMode="singleTop"
	android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
		<intent-filter>
			<action android:name="android.intent.action.MAIN" />
			<category android:name="android.intent.category.LAUNCHER" />
		</intent-filter>
		<intent-filter>
			<action android:name="re.notifica.intent.action.NotificationOpened" />
			<category android:name="android.intent.category.DEFAULT" />
		</intent-filter>
</activity>

If you want to use a url scheme for your app, add it to your strings resources and set an intent filter to your MainActivity:

1
2
3
4
5
6
<intent-filter>
	  <action android:name="android.intent.action.VIEW" />
	  <category android:name="android.intent.category.DEFAULT" />
	  <category android:name="android.intent.category.BROWSABLE" />
	  <data android:scheme="@string/app_url_scheme" />
  </intent-filter>

In order to handle properly the notifications UI also add the following activity in your AndroidManifest.xml:

1
2
3
4
5
<activity
	android:name="re.notifica.ui.NotificationActivity"
	android:theme="@style/AppTheme.Translucent"
	android:configChanges="keyboardHidden|orientation|screenSize"
	android:hardwareAccelerated="true"/>

Where you would need to define the Translucent style in res/values/styles.xml`:

1
2
3
4
<style name="AppTheme.Translucent" parent="AppTheme">
	<item name="android:windowIsTranslucent">true</item>
	<item name="android:windowBackground">@android:color/transparent</item>
</style>

Implementation

You are now ready to start implementing the native code necessary to make our module work. Open the file lib/main.dart and import our plugin as shown below:

1
import 'package:notificare_push_lib/notificare_push_lib.dart';

Then start by creating a reference to our plugin:

1
2
3
4
5
6
class _MyAppState extends State<MyApp> {

	final NotificarePushLib notificare = NotificarePushLib();

	...more code
}

Now the best way to initialize Notificare is to add the following to the initState method in your app's class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
..more code

class _MyAppState extends State<MyApp> {

	final NotificarePushLib notificare = NotificarePushLib();

	@override
	void initState() {
		super.initState();

		notificare.launch();

		notificare.onEventReceived.listen((NotificareEvent event) {
			if (event.name == "ready") {

			}
		});

		... more code
	}
}

At this point, you have completed the basic setup of our plugin. Keep reading our implementation guides to dive deeper into each features available in our SDK.