'Firebase push notifications stops working after some time for all users in my Flutter app. Is this correct flutter fcm implementation?

I'm using aws sns to send notifications to users via firebase cloud messaging. App is built with flutter. Notifications work alright(background,foreground,killed) for some time but after that, notifications stops receiving completely.

Token refresh/update is handled. token updates on db.

There seems to be no issue with aws sns. I want to know is this the correct way of implementing fcm in a flutter app? is there anything wrong with the code?

AndroidManifest.xml

...

        <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.SplashScreenDrawable"
                android:resource="@drawable/launch_background" />

            
            <intent-filter>
                <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
           
        </activity>
       
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="high_importance_channel" />

        
...

android > build.gradle

buildscript {
   ...

    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.5'
    }
...
}

android > app > build.gradle

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation platform('com.google.firebase:firebase-bom:27.1.0')
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.android.support:multidex:1.0.3'
    
}

and the main.dart background handler

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
    FlutterLocalNotificationsPlugin();

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

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();
  print("fcm_bg_handler: 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',
      //       channelDescription: 'channel.description',
      //       icon: 'mipmap/ic_launcher',
      //       playSound: true,
      //       sound: RawResourceAndroidNotificationSound('alarm_ring_sweet'),
      //       importance: Importance.max,
      //       onlyAlertOnce: true),
      // )
      NotificationDetails(
        android: AndroidNotificationDetails(channel.id, channel.name,
            channelDescription: 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());
}

onMessage and onMessageOpenedApp methods are as normal. Is there anything wrong with the code. Notifications work properly when installed first time but after some time they stop recieving for all users.

Tried

1)install app again. -still notifications don't come at all.

2)add app to unmonitored apps in device settings. this didn't work either.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source