Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to implement push notification in flutter

Tags:

flutter

dart

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),           ),          ],       ),     ));   }    } 
like image 268
siva kumar Avatar asked Oct 05 '17 06:10

siva kumar


People also ask

How do you integrate push notifications on flutter?

To send a notification, go to Firebase Console → Cloud Messaging and click on Send your first message. Then enter the Title and body field. If you wish to send it to a particular device then click on Send test message and enter the FCM registration token. (Enter the current FCM registration token).


2 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.

like image 200
Brad Hesse Avatar answered Sep 20 '22 21:09

Brad Hesse


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.

like image 33
Collin Jackson Avatar answered Sep 19 '22 21:09

Collin Jackson