pushportal_package 0.0.3+4 pushportal_package: ^0.0.3+4 copied to clipboard
Push notification project.
example/lib/main.dart
// ignore_for_file: avoid_print
import 'dart:async';
import 'package:example/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 'package:uuid/uuid.dart';
import 'app_config.dart';
import 'app_data.dart';
import 'widgets/alert_button.dart';
import 'widgets/mailbox.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
initializeDateFormatting();
await PushPortal.instance.initializeApp(
username: username,
password: password,
);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
final textTheme = const TextTheme(
bodyText1: TextStyle(
fontSize: 16.0, fontWeight: FontWeight.normal, color: Colors.black),
bodyText2: TextStyle(
fontSize: 18.0, fontWeight: FontWeight.normal, color: Colors.black),
headline1: TextStyle(
fontSize: 36.0, fontWeight: FontWeight.normal, color: Colors.black),
headline2: TextStyle(
fontSize: 34.0, fontWeight: FontWeight.w700, color: Colors.black),
headline3: TextStyle(
fontSize: 32.0, fontWeight: FontWeight.normal, color: Colors.black),
headline4: TextStyle(
fontSize: 30.0, fontWeight: FontWeight.normal, color: Colors.black),
headline5: TextStyle(
fontSize: 24.0, fontWeight: FontWeight.w500, color: Colors.black),
headline6: TextStyle(
fontSize: 24.0, fontWeight: FontWeight.w500, color: Colors.black),
subtitle1: TextStyle(
fontSize: 20.0, fontWeight: FontWeight.w500, color: Colors.black),
subtitle2: TextStyle(
fontSize: 18.0, fontWeight: FontWeight.normal, color: Colors.black),
caption: TextStyle(
fontSize: 16.0, fontWeight: FontWeight.normal, color: Colors.black),
button: TextStyle(
fontSize: 18.0, fontWeight: FontWeight.w500, color: Colors.white),
overline: TextStyle(
fontSize: 14.0, fontWeight: FontWeight.normal, color: Colors.black),
);
@override
Widget build(BuildContext context) {
return GetMaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.yellow,
textTheme: textTheme,
fontFamily: 'DBHeaven',
dialogTheme: DialogTheme(
alignment: Alignment.center,
contentTextStyle: Theme.of(context).textTheme.bodyText1),
),
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;
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;
}
Future<void> prepareData() async {
await setupFlutterNotifications();
await AppController.current.prepareAccessToken();
var uuid = "rookie1234"; //Uuid();
await AppController.current.registerFirebaseMessagingToken(uuid);
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,
// TODO add a proper drawable resource to android, for now using
// one that already exists in example app.
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(() {});
}
@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(
// color: Colors.green,
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(
// color: Colors.green,
width: 50,
height: 38,
child: MailboxAlertButton())),
],
),
),
);
}
}