[英]In my flutter(android) app - Firebase push notifications stop receiving after some time for every user
I have a flutter app(for android) uses fcm notifications.我有一个 Flutter 应用程序(适用于 android)使用 fcm 通知。 notifications are managed by a server via fcm.
通知由服务器通过 fcm 管理。 In the beginning notifications are received in all forground, background, killed states.
一开始,所有前台、后台、已杀状态都会收到通知。
But after some time app stops showing notifications(foreground, background all- no notifications at all) For some users it's after 5 days, for some 1,2 weeks, etc.但是一段时间后,应用程序停止显示通知(前台,后台全部 - 根本没有通知)对于某些用户来说,它是在 5 天之后,大约 1,2 周,等等。
app stops showing notifications(foreground, background both) after some time for every user.一段时间后,应用程序停止为每个用户显示通知(前台、后台)。 App firebase configurations is as follows.
应用 Firebase 配置如下。
AndroidManifest.xml 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="com.google.android.geo.API_KEY"
android:value="AIzaSyBeTjQD2nWGhgyiZPu2VqdxwRLfoeTwKdk" />
<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" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyBeTjQD2nWGhgyiZPu2VqdxwRLfoeTwKdk" />
<!-- 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应用程序.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 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家.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( ... );
}
}
What can be the issue?可能是什么问题? I beilieve configurations and code is ok.
我相信配置和代码没问题。 Any idea how to troubleshoot at least?
知道如何至少排除故障吗?
Issue was with firebase backend.问题在于 Firebase 后端。 Firebase wasn't sending notifications to devices.
Firebase 没有向设备发送通知。 Checked firebase analytics and figured out this.
检查了 Firebase 分析并发现了这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.