Raix:Push - Support for Android Channels

Shivang Kar
Fasal Engineering
Published in
4 min readDec 8, 2019

Raix:Push plays a major role in helping Fasal to notify Farmers about all the important advisories and information related to their farm via Push Notification. This means sending out a large amount of notifications such as predictions, warnings, tips etc. on a daily basis. But what if your user is not happy with receiving a particular type of notification? For instance, we all have that one whatsApp family group which annoys us but we can not leave that either! That’s where the Notification Channel comes in.

Android introduced the notification-channel functionality starting from Android 8.0, making it mandatory for all notifications to have been assigned a channel. Due to this, you would notice that if you target Android 8.0 and post a notification without specifying a notification channel, the notification does not appear and the system logs an error!

Although this might look like a headache, but in reality it’s quite useful and solves a lot of problems! For instance, your users can now manage the behaviour of notifications and decide for themselves, which ones are futile/intrusive and no longer wants to receive them, rather than blocking the whole app from sending any notification. You can also set the visual and auditory behaviour for a channel, which will get applied to all the notifications in that channel. Check out this video for more information about channels.

Settings for an individual channel. Source.

Before getting started with channels, you need to make changes based on your meteor version. If you are using meteor 1.8.1 or higher, skip the below section.

Four changes you need to do in your project for meteor < 1.8.1

  1. Versions

Your Meteor version must be 1.6.1 or higher.
Update your cordova plugins to the following versions:

phonegap-plugin-push@2.1.3
cordova-plugin-device@1.1.6

2. Build

Delete your local folder inside .meteor or you can reset your project using meteor-reset if your local database is specified with MONGO_URL.

Build your project by running:
meteor --settings your_settings_file.json run android-device
or
meteor build --mobile-settings $settingsFile --verbose ~/yourBuild --server=$server
You will receive an error here (which is expected, move on to the next step):

3. Firebase Config

Get yourgoogle-services.json file by following this.

Place this file at .meteor/local/cordova-build/platforms/android in your project.

4. Changes in Gradle Build

Your build.gradle file will be located at .meteor/local/cordova-build/platforms/android
Add these dependencies below the existing ones:

dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
classpath 'com.google.gms:google-services:4.1.0' //Added
classpath 'com.google.firebase:firebase-core:11.0.1' //Added
}

At the end of this file, add:

configurations.all {
resolutionStrategy {
force 'com.android.support:support-v4:27.1.0'
}
}

configurations {
all*.exclude group: 'com.android.support', module: 'support-v13'
}

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

Now run your project and you should be able to build it successfully.

In case you run into this error:

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'android'.
> Cannot add task ':processDebugGoogleServices' as a task with that name already exists.

remove this line apply plugin: 'com.google.gms.google-services' from build.gradle and run again.

Adding channel

Create the channel on your client side before making a call to Push.Configure:

if (Meteor.isCordova) {
PushNotification.createChannel(
function() {
console.log('Channel created successfully!');
},
function() {
console.log('Error creating channel!');
}, {
id: 'forecastChannel', //Must be unique.
description: 'Weather Forecast',
importance: 3,
sound: 'fasal',
vibration: true
}
);
}

This will create a channel forecastChannel.
The description is how the channel will be visible to the user and the importance controls how interruptive notifications posted to this channel are.

You can send the notifications through above channel by mentioning it in android_channel_id while sending notification from your server:

var notificationPayload = {
from: 'push',
title: 'Rainfall Forecast',
text: 'High chance of rainfall in Mayfair, London. Please bring your umbrella.',
badge: 1,
notId: 123456789,
android_channel_id: 'forecastChannel',
query: {
userId: { $in: recipients }
},
apn: {
sound: "www/application/app/mySound.wav"
},
gcm: {
sound: 'mySound'
}
};
Push.send(notificationPayload);

You can find more details about channels here.

Other Errors

If you have other plugins that uses different version of Firebase (such ascordova-plugin-firebase-analytics) follow this.

This way Fasal manages the priority of notifications and makes sure that its farmers won’t miss out on any of the important advisories.

--

--

Shivang Kar
Fasal Engineering

I’m a Lead Product Engineer at Fasal, where I focus on building Tech/Product that would help farmers in increasing yield, optimize resource utilization etc.