1、在pubspec.yaml添加依赖
firebase_messaging:
flutter_local_notifications:
2.新建local_notification.dart,如下:
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import '../../../net/Api.dart';
class LocalNotification {
//静态变量_notification,存储唯一对象
static LocalNotification? _notification;
// notification的getter方法,通过LocalNotification.notification获取对象
static LocalNotification get notification => _getNotification();
//获取对象
static LocalNotification _getNotification() {
//使用私有的构造方法来创建对象
_notification ??= LocalNotification._initialize();
return _notification!;
}
FlutterLocalNotificationsPlugin np = FlutterLocalNotificationsPlugin();
// 私有的自定义命名式构造方法,通过它实现一个类;
LocalNotification._initialize() {
//初始化本地通知。设置安卓的logo,iOS不需要设置
np = FlutterLocalNotificationsPlugin();
//不能通过自定义图片设置logo
var android = const AndroidInitializationSettings('@mipmap/ic_logo');
var ios = const DarwinInitializationSettings();
np.initialize(InitializationSettings(iOS: ios, android: android));
}
void send({required String title, required String body}) async {
//在iOS13以上需要申请权限
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()?.requestPermission();
// 构建描述,暂不需要另外修改
var androidDetails = const AndroidNotificationDetails('ID', 'Name',
importance: Importance.max, priority: Priority.high);
var iosDetails = const DarwinNotificationDetails();
var details = NotificationDetails(android: androidDetails, iOS: iosDetails);
//第一个参数为通知的id,不能重复,否则通知会被替换
np.show(DateTime
.now()
.millisecondsSinceEpoch >> 10, title, body, details);
}
//添加推送相关信息
initializePushNotification() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
NotificationSettings settings =
await FirebaseMessaging.instance.requestPermission(
alert: true,
announcement: false,
badge: false,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
debugPrint('User granted permission: ${settings.authorizationStatus}');
final fcmToken = await FirebaseMessaging.instance.getToken();
if (fcmToken != null) {
//把token传给后台
// Api.showToast(msg: '推送Token:$fcmToken');
debugPrint('fcmToken ========== $fcmToken');
}
// LocalNotification.notification.send(
// title: '测试测试浿',
// body: '朝秦暮楚械');
_getPushNotificationNews();
}
//收到推送的信息
_getPushNotificationNews() {
//前台
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
if (message.notification != null) {
//前台时收到通知转为本地推送
LocalNotification.notification.send(
title: '${message.notification?.title}',
body: '${message.notification?.body}');
}
});
}
}
3、在项目中代码引入推送
LocalNotification.notification.initializePushNotification();
android端配置
1、进入Google firebase 官网(https://console.firebase.google.com/)注册帐号创建项目和应用
下载生成的google-services.json文件,并将其放置在android / app中
2、在android/build.gradle文件下添加google server依赖文件 classpath 'com.google.gms:google-services:4.3.2' 如下:
3、在android/app/build.gradle文件下添加apply plugin: 'com.google.gms.google-services'和implementation 'com.google.firebase:firebase-messaging:20.2.4',如下:
4、在AndroidManifest.xml下添加
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
<uses-permission android:name="android.permission.READ_SMS" />
如下:
5、在AndroidManifest.xml下添加
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@mipmap/ic_logo" />和
<action android:name="FLUTTER_NOTIFICATION_CLICK"/>
@mipmap/ic_logo为通知和图标mipmap文件夹下
如下:
这样android端配置完成.
iOS端配置
1、生成推送证书:打开苹果开发者网站(https://developer.apple.com)。从 Member Center 进入 Certificates, Identifiers & Profiles。
对于开发环境(sandbox)的推送证书,请选择 Apple Push Notification service SSL (Sandbox);
对于生产环境(production)的推送证书,请选择 Apple Push Notification service SSL (Production)。
2、进入Google firebase 官网(https://console.firebase.google.com/)将iOS应用添加到您的项目中,在IOS项目下云消息传递中上传证书,如下:
3、下载生成的GoogleService-Info.plist文件,打开ios /带有Xcode的Runner.xcworkspace,并在Xcode中将文件放置在ios / Runner中,如下:
4、在Xcode更新你的证书,并勾选romote notifications和Backgroid processing两个选项,如下
这样IOS端配置完成.