main.dart

 avatar
Nullyx
dart
2 years ago
2.9 kB
5
Indexable
Never
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:timezone/timezone.dart' as tz;
import 'package:timezone/data/latest_all.dart' as tz;

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

class myApp extends StatelessWidget {
  const myApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: notificationApp(),
    );
  }
}

class notificationApp extends StatefulWidget {
  const notificationApp({Key? key}) : super(key: key);

  @override
  State<notificationApp> createState() => _notificationAppState();
}

class _notificationAppState extends State<notificationApp> {
  var flp = FlutterLocalNotificationsPlugin();
  
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    kurulum();
  }
  Future<void > kurulum() async{
    var androidAyari=AndroidInitializationSettings("@mipmap/ic_launcher");
    var kurulumAyari=InitializationSettings(android: androidAyari);
    await flp.initialize(kurulumAyari,onSelectNotification: bildirimSecilme);
  }

  Future<void > bildirimSecilme(String? payLoad) async{
    if(payLoad !=null)print("Bildirim seçildi : $payLoad");
  }

  Future<void > bildirimGoster() async{
    var androidBildirimDetayi =AndroidNotificationDetails("kanal id", "kanal baslık",
    channelDescription: "kanal icerik",priority: Priority.high,importance:Importance.max
    );
    var bildirimDetayi=NotificationDetails(android: androidBildirimDetayi);
    await flp.show(0, "Başlık", "İçerik", bildirimDetayi,payload: "Payload İçerik");
  }


  Future<void > gecikmeliGoster() async{
    var androidBildirimDetayi =AndroidNotificationDetails("kanal id", "kanal baslık",
        channelDescription: "kanal icerik",priority: Priority.high,importance:Importance.max
    );

    var bildirimDetayi=NotificationDetails(android: androidBildirimDetayi);
    tz.initializeTimeZones();
    var gecikme=tz.TZDateTime.now(tz.local).add(const Duration(seconds: 5));
    await flp.zonedSchedule(0, "Başlık Geçikme", "İçerGeçikme", uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime , gecikme,bildirimDetayi,payload: "Payload İçerik", androidAllowWhileIdle: true);
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(onPressed: (){
                  bildirimGoster();
            }, child: Text("Bildirim")),
            ElevatedButton(onPressed: () {
                  gecikmeliGoster();
            }, child: Text("Gecikmeli Bildirim"))
          ],
        ),
      ),
    );
  }
}