🙂 İNSANLARIN EN HAYIRLISI INSANLARA FAYDALI OLANDIR 🙂

Ramazan HABER / FLUTTER / Awesome Notifications Kullanımı

1-) FLUTTER - Awesome Notifications Kullanımı

 

kaynak :  https://pub.dev/packages/awesome_notifications

 

flutter pub add awesome_notifications

 

EKRAN GÖRÜNTÜSÜ

 

1-) main.dart

 

 

import 'dart:isolate';
import 'dart:ui';

import 'package:awesome_notifications/awesome_notifications.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'NotificationController.dart';


// kaynak :  https://pub.dev/packages/awesome_notifications
void main() {
  
WidgetsFlutterBinding.ensureInitialized();

  
AwesomeNotifications().isNotificationAllowed().then((isAllowed) {
    
if (!isAllowed) {
      
// This is just a basic example. For real apps, you must show some
      // friendly dialog box before call the request method.
      // This is very important to not harm the user experience
      
AwesomeNotifications().requestPermissionToSendNotifications();
    }
  });

  
AwesomeNotifications().initialize('resource://drawable/ic_launcher', [
    
// notification icon
    
NotificationChannel(
      channelGroupKey:
'basic_test',
      channelKey:
'basic',
      channelName:
'Basic notifications',
      channelDescription:
'Notification channel for basic tests',
      channelShowBadge:
true,
      importance:
NotificationImportance.High,
      enableVibration:
true,
    ),
  ]);

  runApp(
MyApp());
}

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

  
@override
  
Widget build(BuildContext context) {
    
return MaterialApp(
      title:
'Flutter Demo',
      theme:
ThemeData(
        primarySwatch:
Colors.blue,
      ),
      home:
MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  
MyHomePage({super.key});

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

class _MyHomePageState extends State<MyHomePage> {
  
@override
  
void initState() {
    
// TODO: implement initState
    
super.initState();

    
AwesomeNotifications().setListeners(
      onActionReceivedMethod: (
ReceivedAction receivedAction) {
        print(
"buradayim!!");

        
if (receivedAction?.buttonKeyPressed == 'Yenile') {
          print(
"Yeniledim...!!");
          sendNoti(
"Rambo 2");
        }

        
return NotificationController.onActionReceivedMethod(receivedAction);
      },
    );
  }

  
@override
  
Widget build(BuildContext context) {
    
return Scaffold(
      appBar:
AppBar(
        title:
Text("LOCAL NOTİ"),
      ),
      body:
Center(
        child:
Column(
          mainAxisAlignment:
MainAxisAlignment.center,
          children: [
            
ElevatedButton(
                onPressed: () {
                  sendNoti(
"Rambo 1");
                },
                child:
Text("BAŞLAT")),
          ],
        ),
      ),
    );
  }

  
void sendNoti(String text) {
    
AwesomeNotifications().createNotification(
      content:
NotificationContent(
        
//simgple notification
        
id: 123,
        channelKey:
'basic',
        
//set configuration wuth key "basic"
        
title: text,
        body:
'This simple notification with action buttons in Flutter App',
        payload: {
"name": "FlutterCampus"},
        autoDismissible:
false,

        locked:
true, // hiç kapanmaması için
      
),
      actionButtons: [
        
NotificationActionButton(
            key:
"Yenile",
            label:
"Yenile",
            autoDismissible:
false,
            isDangerousOption:
true,
            actionType:
ActionType.Default), // KeepOnTop
        
NotificationActionButton(
            key:
"Kapat",
            label:
"Kapat",
            autoDismissible:
false,
            isDangerousOption:
true,
            actionType:
ActionType.DismissAction),
      ],
      
// schedule: NotificationCalendar(
      //   second: 3, // dakikada 1 demek
      //   repeats: true,
      // ),
    
);
  }
}

 

 

 

 

2-) NotificationController.dart

 

 

import 'package:awesome_notifications/awesome_notifications.dart';

import 'main.dart';

class NotificationController {
  
/// Use this method to detect when a new notification or a schedule is created
  
@pragma("vm:entry-point")
  
static Future<void> onNotificationCreatedMethod(
      
ReceivedNotification receivedNotification) async {
    
// Your code goes here
  
}

  
/// Use this method to detect every time that a new notification is displayed
  
@pragma("vm:entry-point")
  
static Future<void> onNotificationDisplayedMethod(
      
ReceivedNotification receivedNotification) async {
    
// Your code goes here
  
}

  
/// Use this method to detect if the user dismissed a notification
  
@pragma("vm:entry-point")
  
static Future<void> onDismissActionReceivedMethod(
      
ReceivedAction receivedAction) async {
    
// Your code goes here
  
}

  
/// Use this method to detect when the user taps on a notification or action button
  
@pragma("vm:entry-point")
  
static Future<void> onActionReceivedMethod(
      
ReceivedAction receivedAction) async {
    
// Your code goes here
    
print("hey33!!");
    
// Navigate into pages, avoiding to open the notification details page over another details page already opened
    // MyApp.navigatorKey.currentState?.pushNamedAndRemoveUntil('/notification-page',
    //         (route) => (route.settings.name != '/notification-page') || route.isFirst,
    //     arguments: receivedAction);
  
}
}

 

 

 

 

3-) AndroidManifest.xml

 

<uses-permission android:name="android.permission.INTERNET"/>
<
uses-permission android:name="android.permission.VIBRATE" />
<
uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<
uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>

 

 

 2022 Kasım 24 Perşembe
 301