karte_notification 0.0.1 copy "karte_notification: ^0.0.1" to clipboard
karte_notification: ^0.0.1 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
    final dynamic data = message['data'];
    try {
      var karteNotification = await krt.Notification.create(message);
      print("karte notification: $karteNotification");
      if (karteNotification != null) {
        karteNotification.handleForAndroid();
      }
    } catch (e) {
      print("exception: $e");
    }
  }

  if (message.containsKey('notification')) {
    // Handle notification message
    final dynamic notification = message['notification'];
  }

  // Or do other work.
}

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

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

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

    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        // Called when received notification on foreground
        print("onMessage: $message");
        setState(() {
          _logText += "\nonMessage";
        });
        try {
          var karteNotification = await krt.Notification.create(message);
          print("karte notification: $karteNotification");
          if (karteNotification != null) {
            karteNotification.handleForAndroid();
          }
        } catch (e) {
          print("exception: $e");
        }
      },
      onBackgroundMessage: Platform.isIOS ? null : myBackgroundMessageHandler,
      onLaunch: (Map<String, dynamic> message) async {
        // Called when app launch by tap notification on iOS
        print("onLaunch: $message");
        setState(() {
          _logText += "\nonLaunch";
        });
        try {
          var karteNotification = await krt.Notification.create(message);
          print("karte notification: $karteNotification");
          if (karteNotification != null) {
            karteNotification.handleForIOS();
          }
        } catch (e) {
          print("exception: $e");
        }
      },
      onResume: (Map<String, dynamic> message) async {
        // Called when app resume by tap notification on iOS
        print("onResume: $message");
        setState(() {
          _logText += "\nonResume";
        });
        try {
          var karteNotification = await krt.Notification.create(message);
          print("karte notification: $karteNotification");
          if (karteNotification != null) {
            karteNotification.handleForIOS();
          }
        } catch (e) {
          print("exception: $e");
        }
      },
    );
    _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);
      setState(() {
        _homeScreenText = "Push Messaging token: $token";
      });
    });
    _firebaseMessaging.getToken().then((String token) {
      assert(token != null);
      krt.Notification.registerFCMToken(token);
      setState(() {
        _homeScreenText = "Push Messaging 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

flutter, karte_core

More

Packages that depend on karte_notification