Flutter推送消息插件flutter_pushed_messaging的使用
Flutter推送消息插件flutter_pushed_messaging的使用
获取开始
首先,在你的pubspec.yaml
文件中添加以下依赖:
# 添加此行到你的dependencies
flutter_pushed_messaging: ^1.4.0
然后在你的Dart文件中导入插件:
import 'flutter_pushed_messaging/flutter_pushed_messaging.dart';
iOS 配置
在iOS上,确保你已经正确配置了应用以支持推送通知。你需要添加推送通知功能和远程通知后台模式。
在AppDelegate.m/AppDelegate.swift
文件中添加以下代码:
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}
Android 配置
支持Fcm
在你的根目录下的build.gradle
文件中添加以下依赖:
buildscript {
...
dependencies {
classpath 'com.google.gms:google-services:4.3.15'
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.8.1'
...
}
}
将google-services.json
文件放置在Android/app
目录下,并在app/build.gradle
文件中添加以下插件:
...
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'
支持Hpk
在你的根目录下的build.gradle
文件中添加以下依赖:
buildscript {
...
repositories {
...
maven { url 'https://developer.huawei.com/repo/' }
}
dependencies {
classpath 'com.huawei.agconnect:agcp:1.5.2.300'
...
}
}
allprojects {
repositories {
...
maven { url 'https://developer.huawei.com/repo/' }
}
}
将agconnect-services.json
文件放置在Android/app
目录下,并在app/build.gradle
文件中添加以下依赖:
...
dependencies {
...
implementation 'com.huawei.agconnect:agconnect-core:1.5.2.300'
}
...
apply plugin: 'com.huawei.agconnect'
支持RuStore
在AndroidManifest.xml
文件中添加以下内容:
<application>
...
<meta-data
android:name="ru.rustore.sdk.pushclient.project_id"
android:value="Your RuStore project ID" />
...
</application>
实现
在你的Dart文件中实现消息处理逻辑:
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter_pushed_messaging/flutter_pushed_messaging.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:permission_handler/permission_handler.dart' as perm;
[@pragma](/user/pragma)('vm:entry-point')
Future<void> backgroundMessage(Map<dynamic,dynamic> message) async {
WidgetsFlutterBinding.ensureInitialized();
print("Backgroung message: $message");
if(Platform.isAndroid){
var flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
var title=(message["data"]["title"])??"";
var body=(message["data"]["body"])??"";
if(title!="" || body!="") {
await flutterLocalNotificationsPlugin.show(
8888,
title,
body,
const NotificationDetails(
android: AndroidNotificationDetails(
'push_channel', // id
'MY CHANNEL FOR PUSH',
icon: 'launch_background',
ongoing: false,
importance: Importance.max,
priority: Priority.high,
visibility: NotificationVisibility.public,
enableLights: true
),
),
);
}
}
}
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
if(Platform.isAndroid) {
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'push_channel', // id
'MY CHANNEL FOR PUSH', // title
description:
'This channel is used for push messages', // description
importance: Importance.max, // importance must be at low or higher level
);
var flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
await perm.Permission.notification.request();
}
await FlutterPushedMessaging.init(backgroundMessage);
print("Token: ${FlutterPushedMessaging.token}");
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
ServiceStatus _status = FlutterPushedMessaging.status;
String _title = '';
String _body = '';
String _token = '';
[@override](/user/override)
void initState() {
super.initState();
initPlatformState();
}
Future<void> initPlatformState() async {
FlutterPushedMessaging.onMessage().listen((message) {
print("Message: $message");
var title=(message["data"]["title"])??"";
var body=(message["data"]["body"])??"";
if(title!="" || body!="") {
setState(() {
_title=title;
_body=body;
});
}
});
FlutterPushedMessaging.onStatus().listen((status) {
setState(() {
_status=status;
});
});
if (!mounted) return;
setState(() {
_title = '';
_token=FlutterPushedMessaging.token??"";
_status=FlutterPushedMessaging.status;
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Pushed messaging plugin example app'),
),
body: Center(
child: Column(
children: [Text("Service status: $_status",style: Theme.of(context).textTheme.titleMedium),
Text(_title,style: Theme.of(context).textTheme.titleMedium),
Text(_body,style: Theme.of(context).textTheme.titleMedium,
textAlign: TextAlign.center, maxLines: 3,),
Text(_token,style: Theme.of(context).textTheme.titleMedium,),
TextButton(
onPressed: () async {
await Clipboard.setData(ClipboardData(text: _token));
},
child: Text("Copy token",style: Theme.of(context).textTheme.titleMedium)),
if(Platform.isAndroid) TextButton(
onPressed: () async {
await FlutterPushedMessaging.reconnect();
},
child: Text("Reconnect",style: Theme.of(context).textTheme.titleMedium)),
if(!kReleaseMode) TextButton(
onPressed: () async {
await Clipboard.setData(ClipboardData(text: await FlutterPushedMessaging.getLog()??""));
},
child: Text("Get Log",style: Theme.of(context).textTheme.titleMedium)),
],
)
),
),
);
}
}
更多关于Flutter推送消息插件flutter_pushed_messaging的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复