在我的颤振(android)应用程序中 - Firebase 推送通知在一段时间后停止接收每个用户

Posted

技术标签:

【中文标题】在我的颤振(android)应用程序中 - Firebase 推送通知在一段时间后停止接收每个用户【英文标题】:In my flutter(android) app - Firebase push notifications stop receiving after some time for every user 【发布时间】:2021-12-15 00:31:51 【问题描述】:

我有一个颤振应用程序(适用于 android)使用 fcm 通知。通知由服务器通过 fcm 管理。一开始,所有前台、后台、已终止状态都会收到通知。

但经过一段时间后,应用程序停止显示通知(前台、后台全部 - 根本没有通知)对于某些用户,它会在 5 天后,大约 1.2 周后,等等。

应用程序会在一段时间后停止为每个用户显示通知(前台、后台)。 App Firebase 配置如下。

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.d.app_name">
  
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />

   
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <!-- End Alarm Manager -->

    <!-- Cleartext Traffic required by Flutter Espresso, do not use in production -->
    <!-- android:name="io.flutter.app.FlutterApplication" -->
    <application
        android:name="io.flutter.app.FlutterApplication"
        android:icon="@mipmap/ic_launcher"
        android:label="app name"
        android:usesCleartextTraffic="true">

        <activity
            android:name=".MainActivity"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:windowSoftInputMode="adjustResize">
           
            <meta-data
                android:name="io.flutter.embedding.android.NormalTheme"
                android:resource="@style/NormalTheme" />

            <meta-data
                android:name="io.flutter.embedding.android.SplashScreenDrawable"
                android:resource="@drawable/launch_background" />

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
    
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />


        <!-- Start Alarm Manager -->
        <service
            android:name="dev.fluttercommunity.plus.androidalarmmanager.AlarmService"
            android:exported="false"
            android:permission="android.permission.BIND_JOB_SERVICE" />


        <receiver
            android:name="dev.fluttercommunity.plus.androidalarmmanager.AlarmBroadcastReceiver"
            android:exported="false" />
        <receiver
            android:name="dev.fluttercommunity.plus.androidalarmmanager.RebootBroadcastReceiver"
            android:enabled="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
        <!-- End Alarm Manager -->
    </application>
</manifest>

Application.kt

package com.d.app_name
import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.view.FlutterMain
import io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingBackgroundService;

class Application : FlutterApplication(), PluginRegistrantCallback 

    override fun onCreate() 
        super.onCreate()
        FlutterFirebaseMessagingBackgroundService.setPluginRegistrant(this);
        FlutterMain.startInitialization(this)
    

    override fun registerWith(registry: PluginRegistry?) 
    
 

main.dart


final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
    FlutterLocalNotificationsPlugin();

const AndroidNotificationChannel channel = AndroidNotificationChannel(
  'high_importance_channel', // id
  'High Importance Notifications', // title
  'This channel is used for important notifications.', // description
  importance: Importance.high,
);

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async 
  await Firebase.initializeApp();
  print("message received");

  final payload = message.data;

  flutterLocalNotificationsPlugin.show(
      int.parse(payload["ID"]),
      payload["TYPE"] + " | " + payload["SITE_NAME"],
      payload["STATUS"] +
          " at " +
          payload["SITE_ID"] +
          "(" +
          payload["OCCURRED_TIME"] +
          ")",
      NotificationDetails(
        android: AndroidNotificationDetails(
            'Alarm', 'channel.name', 'channel.description',
            icon: 'mipmap/ic_launcher',
            playSound: true,
            sound: RawResourceAndroidNotificationSound('alarm_ring_sweet'),
            importance: Importance.max,
            onlyAlertOnce: true),
      ));



void main() async 
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();

  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

  runApp(MyApp());


class MyApp extends StatelessWidget 
  // This widget is the root of your application.

  @override
  Widget build(BuildContext context) 
    return MultiProvider(
      providers: [
        ChangeNotifierProvider<NOCProvider>(create: (context) => NOCProvider())
      ],
      child: MaterialApp(
        initialRoute: '/splash',
        onGenerateRoute: AppRoute.Router.genarateRoute,
      ),
    );
  


home.dart


