Ready Made Code for Firebase Push Notification

Overview

Step 1: Create Firebase Project.

Step 2: Add json to android -> app -> directory.

Step 3: Add "firebase_core" , "firebase_messaging" & "flutter local notification" on pubspec.ymal & import packages.

Step 4: Add this code on main.dart and Override your nessecery things.

import 'package:flutter/material.dart';
import 'package:dhakarchaka/pages/NextScreen.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';

const AndroidNotificationChannel channel = AndroidNotificationChannel(
    'high_importance_channel', // id
    'High Importance Notifications', // title
    description: 'This channel is used for important notifications.', // description
    importance: Importance.high,
    playSound: true);

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();

Future
   
     _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();
  print('A bg message just showed up :  ${message.messageId}');
}


Future
    
      main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp,DeviceOrientation.portraitDown]);


  await flutterLocalNotificationsPlugin
      .resolvePlatformSpecificImplementation
     
      ()
      ?.createNotificationChannel(channel);

  await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
    alert: true,
    badge: true,
    sound: true,
  );
  runApp(MyApp());
}

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

class _MyAppState extends State
       
         { int _counter = 0; @override void initState() { super.initState(); FirebaseMessaging.onMessage.listen((RemoteMessage message) { RemoteNotification? notification = message.notification; AndroidNotification? android = message.notification?.android; if (notification != null && android != null) { flutterLocalNotificationsPlugin.show( notification.hashCode, notification.title, notification.body, NotificationDetails( android: AndroidNotificationDetails( channel.id, channel.name, color: Colors.blue, playSound: true, icon: '@mipmap/ic_launcher', ), )); } }); FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) { print('A new onMessageOpenedApp event was published!'); RemoteNotification? notification = message.notification; AndroidNotification? android = message.notification?.android; if (notification != null && android != null) { showDialog( context: context, builder: (_) { return AlertDialog( title: Text(notification.title.toString()), content: SingleChildScrollView( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [Text(notification.body.toString())], ), ), ); }); } }); } void showNotification() { setState(() { _counter++; }); flutterLocalNotificationsPlugin.show( 0, "Testing $_counter", "How you doin ?", NotificationDetails( android: AndroidNotificationDetails(channel.id, channel.name, importance: Importance.high, color: Colors.blue, playSound: true, icon: '@mipmap/ic_launcher'))); } @override Widget build(BuildContext context) { return MaterialApp( home: NextScreen() ); } } 
       
      
     
    
   

Note: This code copied from one of my Application. Maybe you need to change something.

You might also like...

A Dart testing utility for asserting that some code emits a compilation error.

A Dart testing utility for asserting that some code emits a compilation error.

Dec 11, 2022

AsyncCallQueue is a Dart class which provides a queuing mechanism to prevent concurrent access to asynchronous code.

async_call_queue AsyncCallQueue is a Dart class which provides a queuing mechanism to prevent concurrent access to asynchronous code. Getting Started

Jan 18, 2022

The Dart code generator for your package versions. 🎯

The Dart code generator for your package versions. 🎯

The Dart code generator for your package versions. There is no way to get the package version from the code in the Dart ecosystem. Installation Add bu

Dec 14, 2022

A flutter plugin for execute dart code in background.

flutter_background_service A flutter plugin for execute dart code in background. Android No additional setting required. To change notification icon,

Dec 27, 2022

test code

test code

E-Commerce Complate App - Flutter UI In the first part of our complete e-commerce app, we show you how you can create a nice clean onboarding screen f

Jun 16, 2022

Swagger/OpenAPI code generator based on Chopper and JsonAnnotation for Flutter

Swagger/OpenAPI code generator based on Chopper and JsonAnnotation for Flutter

Code partially generated with chopper 📣 Build dart types from Swagger/OpenAPI schemas SwaggerDartCodeGenerator is a code generator that looks for *.s

Jan 5, 2023

Some examples for how to build dart code using package:analyzer and package:code_builder only (i.e. no build_runner or build.yaml files)

What is this? Some examples for how to build dart code from another dart code us

Apr 17, 2022

Get Version - Get the Version Name, Version Code, Platform and OS Version, and App ID on iOS and Android. Maintainer: @rodydavis

Get Version - Get the Version Name, Version Code, Platform and OS Version, and App ID on iOS and Android. Maintainer: @rodydavis

Get Version - Get the Version Name, Version Code, Platform and OS Version, and App ID on iOS and Android.

Jan 4, 2023

Reflectable is a Dart library that allows programmers to eliminate certain usages of dynamic reflection by specialization of reflective code to an equivalent implementation using only static techniques

Reflectable is a Dart library that allows programmers to eliminate certain usages of dynamic reflection by specialization of reflective code to an equivalent implementation using only static techniques. The use of dynamic reflection is constrained in order to ensure that the specialized code can be generated and will have a reasonable size.

Dec 31, 2022
Owner
HM Athir
Android & iOS Developer. A Proud Developer @ SoftX Dot Dev
HM Athir
VS Code `.code-workspace` file generator

VS Code .code-workspace file generator (for monorepositories with Dart and Flutter projects) TL;DR; Create yaml file config.yaml (check #Format sectio

Mike T 1 Feb 18, 2022
Starter project and code generator for Flutter/Redux

Flutter Redux Starter/Code Generator Videos Short video ~ 1 minute Long video ~ 10 minutes We're using this approach to develop the Flutter app for In

Hillel Coren 278 Dec 12, 2022
The Flutter code generator for your assets, fonts, colors, … — Get rid of all String-based APIs.

The Flutter code generator for your assets, fonts, colors, … — Get rid of all String-based APIs. Inspired by SwiftGen. Motivation Using asset path str

FlutterGen 1.1k Jan 6, 2023
🚀The Flutter dart code generator from zeplin. ex) Container, Text, Color, TextStyle, ... - Save your time.

Flutter Gen Zeplin Extension ?? The Flutter dart code generator from zeplin. ex) Container, Text, Color, TextStyle, ... - Save your time. ⬇ 1.1k Getti

NAVER 49 Oct 12, 2022
A Dart package to web scraping data from websites easily and faster using less code lines.

Chaleno A flutter package to webscraping data from websites This package contains a set of high-level functions that make it easy to webscrap websites

António Nicolau 30 Dec 29, 2022
Basic Dart reverse shell code

dart_rs Basic Dart reverse shell based on this one by Potato-Industries. Pretty self explanatory. You’ll need Windows. I used a Windows 7 64-bit VM. F

null 21 Oct 2, 2022
Provides API to generate Dart source code

DartWriter DartWriter provides API to generate Dart source code. It can make your job easier while developing flutter/dart tools. You can also generat

Ahmet ÇELİK 11 Oct 24, 2022
A code generation tool based on Database. :construction: developing :construction:

dbgen A code generation tool based on Database. Getting Started This project is a starting point for a Flutter application. A few resources to get you

尽管如此世界依然美丽 12 Jun 8, 2022
Dart Code Generator for generating mapper classes

Smartstruct - Dart bean mappings - the easy nullsafe way! Code generator for generating type-safe mappers in dart, inspired by https://mapstruct.org/

Nils 28 Nov 29, 2022
Examples of Flutter Code Generation.

Flutter Code Generation Examples Code generation packages/tools used in the app: flutter_localization/intl build_runner flutter_gen/flutter_gen_runner

Mangirdas Kazlauskas 19 Oct 11, 2022