68-Flutter Aurora using push

1, apply for an account and build applications aurora

Aurora push the official site at: https: //www.jiguang.cn/

Registration good, go to 'service center', then enter "developer platform ', click Create Application.

This time there will be a new page that lets you fill out the "application name" and upload "application icon."

Created, Aurora platform will give us two key.

  • appKey: key mobile client used
  • Master Secret: key end use of the service

We are here not only mobile end server, so only need appKey. Get this Key operating platform can be considered finished Aurora

2, was added dependencies dependent

github URL: https: //github.com/jpush/jpush-flutter-plugin

To use the Aurora push plug-in must first download the package, the package will need to be downloaded add-dependent, directly to the following code to pubspec.yaml file.

jpush_flutter: 0.0.11

After writing the code, select the top right corner of the Android Studio Packages get to download, to operate after the download is complete.

3, build.gradle can add and cpu model code

Open android / app / src / build.gradle file, add the following code:

defaultConfig {
    applicationId "sscai.club.flutter_shop"
    minSdkVersion 16
    targetSdkVersion 28
    versionCode flutterVersionCode.toInteger()
    versionName flutterVersionName
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    /*新加入的*/
    ndk {
        /*选择要添加的对应 cpu 类型的 .so 库。
        abiFilters 'armeabi''armeabi-v7a''x86''x86_64''mips''mips64'// 'arm64-v8a',
        /*还可以添加
    }

    manifestPlaceholders = [
        JPUSH_PKGNAME: applicationId,
        JPUSH_APPKEY : "这里写入你自己申请的Key哦", /*NOTE: JPush 上注册的包名对应的 Appkey.*/
        JPUSH_CHANNEL: "developer-default", /*暂时填写默认值即可.*/
    ]
    /*新加入的*/
}

For details, see: https: //github.com/jpush/jpush-flutter-plugin

4, the main coding

In dependence of the introduced main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:jpush_flutter/jpush_flutter.dart';

The method of preparation of initPlatformState

Future<void> initPlatformState() async {
    String platformVersion;

    try {
      /*监听响应方法的编写*/
      jpush.addEventHandler(
          onReceiveNotification: (Map<Stringdynamic> message) async {
            print(">>>>>>>>>>>>>>>>>flutter 接收到推送: $message");
            setState(() {
              debugLable = "接收到推送: $message";
            });
          }
      );

    } on PlatformException {
      platformVersion = '平台版本获取失败,请检查!';
    }

    if (!mounted){
      return;
    }

    setState(() {
      debugLable = platformVersion;
    });
}

Write build views

@override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: const Text('极光推送'),
        ),
        body: new Center(
            child: new Column(
                children:[
                  new Text('结果: $debugLable\n'),
                  new RaisedButton(
                      child: new Text(
                          '点击发送推送消息\n',
                      ),
                      onPressed: () {
                        /*三秒后出发本地推送*/
                        var fireDate = DateTime.fromMillisecondsSinceEpoch(DateTime.now().millisecondsSinceEpoch + 3000);
                        var localNotification = LocalNotification(
                          id: 234,
                          title: '我是推送测试标题',
                          buildId: 1,
                          content: '看到了说明已经成功了',
                          fireTime: fireDate,
                          subtitle: '一个测试',
                        );
                        jpush.sendLocalNotification(localNotification).then((res) {
                          setState(() {
                            debugLable = res;
                          });
                        });
                      }),
                ]
            )
        ),
      ),
    );
  }

main.dart complete code:

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:jpush_flutter/jpush_flutter.dart';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp{
  String debugLable = 'Unknown';   /*错误信息*/
  final JPush jpush = new JPush(); /* 初始化极光插件*/
  @override
  void initState() {
    super.initState();
    initPlatformState();  /*极光插件平台初始化*/
  }


  Future<void> initPlatformState() async {
    String platformVersion;

    try {
      /*监听响应方法的编写*/
      jpush.addEventHandler(
          onReceiveNotification: (Map<Stringdynamic> message) async {
            print(">>>>>>>>>>>>>>>>>flutter 接收到推送: $message");
            setState(() {
              debugLable = "接收到推送: $message";
            });
          }
      );

    } on PlatformException {
      platformVersion = '平台版本获取失败,请检查!';
    }

    if (!mounted){
      return;
    }

    setState(() {
      debugLable = platformVersion;
    });
  }

  /*编写视图*/
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: const Text('极光推送'),
        ),
        body: new Center(
            child: new Column(
                children:[
                  new Text('结果: $debugLable\n'),
                  new RaisedButton(
                      child: new Text(
                          '点击发送推送消息\n',
                      ),
                      onPressed: () {
                        /*三秒后出发本地推送*/
                        var fireDate = DateTime.fromMillisecondsSinceEpoch(DateTime.now().millisecondsSinceEpoch + 3000);
                        var localNotification = LocalNotification(
                          id: 234,
                          title: '我是推送测试标题',
                          buildId: 1,
                          content: '看到了说明已经成功了',
                          fireTime: fireDate,
                          subtitle: '一个测试',
                        );
                        jpush.sendLocalNotification(localNotification).then((res) {
                          setState(() {
                            debugLable = res;
                          });
                        });
                      }),
                ]
            )
        ),
      ),
    );
  }
}

Renderings:

4, several methods extension

Receive Push Notification

Monitor addReceiveNotificationListener method:

/*
* 收到推送提醒
* */

  void _ReceiveNotification() async {
    FlutterJPush.addReceiveNotificationListener(
        (JPushNotification notification) {
      setState(() {
        /// 收到推送
        print("收到推送提醒: $notification");
      });
    });
  }

Open Push Notification

Monitor addReceiveNotificationListener method:

 /*
  * 打开推送提醒
  * */

  void _OpenNotification() async {
    FlutterJPush.addReceiveOpenNotificationListener(
        (JPushNotification notification) {
      setState(() {
        print("打开了推送提醒: $notification");
      });
    });
  }

Defined message received from the monitor

General project This method used more of it! ! !

Monitor addReceiveCustomMsgListener method:

  /*
  * 监听接收自定义消息
  * */

  void _ReceiveCustomMsg() async {
    FlutterJPush.addReceiveCustomMsgListener((JPushMessage msg) {
      setState(() {
        print("收到推送消息提醒: $msg");
      });
    });
  }

Guess you like

Origin www.cnblogs.com/niceyoo/p/11095994.html
Recommended