Flutter 3.27.4
aliyun_push: ^0.1.7 https://pub.dev/packages/aliyun_push
1. 在Flutter工程的android模块下的AndroidManifest.xml
文件中设置AppKey、AppSecret:
<application android:name="*****">
<!-- 请填写你自己的- appKey -->
<meta-data android:name="com.alibaba.app.appkey" android:value="*****"/>
<!-- 请填写你自己的appSecret -->
<meta-data android:name="com.alibaba.app.appsecret" android:value="****"/>
<receiver android:name="com.aliyun.ams.push.AliyunPushMessageReceiver" android:exported="false">
<intent-filter>
<action android:name="com.alibaba.push2.action.NOTIFICATION_OPENED" />
</intent-filter>
<intent-filter>
<action android:name="com.alibaba.push2.action.NOTIFICATION_REMOVED" />
</intent-filter>
<intent-filter>
<action android:name="com.alibaba.sdk.android.push.RECEIVE" />
</intent-filter>
</receiver>
</application>
2. 代码:
final _aliyunPush = AliyunPush();
void init(){
String appKey = "填写自己iOS项目的appKey";
String appSecret = "填写自己iOS项目的appSecret";
_aliyunPush
.initPush(appKey: _appKey, appSecret: _appSecret)
.then((initResult) {
var code = initResult['code'];
if (code == kAliyunPushSuccessCode) {
success();
} else {
String errorMsg = initResult['errorMsg'];
fail(errorMsg);
}
});
_aliyunPush.addMessageReceiver(
onNotification: _onNotification,
onNotificationOpened: _onNotificationOpened,
onNotificationRemoved: _onNotificationRemoved,
onMessage: _onMessage,
);
}
Future<void> _onNotification(Map<dynamic, dynamic> message) async {
debugPrint('===========>>onNotification: $message');
}
Future<void> _onMessage(Map<dynamic, dynamic> message) async {
debugPrint('===========>>onMessage: $message');
}
Future<void> _onNotificationOpened(Map<dynamic, dynamic> message) async {
debugPrint('===========>>onNotificationOpened: $message');
}
Future<void> _onNotificationRemoved(Map<dynamic, dynamic> message) async {
debugPrint('===========>>onNotificationRemoved: $message');
}
3. java 服务端: 开发文档 https://help.aliyun.com/document_detail/2249910.html
void push() throws Exception {
com.aliyun.push20160801.Client client = new com.aliyun.push20160801.Client(
new Config()
.setAccessKeyId("*******")
.setAccessKeySecret("*******")
.setEndpoint("cloudpush.aliyuncs.com"));
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
com.aliyun.push20160801.models.PushRequest pushRequest = new com.aliyun.push20160801.models.PushRequest()
.setAppKey(*******)
.setBody("测试内容")
.setTitle("测试标题")
.setPushType("NOTICE")
.setDeviceType("ALL")
.setTarget("ALL")
.setStoreOffline(true)
.setTargetValue("1");
try {
client.pushWithOptions(pushRequest, runtime);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
System.out.println(error.getMessage());
com.aliyun.teautil.Common.assertAsString(error.message);
}
}