pushportal_package 0.0.6 copy "pushportal_package: ^0.0.6" to clipboard
pushportal_package: ^0.0.6 copied to clipboard

Push notification project.

example/lib/main.dart

// ignore_for_file: avoid_print

import 'dart:async';

import 'package:example2/controller.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:get/get.dart';
import 'package:intl/date_symbol_data_local.dart';
import 'package:pushportal_package/pushportal_package.dart';
import 'app_config.dart';
import 'widgets/alert_button.dart';
import 'widgets/mailbox.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  initializeDateFormatting();
  await PushPortal.instance.initializeApp(
    username: "username",
    password: "password",
    serviceLog: true,
  );
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.yellow,
        fontFamily: 'DBHeaven',
        // dialogTheme: DialogTheme(
        //   alignment: Alignment.center,
        //   contentTextStyle: Theme.of(context).textTheme.bodyLarge,
        // ),
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
      navigatorKey: AppConfig.navigatorKey,
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  late AndroidNotificationChannel channel;
  bool isFlutterLocalNotificationsInitialized = false;

  /// Initialize the [FlutterLocalNotificationsPlugin] package.
  late FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;

  @override
  void initState() {
    super.initState();
    prepareData();
    PushPortal.handleReceivePayload.listen((event) async {
      Get.to(const Mailbox());
    });
  }

  @override
  Widget build(BuildContext context) {
    final appWidth = MediaQuery.of(context).size.width;
    final appHeight = MediaQuery.of(context).size.height;
    return Scaffold(
      backgroundColor: AppConfig.primary_color,
      body: Container(
        margin: const EdgeInsets.only(top: 34),
        width: appWidth,
        height: appHeight,
        decoration: const BoxDecoration(
          image: DecorationImage(
            fit: BoxFit.fitWidth,
            image: AssetImage('assets/images/event_copy_2.jpg'),
          ),
        ),
        child: Stack(
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.only(left: 5, top: 5),
              child: SizedBox(
                width: 50,
                height: 38,
                child: MailboxAlertButton(),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Future<void> prepareData() async {
    await setupFlutterNotifications();
    await AppController.current.prepareAccessToken();
    await AppController.current.registerFirebaseMessagingToken("memberID");
    await AppController.current.getUnreadCount();
    PushPortal.instance.setCallbackShowFlutterNotification((message) {
      print("----\nhandle show flutter notification\n----");
      print("data: ${message.data}");
      RemoteNotification? notification = message.notification;
      AndroidNotification? android = message.notification?.android;
      if (notification != null && android != null && !kIsWeb) {
        flutterLocalNotificationsPlugin.show(
          notification.hashCode,
          notification.title,
          notification.body,
          NotificationDetails(
            android: AndroidNotificationDetails(
              channel.id,
              channel.name,
              channelDescription: channel.description,
              icon: 'launch_background',
            ),
          ),
        );
      }
    });
    PushPortal.instance.setCallbackReceivePayload((message) {
      print("----\ncallback Receive Payload\n----");
    });
    PushPortal.instance.setCallbackInitialMessage((massage) async {
      print("----\ncallback Initial Message\n----");
      WidgetsBinding.instance.addPostFrameCallback((_) async {
        Get.to(const Mailbox());
      });
    });
    setState(() {});
  }

  Future<void> setupFlutterNotifications() async {
    if (isFlutterLocalNotificationsInitialized) {
      return;
    }
    channel = const AndroidNotificationChannel(
      'high_importance_channel', // id
      'High Importance Notifications', // title
      description: 'This channel is used for important notifications.', // description
      importance: Importance.high,
    );
    flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

    /// Create an Android Notification Channel.
    /// We use this channel in the `AndroidManifest.xml` file to override the
    /// default FCM channel to enable heads up notifications.
    await flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
        ?.createNotificationChannel(channel);
    isFlutterLocalNotificationsInitialized = true;
  }
}
1
likes
125
points
62
downloads

Publisher

unverified uploader

Weekly Downloads

Push notification project.

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

dio, firebase_core, firebase_messaging, flutter

More

Packages that depend on pushportal_package