Android 极光推送

官网:极光 Aurora Mobile-中国领先的企业营销增长服务供应商 ,助力企业运营、增长和变现

SDK地址:https://github.com/jpush/jpush-flutter-plugin

jpush_flutter | Flutter PackageSDK地址:jpush_flutter | Flutter Package

// pub 集成
dependencies:
  jpush_flutter: 2.2.3

配置 

Android:

在 /android/app/build.gradle 中添加下列代码

android: {
  ....
  defaultConfig {
    applicationId "替换成自己应用 ID"
    ...
    ndk {
	//选择要添加的对应 cpu 类型的 .so 库。
	abiFilters 'armeabi', 'armeabi-v7a', 'x86', 'x86_64', 'mips', 'mips64', 'arm64-v8a',        
    }

    manifestPlaceholders = [
        JPUSH_PKGNAME : applicationId,
        JPUSH_APPKEY : "appkey", // NOTE: JPush 上注册的包名对应的 Appkey.
        JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.
    ]
  }  

build.gradle替换源

buildscript {
    repositories {
//        google()
//        mavenCentral()
        maven { url 'https://maven.aliyun.com/repository/public'}
        maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
        maven { url 'https://maven.aliyun.com/repository/google' }
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.0'
    }
}

allprojects {
    repositories {
//        google()
//        mavenCentral()
        maven { url 'https://maven.aliyun.com/repository/public'}
        maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
        maven { url 'https://maven.aliyun.com/repository/google' }
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
    }
}

创建应用

 填入包名

代码段:  JPushUtils

class JPushUtils {
  JPush? jpush;
  JPushImplements? jPushImplements;
  // static _instance,_instance会在编译期被初始化,保证了只被创建一次
  static final JPushUtils _instance = JPushUtils._internal();

  factory JPushUtils() {
    return _instance;
  }

  setJPushImplements(JPushImplements ji){
    jPushImplements = ji;
  }
  // 通过私有方法_internal()隐藏了构造方法,防止被误创建
  JPushUtils._internal() {
    // 初始化
    initPlatformState();
  }

  Future<void> initPlatformState() async {
    jpush = new JPush();
    String? platformVersion;
    try {
      jpush!.addEventHandler(
        // 接收通知回调方法。
        onReceiveNotification: (Map<String, dynamic> message) async {
          print("flutter onReceiveNotification: $message");
        },
        // 点击通知回调方法。
        onOpenNotification: (Map<String, dynamic> message) async {
          print("flutter onOpenNotification: $message");
          String title = message['title'];
          String content = message['alert'];
          print("message['extras']: $message['extras']");
          if(message['extras'] != null){
            dynamic object = convert.jsonDecode(message['extras']['cn.jpush.android.EXTRA']);
            jPushImplements!.clickMessage(object);
          }
        },
        // 接收自定义消息回调方法。
        onReceiveMessage: (Map<String, dynamic> message) async {
          print("flutter onReceiveMessage: $message");
        },
      );

      jpush!.setup(
        appKey: "XXXXXXXXX",
        channel: "theChannel",
        production: false,
        debug: false, // 设置是否打印 debug 日志
      );

      jpush!.applyPushAuthority(
          new NotificationSettingsIOS(sound: true, alert: true, badge: true));

      // Platform messages may fail, so we use a try/catch PlatformException.
      jpush!.getRegistrationID().then((rid) {
        print("获得ID flutter get registration id : $rid");
      });

    } on PlatformException {
      print('JPUSH init error !!!');
    }
  }

  sendLocalMessage(){
    var fireDate = DateTime.fromMillisecondsSinceEpoch(DateTime.now().millisecondsSinceEpoch + 1000);
    var localNotification = LocalNotification(
        id: 234,
        title: 'notification title',
        buildId: 1,
        content: 'notification content',
        fireTime: fireDate,
        subtitle: 'notification subtitle', // 该参数只有在 iOS 有效
        badge: 5
    );
    jpush!.sendLocalNotification(localNotification).then((res) {});
  }

  /// 字符串转json
  void string2Json(String str){
    return convert.jsonDecode(str);
  }
}
JPushImplements
abstract class JPushImplements {
  clickMessage(dynamic data);
}
class JPushDemo extends StatefulWidget {
  const JPushDemo({Key? key}) : super(key: key);

  @override
  _JPushDemoState createState() => _JPushDemoState();
}

class _JPushDemoState extends State<JPushDemo> implements JPushImplements {

  @override
  void initState() {
    super.initState();
    JPushUtils().setJPushImplements(this);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('A'),
      ),
      body: Container(
        child: Text('B'),
      ),
    );
  }

  @override
  clickMessage(data) {
    print(data);
  }
}

版权声明:本文为diangedan原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
THE END
< <上一篇
下一篇>>