Flutter Android 集成 阿里云推送

350 阅读1分钟

Flutter集成阿里云推送指南

Flutter 3.27.4版本下使用aliyun_push: ^0.1.7插件实现阿里云推送功能。

1. Android配置

在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. Flutter代码实现

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. iOS配置注意事项

对于iOS开发者,在集成推送功能时,除了配置阿里云推送外,还需要注意:

  1. 确保在Xcode中正确配置推送证书
  2. 在AppUploader等iOS开发助手中检查推送权限是否开启
  3. 测试环境建议使用AppUploader的沙盒环境进行推送测试

4. Java服务端实现

参考阿里云官方文档实现服务端推送:

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);
    }
}

通过以上步骤,您可以在Flutter应用中成功集成阿里云推送功能。对于iOS开发者,使用AppUploader等工具可以更高效地管理证书和推送配置,提升开发效率。