单击通知按钮不会在颤动中打开应用程序
Posted
技术标签:
【中文标题】单击通知按钮不会在颤动中打开应用程序【英文标题】:Clicking on Notification button not opening app in flutter 【发布时间】:2020-02-01 15:44:12 【问题描述】:我已经集成了用于通知的 firebase-messaging 插件,但是在单击通知时,如果它在后台或被杀死,则应用程序不会打开。我想在单击 natification 时打开应用程序。
下面是我的代码
void firebaseCloudMessagingListeners()
FirebaseMessaging _firebaseMessaging = new FirebaseMessaging();
_firebaseMessaging.getToken().then((token)
print(token);
);
_firebaseMessaging.requestNotificationPermissions(
const iosNotificationSettings(sound: true, badge: true, alert: true));
_firebaseMessaging.onIosSettingsRegistered
.listen((IosNotificationSettings settings)
print("Settings registered: $settings");
);
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async
_showNotificationWithDefaultSound(message['notification']['body']);
print('on message $message');
return;
,
onResume: (Map<String, dynamic> message) async
_showNotificationWithDefaultSound(message['data']['body']);
print('on message $message');
return;
,
onLaunch: (Map<String, dynamic> message) async
_showNotificationWithDefaultSound(message['data']['body']);
print('on message $message');
,
);
_showNotificationWithDefaultSound(message) async
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
new FlutterLocalNotificationsPlugin();
var android = new AndroidInitializationSettings('@mipmap/ic_launcher');
var ios = new IOSInitializationSettings();
var initSettings = new InitializationSettings(android, ios);
flutterLocalNotificationsPlugin.initialize(initSettings,
onSelectNotification: onSelectNotification);
var android1 = new AndroidNotificationDetails(
'channel id', 'channel NAME', 'channel DESCRIPTION',
importance: Importance.Max, priority: Priority.High, ticker: 'ticker');
var ios1 = new IOSNotificationDetails();
var platform = new NotificationDetails(android1, ios1);
await flutterLocalNotificationsPlugin
.show(0, message, 'App Notification!', platform, payload: message);
【问题讨论】:
【参考方案1】:你需要添加AndroidManifest.xml
<intent-filter>
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
然后,您需要在 firebase 通知的数据正文中发送 click_action:FLUTTER_NOTIFICATION_CLICK,如下所示:
DATA='
"notification":
"body": "this is a body",
"title": "this is a title"
,
"data":
"att1": "value..",
"att2": "value..",
"click_action": "FLUTTER_NOTIFICATION_CLICK",
,
"to": "<FCM TOKEN>"
'
当你的应用被杀死时,如果你点击通知,就会调用onLaunch方法,你必须通过message['data']获取参数。
在此处查看更多信息:https://***.com/a/48405551/7105694
【讨论】:
【参考方案2】:可能你需要实现onSelectNotification()
方法
Future<void> onSelectNotification(String payload) async
Navigator.push(
context, PageTransition(
type: PageTransitionType.leftToRight,
child: /* your home screen name */));
【讨论】:
但是如果应用程序被杀死会怎样以上是关于单击通知按钮不会在颤动中打开应用程序的主要内容,如果未能解决你的问题,请参考以下文章
当应用程序不在前台时,特定活动不会从通知单击打开(应用程序是最近的,没有被杀死!)