在Flutter中集成极光推送的iOS厂商推送服务(APNs),主要步骤如下:
-
配置极光推送账号和iOS证书
- 登录极光推送控制台,创建应用并上传APNs证书(开发/生产环境)
- 记录应用的AppKey
-
添加依赖
在pubspec.yaml中添加:
dependencies:
jpush_flutter: ^2.4.0
-
iOS原生配置
- 在Xcode中开启Push Notifications能力
- 在
Info.plist中添加:
<key>JPUSHChannel</key>
<string>developer-default</string>
-
Flutter代码集成
import 'package:jpush_flutter/jpush_flutter.dart';
final JPush jpush = JPush();
void initJPush() async {
// 初始化
await jpush.setup(
appKey: "你的AppKey",
channel: "developer-default",
production: false, // 开发环境false,生产环境true
);
// 监听消息
jpush.addEventHandler(
onReceiveNotification: (Map<String, dynamic> message) async {
print("接收到推送: $message");
},
onOpenNotification: (Map<String, dynamic> message) async {
print("点击通知: $message");
},
);
}
-
请求通知权限
await jpush.applyPushAuthority(
NotificationSettingsIOS(
sound: true,
alert: true,
badge: true,
),
);
-
获取RegistrationID
String? registrationId = await jpush.getRegistrationID();
注意事项:
- 确保iOS证书与Bundle ID匹配
- 生产环境需要切换
production参数为true
- 真机测试需要配置开发者账号和推送证书
完成以上步骤后,即可通过极光控制台向iOS设备发送推送通知。