kun*_*369 4 android dart flutter firebase-cloud-messaging
我已经集成了用于通知的 firebase-messaging 插件,但单击通知应用程序如果在后台或被杀死,则不会打开。我想在单击通知时打开应用程序。
下面是我的代码
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);
}
Run Code Online (Sandbox Code Playgroud)
小智 7
您需要在AndroidManifest.xml中添加
<intent-filter>
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Run Code Online (Sandbox Code Playgroud)
然后,您需要在 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>"
}'
Run Code Online (Sandbox Code Playgroud)
当你的应用程序被杀死时,如果你点击通知,则会调用 onLaunch 方法,你必须获取 message['data'] 来获取参数。
在这里查看更多信息:https ://stackoverflow.com/a/48405551/7105694
| 归档时间: |
|
| 查看次数: |
4898 次 |
| 最近记录: |