flutter - 应用关闭时的 Onesignal 通知
问题描述
当应用程序关闭时如何发送一个信号通知?当应用程序打开时没有问题。该应用程序完全由用户关闭,而不是在后台运行的应用程序。
我提供了我的代码供审查:我是新来的,所以不确定它是如何工作的,尤其是推送通知
import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:connectivity/connectivity.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:onesignal_flutter/onesignal_flutter.dart';
Future main() async {
runApp(new MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _outputText = "";
@override
void initState() {
super.initState();
initOneSignal();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: IndexedStack(
index: 0,
children: <Widget>[
InAppWebViewPage(),
Container(
child: Text(_outputText),
)
],
));
}
Future<void> initOneSignal() async {
await OneSignal.shared
.init("xxxxxxxx-xxxxxxxxxx", iOSSettings: null);
OneSignal.shared
.setInFocusDisplayType(OSNotificationDisplayType.notification);
OneSignal.shared
.setNotificationReceivedHandler((OSNotification notification) {
this.setState(() {
_outputText =
"Received notification: \n${notification.jsonRepresentation().replaceAll("\\n", "\n")}";
});
});
}
}
class InAppWebViewPage extends StatefulWidget {
@override
_InAppWebViewPageState createState() => new _InAppWebViewPageState();
}
class _InAppWebViewPageState extends State<InAppWebViewPage> {
InAppWebViewController webView;
int _page = 2;
bool _loadError = false;
StreamSubscription<ConnectivityResult> subscription;
@override
initState() {
super.initState();
subscription = Connectivity()
.onConnectivityChanged
.listen((ConnectivityResult result) {
if (result != ConnectivityResult.none && webView != null) {
print("reload");
_loadError = false;
webView.reload();
}
});
}
@override
dispose() {
super.dispose();
subscription.cancel();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: IndexedStack(
index: _page,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 20.0),
child: InAppWebView(
initialUrl: "https://flutter.dev/",
initialHeaders: {},
initialOptions: InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(
clearCache: true,
debuggingEnabled: true,
),
),
onWebViewCreated: (InAppWebViewController controller) {
webView = controller;
},
onLoadStart: (InAppWebViewController controller, String url) {},
onLoadStop: (InAppWebViewController controller, String url) {
print(url);
setState(() {
if (!_loadError) {
_page = 0;
} else {
_page = 1;
}
});
},
onLoadError: (InAppWebViewController controller, String url,
int code, String message) async {
print("error $url: $code, $message");
_loadError = true;
},
onLoadHttpError: (InAppWebViewController controller, String url,
int statusCode, String description) async {
print("HTTP error $url: $statusCode, $description");
},
),
),
(Platform.isAndroid)
? Container(
child: Text("My custom error message"),
)
: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/splash.png'),
fit: BoxFit.fill,
),
)),
Container(
color: Colors.transparent,
decoration: BoxDecoration(
image: DecorationImage(
image: ExactAssetImage('assets/images/splash.png'),
fit: BoxFit.fill,
),
)),
],
),
);
}
}
任何帮助表示赞赏。提前致谢
解决方案
确保您正确遵循 Flutter SDK 设置 https://documentation.onesignal.com/docs/flutter-sdk-setup
推荐阅读
- python - pymysql.err.IntegrityError: (1048, "列 'dob' 不能为空")
- java - 如何在 Spring 5.3 及更高版本中使用 Stomp 和 SockJS 处理 CORS 起源?
- c++ - 返回对临时对象的引用
- android - Android Kotlin - 动画翻译X忽略距离并离开屏幕
- css - 响应式列表样式
- haskell - Haskell 理解类型
- jekyll - 创建要在部署中设置的变量
- javascript - Vue CLI 给出错误 ERR_CONNECTION_TIMED_OUT
- core-data - 将核心数据对象提取到 SwiftUI 选择器中
- javascript - 如何将变量设置为两个随机选项之一?