扑扑+ Firebase adminsdk C#消息传递

时间:2019-04-23 14:44:35

标签: c# android firebase push-notification flutter

使用firebase C#admin sdk在Flutter移动应用程序上获取简单的推送通知时遇到一些麻烦:

我的C#代码如下

private async Task SendPushNotification3Async(string val)
{
    FirebaseApp.Create(new AppOptions()
    {
        Credential = GoogleCredential.FromFile(@"e:\firebase-adminsdk.json"),
    });
    var condition = "'SQL' in topics";
    var message = new FirebaseAdmin.Messaging.Message()
    {

        Notification = new FirebaseAdmin.Messaging.Notification()
        {
            Title = "simple title",
            Body = "simple message",
        },
        Condition = condition
    };


    var response = await FirebaseMessaging.DefaultInstance.SendAsync(message);

}

在颤动的一面:

import 'package:flutter/material.dart';
import 'package:sqlnotifapp/widget/messaging_widget.dart';


void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  final String appTitle = 'Simple Notifications';
  @override
  Widget build(BuildContext context) => MaterialApp(
        title: appTitle,
        home: MainPage(appTitle: appTitle),
      );
}

class MainPage extends StatelessWidget {
  final String appTitle;

  const MainPage({this.appTitle});

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(
          title: Text(appTitle),
        ),
        body: MessagingWidget(),
      );
}

和消息传递小部件:

import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:sqlnotifapp/model/message.dart';


class MessagingWidget extends StatefulWidget {
  @override
  _MessagingWidgetState createState() => _MessagingWidgetState();
}

class _MessagingWidgetState extends State<MessagingWidget> {
   final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();



  final List<Message> messages = [];

  @override
  void initState() {
    super.initState();
    _firebaseMessaging.configure(

      onMessage: (Map<String, dynamic> message) async {
        print("onMessage: $message");
        final notification = message['notification'];
        setState(() {
          messages.add(Message(
              title: notification['title'], body: notification['body']));
        });
      },

      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message");
_firebaseMessaging.subscribeToTopic('SQL');
        final notification = message['data'];
        setState(() {
          messages.add(Message(
            title: '${notification['title']}',
            body: '${notification['body']}',
          ));
        });
      },
      onResume: (Map<String, dynamic> message) async {
        print("onResume: $message");
      },
    );
    _firebaseMessaging.requestNotificationPermissions(
        const IosNotificationSettings(sound: true, badge: true, alert: true));
  }


  @override
  Widget build(BuildContext context) => ListView(
        children: messages.map(buildMessage).toList(),
      );

  Widget buildMessage(Message message) => ListTile(
        title: Text(message.title),
        subtitle: Text(message.body),
      );
}

请不要在线

  

_firebaseMessaging.subscribeToTopic('SQL');

运行此代码时,没有例外,但也没有推送通知。 请注意,我可以从Firebase控制台获取推送通知 可以毫无问题地连接到移动设备,但不能通过.net api。

我在这里想念什么?

还有没有办法使用它的应用程序ID通过api将消息发送到android应用程序?

欢迎任何帮助或想法,谢谢

0 个答案:

没有答案