Skip to main content

Push notifications SDK for Flutter (version 1.0.0)

Push notification terms and conditions:

  • The current version of RuStore is installed on the user's device.
  • RuStore app supports push notifications.
  • The RuStore app is allowed to run in the background.
  • User is authorized in RuStore.
  • The signature fingerprint of the app must match the fingerprint added to the RuStore Console.

Implementation example

Checkout example app to learn how to implement push notification SDK.

Connecting to project

Run the command to link the package to the project:

flutter pub add flutter_rustore_push

It will add a line to pubspec.yaml:

pubspec.yaml
dependencies:
flutter_rustore_push: ^0.0.4

Initialization

To initialise the push notification service, add a value to values of your Android project.

<resources>
<string name= "flutter_rustore_push_project" translatable= "false">xxx</string>
</resources>

xxxproject ID from the RuStore Console. To get the project ID on the app page navigate to Push notifications > Projects and copy the value in the Project ID field.

To run the push notification service, add a Application class inherited from FlutterRustoreApplication. Example in Kotlin

package ru.rustore.flutter_rustore_push_example
import ru.rustore.flutter_rustore_push.FlutterRustoreApplication
open class Application: FlutterRustoreApplication() {
}

Specify this class in AndroidManifest.xml:

AndroidManifest.xml
<application
android:label= "flutter_rustore_push_example"
android:name= ".Application"
android:icon= "@mipmap/ic_launcher">
// ...
</application>

ProGuard settings

Add the following rule to configure ProGuard:

-keep public class com.vk.push.** extends android.os.Parcelable

In the android/app/build.gradle file, add the following lines:

android/app/build.gradle
buildTypes {
release {
// ...
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
// ...
}

Push notifications availability check

To check the listed prerequisites, use the RustorePushClient.available() method.

RustorePushClient.available().then((value) {
print("available success: ${value}");
}, onError: (err) {
print("available error: ${err}");
});

Push token methods

Getting user push token

caution

The method will create and return a new push token if the user does not have one.

Once the library has been initialised, you can use the RuStorePushClient.getToken(), method to get the user's current push token.
RustorePushClient.getToken().then((value) {
print("get token success: ${value}" );
}, onError: (err) {
print("get token error: ${err}" );
})

Deleting user push token

After the library has been initialised, the user's current push token can be removed using the RuStorePushClient.deleteToken() method.

RustorePushClient.deleteToken().then(() {
print( "delete success:" );
}, onError: (err) {
print( "delete error: ${err}" );
})

Push token modification events

When the old token becomes invalid, it can be reissued. To know that a new token has been issued, use callback RustorePushClient().

RustorePushClient.onNewToken((value) {
print("on new token success: ${value}");
}, error: (err) {
print("on new token err: ${err}");
});

Push notification methods

Retrieving push notification contents

To get information from a push notification, add a callback RustorePushClient.onMessageReceived().
RustorePushClient.onMessageReceived((value) {
print("on message received success: id=${value.messageId}, data=${value.data}, notification.body: ${value.notification?.body}");
}, error: (err) {
print("on message received error: ${err}");
});

Deleting push notification

Add callback RustorePushClient.onDeletedMessages() to delete a push notification.

RustorePushClient.onDeletedMessages(() {
print( "deleted messages" );
}, error: (err) {
print( "on message received error: ${err}" );
});

Notification structure

class Message {
String? messageId;
int priority;
int ttl;
String? collapseKey;
Map<String?, String?> data;
Notification? notification;
}
  • messageId — unique message ID. It corresponds to an ID of each message.

  • priority — returns the priority value (currently disregarded).

    The following options are now available:

    • 0UNKNOWN.
    • 1HIGH.
    • 2NORMAL.

    < p/>

  • ttl — push notification lifetime of Int type in seconds.

  • from — field which shows the notification's origin: < p/>

    • The field displays the topic name for notifications sent to it.
    • Otherwise it is part of your service token.

    < p/>

  • collapseKey - notification group ID (currently disregarded).

  • data — dictionary to which additional notification data can be passed.

    .

  • rawData — dictionary of data as an array of bytes.

  • notification — notification object.

Структура объекта Notification
class Notification {
String? title;
String? body;
String? channelId;
String? imageUrl;
String? color;
String? icon;
String? clickAction;
}
  • title — notification header.

  • body — notification body
  • channelId — option to create the channel to which notification will be sent. For Android 8.0 or later.

  • imageUrl — direct link to an image to be inserted into the notification. The size of the image must not exceed 1 Mbyte .

    .

  • color — notification colour in HEX format, string. For example, #0077FF.

  • icon — notification icon from res/drawable in a string format that matches the resource name. < p/> For example, res/drawable has an icon small_icon.xml, which is accessible in code via R.drawable.small_icon. For the icon to be displayed in the notification, the server must specify a icon value of small_icon. < p/>

  • clickActionintent action, with which activity is opened when a notification is pressed on..

Creating notification channel

The channel to which the message is sent will be given the following priority:

  • If the push notification has a channelId field, RuStore SDK will send the notification to the specified channel. Your app must create this channel in advance.

  • If there is no channelId field in the push notification, but your app has specified a parameter with a channel in AndroidManifest.xml, the specified channel will be used. Your app must create this channel in advance.

  • If there is no channelId field in the push notification and the default channel is not set in AndroidManifest.xml, RuStore SDK will create a channel and send the notification to it. From now on, all notifications without an explicit channel will be sent to that channel..

See also