gpt4 book ai didi

flutter - 从推送通知打开 Flutter 应用程序,当它被杀死时

转载 作者:行者123 更新时间:2023-12-03 20:57:56 25 4
gpt4 key购买 nike

我是 flutter 的新手并试图实现推送通知。我已使用 ONE SIGNAL 进行推送通知

在这里,我初始化了一个信号并获取了我发送到服务器的 playerId。

    void oneSignalInit() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
OneSignal.shared.init('one signal id');
OneSignal.shared.setInFocusDisplayType(OSNotificationDisplayType.notification);

status = await OneSignal.shared.getPermissionSubscriptionState();

var playerId = status.subscriptionStatus.userId;
print(playerId);
preferences.setString(Constants.PLAYER_ID, playerId);
}

在这里,我收到来自一个信号的通知并从中获取所需的数据。
 notificationHandler() {
OneSignal.shared.setNotificationReceivedHandler((OSNotification notification) {
// will be called whenever a notification is received

var data = notification.payload.additionalData;
print(data['body']['tripID'].toString());
showNotification(message);

});

现在,我正在提高我的自定义通知,使用 flutter_local_notification 依赖项
      showNotification(var msg) async {
print("show notification is working");
AndroidNotificationDetails androidPlatformChannelSpecifics =
new AndroidNotificationDetails(
msg, msg, msg);
IOSNotificationDetails iOSPlatformChannelSpecifics =
new IOSNotificationDetails();
NotificationDetails platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0, msg, msg, platformChannelSpecifics,
payload: 'item id 2');
}

在这里,notificatin 将在 android 和 ios 平台上引发,并且 onNotificationcClick 回调也在此处声明。
 flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
var android = AndroidInitializationSettings('@mipmap/ic_launcher');
var iOS = IOSInitializationSettings();
var initSettings = InitializationSettings(android, iOS);
flutterLocalNotificationsPlugin.initialize(initSettings,
onSelectNotification: onNotification);

点击通知时
      Future onNotification(String payload) {
print(payload);
navigatorKey.currentState.pushNamed("/notification");

}

当应用程序在后台或未被杀死时,这工作正常。当它被杀死时,我无法打开我的 Flutter 应用程序。

任何形式的帮助将不胜感激.. !!

最佳答案

FirebaseMessaging.instance.getInitialMessage() .then((RemoteMessage message) {}});当应用程序被杀死时收到消息时,此函数将运行。在函数体里面写你想做的任何事情。

关于flutter - 从推送通知打开 Flutter 应用程序,当它被杀死时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59942097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com