Flutter推送通知插件karte_notification的使用
Flutter推送通知插件karte_notification的使用
KARTE Notification 插件用于在 Flutter 应用程序中实现推送通知功能。它支持 Android 和 iOS 平台。
文档
开发指南可以在以下位置找到:
许可证
Flutter KARTE Notification 插件是在 Apache 2.0 许可证下发布的。
你的使用受 KARTE 使用条款 管理。
示例代码
以下是使用 karte_notification
插件的基本示例代码:
import 'dart:async';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:karte_core/karte_core.dart';
import 'package:karte_notification/karte_notification.dart' as krt;
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
Future<dynamic> myBackgroundMessageHandler(RemoteMessage message) async {
// 当仅在后台接收到通知时调用
print('myBackgroundMessageHandler $message');
var karteNotification = await krt.Notification.create(message);
print("karte notification: $karteNotification");
if (karteNotification != null) {
karteNotification.handleForAndroid();
}
}
class MyApp extends StatefulWidget {
[@override](/user/override)
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _homeScreenText = "等待令牌...";
String _logText = "";
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance;
void updateState({String? log, String? token}) {
if (!mounted) return;
setState(() {
if (log != null) _logText += log;
if (token != null) _homeScreenText = "推送消息令牌: $token";
});
}
void checkInitialMessage() async {
RemoteMessage? message = await FirebaseMessaging.instance.getInitialMessage();
// 当应用程序通过点击通知启动时调用
print("checkInitialMessage: $message");
updateState(log: "\nonLaunch");
if (message == null) return;
var karteNotification = await krt.Notification.create(message);
print("karte notification: $karteNotification");
if (karteNotification != null) {
karteNotification.handleForIOS();
}
}
[@override](/user/override)
void initState() {
super.initState();
checkInitialMessage();
FirebaseMessaging.onBackgroundMessage(myBackgroundMessageHandler);
FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
// 当在前台接收到通知时调用
print("onMessage: $message");
updateState(log: "\nonMessage");
var karteNotification = await krt.Notification.create(message);
print("karte notification: $karteNotification");
if (karteNotification != null) {
karteNotification.handleForAndroid();
}
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) async {
// 当通过点击通知恢复应用程序时调用
print("onMessageOpenedApp: $message");
updateState(log: "\nonMessageOpenedApp");
var karteNotification = await krt.Notification.create(message);
print("karte notification: $karteNotification");
if (karteNotification != null) {
karteNotification.handleForIOS();
}
});
_firebaseMessaging
.requestPermission(
alert: true, badge: true, provisional: true, sound: true)
.then((NotificationSettings value) {
print("设置已注册: $value");
});
_firebaseMessaging.onTokenRefresh.listen((String token) {
print("onTokenRefreshed: $token");
krt.Notification.registerFCMToken(token);
updateState(token: token);
});
_firebaseMessaging.getToken().then((String? token) {
if (token == null) return;
krt.Notification.registerFCMToken(token);
updateState(token: token);
print(_homeScreenText);
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('KARTE Notification 示例应用'),
),
body: Center(
child: Column(
children: [
Text(_homeScreenText),
ElevatedButton(
onPressed: () {
Tracker.view("push_text");
},
child: Text("查看"),
),
Text(_logText),
]),
),
),
);
}
}
更多关于Flutter推送通知插件karte_notification的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复