繁体   English   中英

当应用程序在前台使用 ZB12E1515C25C7D8C0352F1413AB9A15Z 通知 android

[英]Notification is coming sometimes and sometimes not coming when the app is in foreground using firebase notifications in Flutter for android

我在 flutter 中使用 firebase 消息作为 firebase 通知插件来发送和接收通知。 但是,当我触发从一部手机到另一部手机的通知时,它会触发,但当应用程序处于后台时,其他手机中不会获取通知。 而且 OnMessage 没有在其他手机上收到任何通知

import 'dart:convert';
import 'dart:io';
import 'package:device_info/device_info.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:mangoo/ui/Calls/VideoCall/calls.dart';
import 'package:mangoo/ui/Calls/VoiceCall/voice_call.dart';

class FirebaseNotifications {
  FirebaseMessaging _firebaseMessaging = FirebaseMessaging();

//  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
  BuildContext context;

  void setUpFirebase(BuildContext context) {
    print("in Firebase Setup");
//    _firebaseMessaging = FirebaseMessaging();
    this.context = context;
//    flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
//    firebaseCloudMessaging_Listeners();
    initNotification();
  }

  initNotification() {
    print("initNotification ")

    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print('on messagemessage ${message}');
        if (message.containsKey('data')) {
          String channel_name = message['data']['channel_name'];
          String phoneNumber = message['data']['phone_number'];
          String screen_id = message['data']['screen_id'];
          if (screen_id == "0") {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => CallPage(
                  channelName: channel_name,
                  phoneNumber: phoneNumber,
                ),
              ),
            );
          } else if (screen_id == "1") {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => VoiceCallPage(
                  channelName: channel_name,
                  phoneNumber: phoneNumber,
                ),
              ),
            );
          }
        }

        // initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
//        displayNotification(message);
//        Navigator.push(
//            context, MaterialPageRoute(builder: (context) => CallPage()));
        // _showItemDialog(message);
      },
      onBackgroundMessage: myBackgroundMessageHandler,
//        print('on backgroundmessage ${message}');
//      },
      onResume: (Map<String, dynamic> message) async {
        print('on messageResume ${message}');
//        displayNotification(message);
      },
      onLaunch: (Map<String, dynamic> message) async {
        print('on messageLaunch ${message}');
//        displayNotification(message);
      },
    );
  }


  Widget ShowDialog(String channelName, String phoneNumber) {
    return Container(
        width: MediaQuery.of(context).size.width / 2,
        height: MediaQuery.of(context).size.height / 4,
        child: Column(
          children: <Widget>[
            Container(
                color: Colors.yellowAccent,
                width: MediaQuery.of(context).size.width,
                height: 50,
                child: Text("Incoming Video call")),
            SizedBox(
              height: 5.0,
            ),
            Container(
              child: Row(
                children: <Widget>[
                  RaisedButton(
                    onPressed: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (context) => CallPage(
                            channelName: channelName,
                            phoneNumber: phoneNumber,
                          ),
                        ),
                      );
                    },
//                  textColor: Colors.black,
                    child: Text(
                      "ACCEPT",
                      style: TextStyle(color: Colors.black, fontSize: 12),
                    ),
                  ),
                  VerticalDivider(
                    width: 3,
                  ),
                  RaisedButton(
                    onPressed: () {},
                    color: Colors.green,
                    child: Text(
                      "REJECT",
                      style: TextStyle(color: Colors.black, fontSize: 12),
                    ),
                  )
                ],
              ),
            )
          ],
        ));
  }
}

我有一个类似的问题,有时会发送通知,我发现了一个简单的解决方法,只需将您的myBackgroundMessageHandler定义为:

Future<void> myBackgroundMessageHandler(Map<String, dynamic> message) async {
      final prefs = await SharedPreferences.getInstance();
      // SharedPreferences could have been modified in the UI isolate, so we have to fetch it from the host platform.
      await prefs.reload();
}

You can also go through this blog which explains how to setup firebase messaging on Flutter https://medium.com/@sylvainduch/firebase-messaging-catch-them-all-3f9c594734d7

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM