跳到主要内容

推送概述

Flutter 仅封装了设置推送参数的接口,推送功能依赖 Android、iOS 平台的推送能力。

集成推送功能

请参考 Android、iOS 端的推送文档进行集成。

设置 Android 第三方推送


class RCIMIWPushOptions {
// 小米推送 Id
String? idMI;
// 小米推送 appKey
String? appKeyMI;
// 魅族推送 Id
String? appIdMeizu;
// 魅族推送 appKey
String? appKeyMeizu;
// Oppo 推送 appKey
String? appKeyOPPO;
// Oppo 推送 appSecret
String? appSecretOPPO;
// 是否开启华为推送
bool? enableHWPush;
// 是否开启 FCM 推送
bool? enableFCM;
// 是否开启 Vivo 推送
bool? enableVIVOPush;
}

代码示例

RCIMIWEngineOptions options = RCIMIWEngineOptions.create();
RCIMIWPushOptions pushOptions = RCIMIWPushOptions.create(
idMI: AndroidPushInfo.idMI,
appKeyMI: AndroidPushInfo.appKeyMI,
appIdMeizu: AndroidPushInfo.appIdMeizu,
appKeyMeizu: AndroidPushInfo.appKeyMeizu,
appKeyOPPO: AndroidPushInfo.appKeyOPPO,
appSecretOPPO: AndroidPushInfo.appSecretOPPO,
enableHWPush: true,
enableFCM: false,
enableVIVOPush: true,
);
options.pushOptions = pushOptions;
RCIMIWEngine e = await RCIMIWEngine.create(appkey, options);

安卓厂商推送,除了调用上述接口外,还需要在原生层执行相关配置。安卓推送

iOS 推送

前期准备

参考 iOS 推送

请求推送权限

- (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
// App 启动后向用户请求推送权限
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
//点击允许
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {

}];

} else {

}
}];
} else if ([[UIDevice currentDevice].systemVersion floatValue] > 8.0) {
//iOS8 - iOS10
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge categories:nil]];
}

// 注册获得 device Token
[application registerForRemoteNotifications];
- (void)application:(UIApplication )application didRegisterUserNotificationSettings:(UIUserNotificationSettings )notificationSettings{
[application registerForRemoteNotifications];
}

设置 deviceToken

- (void)application:(UIApplication )application didRegisterForRemoteNotificationsWithDeviceToken:(NSData )deviceToken {
// Flutter SDK 内部已经执行了 iOS 原生 SDK setDeviceToken 方法,用户无需调用
}