class Home extends StatefulWidget 
  final PageContainer _pageContainer;

  Home(this._pageContainer);

  @override
  _HomeState createState() => _HomeState(this._pageContainer);


class _HomeState extends State<Home> with WidgetsBindingObserver 
  PageContainer pageContainer;
  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();

  _HomeState(this.pageContainer);

  /// Create a [AndroidNotificationChannel] for heads up notifications
  static const AndroidNotificationChannel channel = AndroidNotificationChannel(
    'high_importance_channel', // id
    'High Importance Notifications', // title
    'This channel is used for important notifications.', // description
    importance: Importance.high,
  );

  /// Initialize the [FlutterLocalNotificationsPlugin] package.
  final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
      FlutterLocalNotificationsPlugin();

  void initFirebaseReciver() async 
    await flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<
            AndroidFlutterLocalNotificationsPlugin>()
        ?.createNotificationChannel(channel);

    await FirebaseMessaging.instance
        .setForegroundNotificationPresentationOptions(
      alert: true,
      badge: true,
      sound: true,
    );

    FirebaseMessaging.onMessage.listen((RemoteMessage message) 
      var provider = Provider.of<NOCProvider>(context, listen: false);
      RemoteNotification notification = message.notification;
      AndroidNotification android = message.notification?.android;
      provider.getAllAlarm();

      final payload = message.data;
      flutterLocalNotificationsPlugin.show(
          int.parse(payload["ID"]),
          payload["TYPE"] + " | " + payload["SITE_NAME"],
          payload["STATUS"] +
              " at " +
              payload["SITE_ID"] +
              "(" +
              payload["OCCURRED_TIME"] +
              ")",
          NotificationDetails(
            android: AndroidNotificationDetails(
                channel.id, channel.name, channel.description,
                icon: 'mipmap/ic_launcher',
                playSound: true,
                sound: RawResourceAndroidNotificationSound('alarm_ring_sweet'),
                importance: Importance.max,
                onlyAlertOnce: true),
          ));

     
    );

    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) 
      var provider = Provider.of<NOCProvider>(context, listen: false);
      //get notifications and save in hive and return
      provider.getAllAlarm();
    );
  

  void callBack(PageContainer callBackPageContainer) 
    setState(() 
      pageContainer = callBackPageContainer;
    );
  


  Future<void> _firebaseMessagingBackgroundHandler(
      RemoteMessage message) async 
    await Firebase.initializeApp();
  

  void _onTokenRefresh(String token) 
    var provider = Provider.of<NOCProvider>(context, listen: false);

    provider.updateFcmTokenOnRefresh(token);
  

  void _tokenUpdate() 
    var provider = Provider.of<NOCProvider>(context, listen: false);
    provider.saveFcmToken();
  

  @override
  void initState() 
    super.initState();
    FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
    _tokenUpdate(); //update token in db
    FirebaseMessaging.instance.onTokenRefresh.listen(_onTokenRefresh);
    AndroidAlarmManager.initialize();
    
    WidgetsBinding.instance.addObserver(this);
    initFirebaseReciver();
  


  @override
  Widget build(BuildContext context) 
    var provider = Provider.of<NOCProvider>(context, listen: true);
    FirebaseMessaging.instance
        .getInitialMessage()
        .then((RemoteMessage message) 
      print('FirebaseMessaging.instance');
    );

    return Scaffold( ... );
  


可能是什么问题?我相信配置和代码没问题。知道如何排除故障吗?

【问题讨论】:

【参考方案1】:

问题出在 Firebase 后端。 Firebase 没有向设备发送通知。检查了 Firebase 分析并发现了这一点。

【讨论】:

以上是关于在我的颤振(android)应用程序中 - Firebase 推送通知在一段时间后停止接收每个用户的主要内容,如果未能解决你的问题,请参考以下文章

如何从 Android 活动导航到特定的颤振路线?

无法在我的颤振演示应用程序中的列表视图中显示网格视图

Firebase颤振推送通知不适用于Android

我想用我的 android 手机作为模拟器在 windows 7 上运行颤振。在我的调试模式下进行了调整。如何解决颤振医生命令的问题

在颤振中为Android构建时“错误:找不到符号”

颤振设备命令无法识别 android 设备