前言:
Flutter 消息推送是指在應用程序向設備發送通知或消息的過程,主要用於嚮應用用戶傳遞重要信息、提醒或更新等。Flutter 消息推送有很多種方式,在這裏要跟大家分享的是 Flutter 集成 Mapp 實現了 android 和 iOS 消息推送的功能,更多的消息推送方式請參考:Flutter 集成 FCM 推送功能 和 Flutter 集成 Rammus 實現阿里雲推送的功能。
實現的步驟:
1. 在 pubspec.yaml 文件添加插件
cupertino_icons: ^1.0.2
mapp_sdk: 0.0.8
2. Android 集成
2.1 Project 的 build.gradle
classpath 'com.google.gms:google-services:4.3.15'
2.2 Module 的 build.gradle
apply plugin: 'com.android.application'
//添加
apply plugin: 'com.google.gms.google-services'
2.3 在 AndroidManifest.xml 文件設置啓動模式爲singleTop
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
2.4 在 MainActivity 添加代碼
class MainActivity : FlutterFragmentActivity() {
override fun onCreate(@Nullable savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
MappSdkPlugin.handleIntent(
this,
intent
)
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
MappSdkPlugin.handleIntent(
this,
intent
)
}
}
2.5 Module 的 app 目錄下創建 proguard-rules.pro 混淆文件 (集成 Mapp sdk 需要添加混淆規則,否則打包 apk 會運行不了)
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-keepclasseswithmembernames class * {
native <methods>;
}
# 添加support.v7的混淆規則
-dontwarn android.support.v7.**
-keep class android.support.v7.** { *; }
-keep interface android.support.v7.app.** { *; }
-keep public class * extends android.support.v7.**
# 添加AndroidX的混淆規則
-dontwarn androidx.**
-keep class androidx.** { *; }
# 添加Mapp sdk的混淆規則
-keep class com.mapp.flutter.sdk.** { *; }
-keep class com.appoxee.** { *; }
# 保留mapp-sdk包中所有公共和受保護的方法
-keepclassmembers class com.mapp.flutter.sdk.** {
public protected *;
}
-keepclassmembers class com.appoxee.** {
public protected *;
}
# 保留mapp-sdk包中的所有字段
-keepclassmembers class com.mapp.flutter.sdk.** {
<fields>;
}
-keepclassmembers class com.appoxee.** {
<fields>;
}
2.6 Module 的 build.gradle 開啓混淆
buildTypes {
release {
signingConfig signingConfigs.release
shrinkResources true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
3. iOS 集成
3.1 在 ios/Podfile 文件添加
platform :ios, '10.0'
target 'Runner' do
# use_frameworks!
use_modular_headers!
flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
end
3.2 安裝插件命令
>cd ios
>pod install
>arch -x86_64 pod install --repo-update (M1 mac)
>flutter run
3.3 在 Xcode 項目的 Signing & Capabilities 頁面,啓用 Push Notification 和 Remote Notifications 通知功能
3.4 在 AppoxeeConfig.plist 添加
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>inapp</key>
<dict>
<key>custom_fields</key>
<array>
<string>customString</string>
<string>customNumber</string>
<string>customDate</string>
</array>
<key>media_timeout</key>
<integer>5</integer>
</dict>
<key>sdk</key>
<dict>
<key>app_id</key>
<string>your_app_id</string>
<key>dmc_system_id</key>
<integer>your_dmc_system_id</integer>
<key>sdk_key</key>
<string>your_sdk_key</string>
<key>is_eu</key>
<true/>
<key>open_landing_page_inside_app</key>
<false/>
<key>apx_open_url_internal</key>
<string>YES</string>
</dict>
</dict>
</plist>
4. 在dart 代碼調用 Mapp sdk
4.1 初始化 Mapp sdk
(1) Mapp 平臺註冊應用會提供 sdkKey、appID、tenantID
(2) googleProjectId 就是 Google service.json 中 的 project_id
@override
void initState() {
super.initState();
//初始化Mapp sdk ,
MappSdk.engage(Config.sdkKey, Config.googleProjectId, Config.server,
Config.appID, Config.tenantID);
initPlatformState();
}
initPlatformState() async {
String? platformVersion;
if (!mounted) return;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
platformVersion = await MappSdk.platformVersion;
} on PlatformException {
platformVersion = '無法獲取 Mapp sdk 版本';
if (kDebugMode) {
print("Mapp 推送測試: $platformVersion");
}
}
}
4.2 獲取設備別名
MappSdk.getAlias().then((String value) => {
//TO DO
});
4.3 開啓 Mapp 通知
MappSdk.isPushEnabled().then((bool value) =>{
//TO DO
});
4.4 開啓 Opt
MappSdk.setPushEnabled(true);
4.5 關閉 Opt
MappSdk.setPushEnabled(false);
4.6 退出 Mapp
MappSdk.logOut(true).then((String? result) =>{
//TO DO
});
總結:Flutter 集成 Mapp 實現了 android 和 iOS 消息推送的功能,如果大家有什麼疑問的話,歡迎留言聯繫我。整理不易,轉載請標明出處,謝謝!