Flutter Android 集成 阿里云推送

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值