如何在Flutter中实现推送通知 [英] how to implement push notification in flutter

查看:2106
本文介绍了如何在Flutter中实现推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在通知中实施推送通知,如何在通知可以帮助任何人的情况下进行显示,我能够在收到通知时进行收听,但看不到味精,并且显示为警报,但是我想要作为通知可以提供任何帮助,并且在android或iOS中,我们应该在清单文件和应用程序委托文件中进行修改

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

,我的代码如下所示

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

        ],
      ),
    ));
  }

  }


推荐答案

在Flutter中推送通知可能特别困难,因为要使其与Android& amp; iOS的特定实现(当然两者都有很大的不同)。

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

我一直在为抖动开发OneSignal SDK。几天之内就可以准备好了。我们非常努力地尽可能简化推送通知,并且很容易以符合GDPR的方式使用我们的SDK。

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.

此处为repo( https://github.com/OneSignal/OneSignal-Flutter-SDK ),它是开源的,我们欢迎任何人的贡献。它充当原生Android& amp;的包装。 iOS OneSignal SDK。

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.

这篇关于如何在Flutter中实现推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