karte_notification 0.1.0 copy "karte_notification: ^0.1.0" to clipboard
karte_notification: ^0.1.0 copied to clipboard

outdated

Flutter plugin for KARTE Notification.

example/lib/main.dart

import 'dart:async';
import 'dart:io';

import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:karte_core/karte_core.dart';
import 'package:karte_notification/karte_notification.dart' as krt;

void main() {
  runApp(MyApp());
}

Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) async {
  // Called when received notification on background only Android
  print('myBackgroundMessageHandler $message');
  if (message.containsKey('data')) {
    // Handle data message
    var karteNotification = await krt.Notification.create(message);
    print("karte notification: $karteNotification");
    if (karteNotification != null) {
      karteNotification.handleForAndroid();
    }
  }
}

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

class _MyAppState extends State<MyApp> {
  String _homeScreenText = "Waiting for token...";
  String _logText = "";
  final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();

  void updateState({String log, String token}) {
    if (!mounted) return;
    setState(() {
      if (log != null) _logText += log;
      if (token != null) _homeScreenText = "Push Messaging token: $token";
    });
  }

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

    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        // Called when received notification on foreground
        print("onMessage: $message");
        updateState(log: "\nonMessage");
        var karteNotification = await krt.Notification.create(message);
        print("karte notification: $karteNotification");
        if (karteNotification != null) {
          karteNotification.handleForAndroid();
        }
      },
      onBackgroundMessage: Platform.isIOS ? null : myBackgroundMessageHandler,
      onLaunch: (Map<String, dynamic> message) async {
        // Called when app launch by tap notification on iOS
        print("onLaunch: $message");
        updateState(log: "\nonLaunch");
        var karteNotification = await krt.Notification.create(message);
        print("karte notification: $karteNotification");
        if (karteNotification != null) {
          karteNotification.handleForIOS();
        }
      },
      onResume: (Map<String, dynamic> message) async {
        // Called when app resume by tap notification on iOS
        print("onResume: $message");
        updateState(log: "\nonResume");
        var karteNotification = await krt.Notification.create(message);
        print("karte notification: $karteNotification");
        if (karteNotification != null) {
          karteNotification.handleForIOS();
        }
      },
    );
    _firebaseMessaging.requestNotificationPermissions(
        const IosNotificationSettings(
            sound: true, badge: true, alert: true, provisional: true));
    _firebaseMessaging.onIosSettingsRegistered
        .listen((IosNotificationSettings settings) {
      print("Settings registered: $settings");
    });
    _firebaseMessaging.onTokenRefresh.listen((String token) {
      print("onTokenRefreshed: $token");
      krt.Notification.registerFCMToken(token);
      updateState(token: token);
    });
    _firebaseMessaging.getToken().then((String token) {
      assert(token != null);
      krt.Notification.registerFCMToken(token);
      updateState(token: token);
      print(_homeScreenText);
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('KARTE Notification example app'),
        ),
        body: Center(
          child: Column(
              // mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(_homeScreenText),
                RaisedButton(
                  onPressed: () {
                    Tracker.view("push_text");
                  },
                  child: Text("View"),
                ),
                Text(_logText),
              ]),
        ),
      ),
    );
  }
}
0
likes
0
pub points
80%
popularity

Publisher

unverified uploader

Flutter plugin for KARTE Notification.

Homepage
Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

firebase_messaging, flutter, karte_core

More

Packages that depend on karte_notification