Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
941 views
in Technique[技术] by (71.8m points)

dart - how to implement push notification in flutter

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),
          ),

        ],
      ),
    ));
  }

  }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...