azure_notificationhubs_flutter 0.0.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 78

Microsoft Azure Notification Hubs wrapper for Flutter #

This is a work in progress. View todos in our issues page for more information. Please submit PR!

Microsoft Azure Notification Hubs provide a multiplatform, scaled-out push infrastructure that enables you to send mobile push notifications from any backend (in the cloud or on-premises) to any mobile platform. To learn more, visit the Developer Center.

Getting Started #

If you are new to Notification Hubs, you can get started by following the tutorials to push notifications to your apps.

Configuration #

Android Integration

To integrate your plugin into the Android part of your app, follow these steps:

  1. Using the Firebase Console add an Android app to your project: Follow the assistant, download the generated google-services.json file and place it inside android/app.

  2. Add your notification hub name and connection string to android/app/src/main/AndroidManifest.xml.

<meta-data 
    android:name="NotificationHubName"
    android:value="" />
<meta-data 
    android:name="NotificationHubConnectionString"
    android:value="Endpoint=..." />
  1. Add permissions to android/app/src/main/AndroidManifest.xml.
  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
  <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

iOS Integration

To integrate your plugin into the iOS part of your app, follow these steps:

  1. Generate the certificates required by Apple for receiving push notifications following this guide in the Azure docs.

  2. Register your app for push notifications with Apple following this guide in the Azure docs.

  3. In Xcode, select Runner in the Project Navigator. In the Capabilities Tab turn on Push Notifications and Background Modes, and enable Background fetch and Remote notifications under Background Modes.

  4. Add your notification hub name and connection string to Info.plist.

NotificationHubName
NotificationHubConnectionString

Dart/Flutter Integration

From your Dart code, you need to import the plugin and instantiate it:

import 'package:azure_notificationhubs_flutter/azure_notificationhubs_flutter.dart';

final AzureNotificationhubsFlutter _anh = AzureNotificationhubsFlutter();

Requesting permissions on iOS is managed by the plugin and the request will bring up a permissions dialog for the user to confirm on iOS on app launch.

Afterwards, you can listen to the notifications and write your own logic to handle extra data payloads.

  _anh.configure(
    onLaunch: (Map<String, dynamic> notification) async {
      print('onLaunch: $notification');
    },
    onResume: (Map<String, dynamic> notification) async {
      print('onResume: $notification');
    },
    onMessage: (Map<String, dynamic> notification) async {
      print('onMessage: $notification');
    },
    onToken: (Map<String, dynamic> notification) async {
      print('onToken: $notification');
    },
  );

Payloads #

iOS and Android have different payloads. The entire payload will be sent to dart onResume, onLaunch, onMessage depending on the situation.

iOS Payload #

{
    aps: {
      alert: {
        title: "Hello world",
        body: "by chu"
      },
      badge: 1
    },
    <!-- extra data payload -->
    ...
  }

Android Payload #

{
  "data": {
    "title": "Hello world",
    "body": "to you",
    <!-- extra data payload -->
    ...
  }
}

Subscribing to tags #

Every device is automatically subscribed to device:deviceID. For Android, deviceId is a SHA1 hash of FirebaseCloudMessaging token because it exceeds the length limits of a tag.

Manual subscribing to tags is not supported as it is not my priority right now. Feel free to create a PR.

Why am I receiving silent notifications on android? #

It's a feature, not a bug. In android, sometimes the user does not receive any heads up notifications and the notifications appear silently in the notification drawer. This is because heads up notifications have a built in rate limiting - if the user swipes your heads up notification up (putting it back into the notification tray) or to the side (dismissing it), then the system prevents further heads up notifications for some period of time.

Download Source Code #

To get the source code of our wrapper via git just type:

git clone https://github.com/rswiftoffice/azure-notificationhubs-flutter.git

Contributing #

If you're unsure about anything, just ask -- or submit the issue or pull request anyway. The worst that can happen is you'll be politely asked to change something. We love all friendly contributions.

0.0.1 #

  • Initial release

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:azure_notificationhubs_flutter/azure_notificationhubs_flutter.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  AzureNotificationhubsFlutter _anh;

  @override
  void initState() {
    super.initState();
    _anh.configure(
      onMessage: (Map<String, dynamic> message) async {
        print("onMessage: $message");
        showDialog(
          context: context,
          builder: (context) => AlertDialog(
            content: ListTile(
            title: Text(message['notification']['title']),
            subtitle: Text(message['notification']['body']),
            ),
            actions: <Widget>[
              FlatButton(
                child: Text('Ok'),
                onPressed: () => Navigator.of(context).pop(),
              ),
            ],
          ),
        );
      }
    );
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Text('Running on: $_platformVersion\n'),
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  azure_notificationhubs_flutter: ^0.0.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:azure_notificationhubs_flutter/azure_notificationhubs_flutter.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
67
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
72
Overall:
Weighted score of the above. [more]
78
Learn more about scoring.

We analyzed this package on Jul 2, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform linux

Because of import path [package:azure_notificationhubs_flutter/azure_notificationhubs_flutter.dart] that declares support for platforms: android, ios

Package does not support Flutter platform macos

Because of import path [package:azure_notificationhubs_flutter/azure_notificationhubs_flutter.dart] that declares support for platforms: android, ios

Package does not support Flutter platform web

Because of import path [package:azure_notificationhubs_flutter/azure_notificationhubs_flutter.dart] that declares support for platforms: android, ios

Package does not support Flutter platform windows

Because of import path [package:azure_notificationhubs_flutter/azure_notificationhubs_flutter.dart] that declares support for platforms: android, ios

Package not compatible with SDK dart

because of import path [azure_notificationhubs_flutter] that is in a package requiring null.

Health suggestions

Format lib/azure_notificationhubs_flutter.dart.

Run flutter format to format lib/azure_notificationhubs_flutter.dart.

Maintenance suggestions

The package description is too short. (-18 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8
Dev dependencies
flutter_test