how to implement push notification in flutter

Viewed 42496

Hi I am trying to implement push notification in flutter how to display as notification can any one help,I am able to listen as I am getting notification but I am not able to see the msg and it is appearing as alert but I want as notification can any one help and in android or iOS we should right in manifest file and app delegate file what about this in flutter

and my code look like this

 class PushMessagingExample extends StatefulWidget {
 @override
 _PushMessagingExampleState createState() => new _PushMessagingExampleState();
 }

class _PushMessagingExampleState extends State<PushMessagingExample> {
String _homeScreenText = "Waiting for token...";
bool _topicButtonsDisabled = false;

final FirebaseMessaging _firebaseMessaging = new FirebaseMessaging();
final TextEditingController _topicController =
new TextEditingController(text: 'topic');

Future<Null> _showItemDialog(Map<String, dynamic> message) async {
final Item item = _itemForMessage(message);
showDialog<Null>(
    context: context,
    child: new AlertDialog(
      content: new Text("Item ${message} has been updated"),
      actions: <Widget>[
        new FlatButton(
            child: const Text('CLOSE'),
            onPressed: () {
              Navigator.pop(context, false);
            }),
        new FlatButton(
            child: const Text('SHOW'),
            onPressed: () {
              Navigator.pop(context, true);
            }),
      ],
    )).then((bool shouldNavigate) {
  if (shouldNavigate == true) {
    _navigateToItemDetail(message);
  }
});
}

 Future<Null> _navigateToItemDetail(Map<String, dynamic> message) async     {
final Item item = _itemForMessage(message);
// Clear away dialogs
Navigator.popUntil(context, (Route<dynamic> route) => route is PageRoute);
if (!item.route.isCurrent) {
  Navigator.push(context, item.route);
}
}

  @override
 void initState() {
  super.initState();
_firebaseMessaging.configure(
  onMessage: (Map<String, dynamic> message) {
    print("onMessage: $message");
    print(message);
    _showItemDialog(message);
  },
  onLaunch: (Map<String, dynamic> message) {
    print("onLaunch: $message");
    print(message);
    _navigateToItemDetail(message);
  },
  onResume: (Map<String, dynamic> message) {
    print("onResume: $message");
    print(message);
    _navigateToItemDetail(message);
  },
);
_firebaseMessaging.requestNotificationPermissions(
    const IosNotificationSettings(sound: true, badge: true, alert: true));
_firebaseMessaging.onIosSettingsRegistered
    .listen((IosNotificationSettings settings) {
  print("Settings registered: $settings");
});
_firebaseMessaging.getToken().then((String token) {
  assert(token != null);
  setState(() {
    _homeScreenText = "Push Messaging token: $token";
  });
  print(_homeScreenText);
});
}

@override
Widget build(BuildContext context) {
return new Scaffold(


    body: new Material(
      child: new Column(
        children: <Widget>[
          new Center(
            child: new Text(_homeScreenText),
          ),

        ],
      ),
    ));
  }

  }
4 Answers

Push notifications in Flutter can be particularly difficult because you have to do twice the work to get them working with Android & iOS' particular implementations (both of which are, of course, quite different).

I've been building the OneSignal SDK for flutter. It will be ready within a few days. We work pretty hard to simplify push notifications as much as possible, and it's easy to use our SDK in a GDPR compliant way.

The repo is here (https://github.com/OneSignal/OneSignal-Flutter-SDK), it's open source and we welcome contributions from anyone. It acts as a wrapper on top of the native Android & iOS OneSignal SDK's.

If you're trying to put messages on the lock screen, make sure you're sending a "notification" message type, rather than a "data" message that will be delivered to the running app. You can learn more about the different types of Firebase messages in the Firebase developer guide.


1. Add Dependency

For sending push notifications we use a plugin called firebase_messaging…

https://pub.dev/packages/firebase_messaging

Open your pubspec.yaml file and add this dependency

dependencies: firebase_messaging: ^6.0.9


2. Firebase Account

Make sure you have a firebase account before proceeding.
In the next step, you should be able to download the google-services.json file.

After downloading the file, make sure to copy it inside the app/ folder of your Android project.


3. Android Manifest

ADD

<intent-filter>
  <action android:name="FLUTTER_NOTIFICATION_CLICK" />
  <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

Add these lines to [project]/android/build.gradle

dependencies {
    classpath 'com.android.tools.build:gradle:4.0.0'
    classpath 'com.google.gms:google-services:4.3.3'
}



Add these lines to [project]/android/app/build.gradle

 dependencies {
   implementation fileTree(dir: "libs", include: ["*.jar"])
   implementation 'androidx.appcompat:appcompat:1.1.0'
   implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
   implementation 'com.google.firebase:firebase-messaging:20.2.3'
   implementation 'com.google.firebase:firebase-analytics:17.4.4'

   // Add the SDK for Firebase Cloud Messaging
   testImplementation 'junit:junit:4.12'
   androidTestImplementation 'androidx.test.ext:junit:1.1.1'
   androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

 }


 apply plugin: 'com.google.gms.google-services'

DONE

Let me Give you the Most Prominent Solution for Pushing Notifications with Flutter, which i came Across Today, it is, locally it is a package by samuelezedi.com it Sends local Notifications. Schedule it, and Best part is it Doesn't need much setup like we always do with flutter_local_notification people are loosing their Strands after using it, it is So complicated and not Begineer Friendly, So We found locally locally will help you.

Sending Local Push Notifications

Locally locally = Locally(
      context: context,
      payload: 'test',
      pageRoute: null, //Link to which page you want to open
      appIcon: 'mipmap/ic_launcher', //Icon of your app, in android>app>src>main>res>drawable-v21 I put my png Icon..
  );

  locally.show(title: "Remaindero to use Toodolee", message: "Hey do this work");

See this is How easy it was to Send a push notification,

Sending Local Scheduled Notifications

Just instead of .show use .schedule this will help you to schedule

 locally.schedule(title: "Toodolee Reminder, message: "Hey have a run, duration: Duration(seconds: 5));

Their Documentation is so Good and amazing and newbie like me could understand it so beautifullly, one thing i could not found here is the support of custom Sounds.

Here is the Link of the Documentation. https://pub.dev/packages/locally

Thanks alot

Related