flutter_jpush 0.0.4

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • new70

pub package

flutter_jpush

Flutter 版本 jpush(极光推送),经过热心网友帮助,终于开发完成,目前功能比较稳定,已经上到pub。

ROADMAP

  • [x] ios
  • [x] android
  • [x] 集成notification
  • [x] 集成message
  • [x] 集成alias
  • [x] 集成tags
  • [ ] 后台接口放出服务
  • [x] 可以运行的例子
  • [ ] 用户可相互自行推送

准备工作

申请key

进入这里申请key

ios 证书申请

不熟悉怎么申请戳这里

集成

ios 集成

ios 修改 AppDelegate.m,新版本一行代码就可以集成了

增加
#include "FlutterJPushPlugin.h"

增加
 [self startupJPush:launchOptions appKey:@"你的key" channel:@"你的渠道" isProduction:是否生产版本];

全部的AppDelegate.m如下:

#include "AppDelegate.h"
#include "GeneratedPluginRegistrant.h"
#include "FlutterJPushPlugin.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

   [self startupJPush:launchOptions appKey:@"你的key" channel:@"你的渠道" isProduction:是否生产版本];
    [GeneratedPluginRegistrant registerWithRegistry:self];
  // Override point for customization after application launch.
  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}


@end

android 集成

修改 你的项目目录/android/app/build.gradle

android/defaultConfig节点修改manifestPlaceholders,新增极光推送key配置

android {
    .... 你的代码

    defaultConfig {
        .....
        manifestPlaceholders = [
               JPUSH_PKGNAME : applicationId,
               JPUSH_APPKEY : "你的极光推送key", //JPush上注册的包名对应的appkey.
               JPUSH_CHANNEL : "你的推送渠道,如果不知道填写developer-default即可",
        ]

    }

API调用

1、启动

dart:

在程序入口处做一下调用

void _startupJpush() async {
    print("初始化jpush");
    await FlutterJPush.startup();
    print("初始化jpush成功");
  }

2、获取设备唯一号

jpush在启动的时候会去连接自己的服务器,连接并注册成功之后会返回一个唯一的设备号(registrationId),

有两种方法可以得到

设置一个监听


FlutterJPush.addnetworkDidLoginListener((String registrationId) {
      setState(() {
        /// 用于推送
        print("收到设备号:$registrationId");
        this.registrationId = registrationId;
      });
    });

或者主动去取

await FlutterJPush.getRegistrationID()

主动去取有可能取出来是空的

3、设置tag

每一个设备可以对应多个tag


/// 设置tags
 FlutterJPush.setTags( ["tag1","tag2" );
 
/// 新增tag
 JPushResult result = await FlutterJPush.addTags(["tag1","tag2"]);

/// 获取所有tag
  FlutterJPush.getAllTags();

4、设置和获取alias

alias为别名,别名一般来说是惟一的。


/// 获取别名
FlutterJPush.getAlias().then((JPushResult result) {
      if (result.isOk) {
        setState(() {
          if (mounted) _text = r.result;
        });
      }
    });

/// 设置别名

 JPushResult result = await FlutterJPush.getAlias();
if (result.isOk) {
    //设置成功
}
            


5、收到通知提醒

要处理两个监听,一个是收到了通知提醒,并出现在状态栏上面;一个是用户点击了状态栏上面的提醒,打开通知。

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

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

        /// 打开了推送提醒
        notificationList.add(notification);
      });
    });

6、收到自定义消息

消息是服务端发送的一段代码,一般是json格式,app在收到消息之后,一般不直接做前台通知。

设置收到自定义消息的监听


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

        /// 打开了推送提醒
        notificationList.add(msg);
      });
    });

目前这个版本还存在编译问题,flutter官方也在积极解决

真机运行报错couldn't find "libflutter.so"

暂时的解决方法有:

build.gradle设置

ndk{
     abiFilters 'armeabi', 'armeabi-v7a'//, 'arm64-v8a'
}

或者可以增加编译选项:

--target-platform android-arm64 或者 --target-platform android-arm

还有疑问的话可以发issue或者加qq群854192563交流

0.0.4

  • ios 更加简单的集成方式

0.0.3

  • 完善文档
  • 修复一些拼写错误,感谢@YaoShuwei

0.0.2

  • 完善文档

0.0.1

  • 基本功能

example/lib/main.dart

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

import 'package:flutter/services.dart';
import 'package:flutter_jpush/flutter_jpush.dart';
import 'package:flutter_jpush_example/alias_test.dart';
import 'package:flutter_jpush_example/info_test.dart';
import 'package:flutter_jpush_example/push_test.dart';
import 'package:flutter_jpush_example/tag_test.dart';

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

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

class _MyAppState extends State<MyApp> {
  bool isConnected = false;
  String registrationId;
  List notificationList = [];

  int _index = 0;

  @override
  void initState() {
    super.initState();
    _startupJpush();

    FlutterJPush.addConnectionChangeListener((bool connected) {
      setState(() {
        /// 是否连接,连接了才可以推送
        print("连接状态改变:$connected");
        this.isConnected = connected;
        if (connected) {
          FlutterJPush.getRegistrationID().then((String regId) {
            print("主动获取设备号:$regId");
            setState(() {
              this.registrationId = regId;
            });
          });
        }
      });
    });

    FlutterJPush.addnetworkDidLoginListener((String registrationId) {
      setState(() {
        /// 用于推送
        print("收到设备号:$registrationId");
        this.registrationId = registrationId;
      });
    });

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

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

        /// 打开了推送提醒
        notificationList.add(notification);
      });
    });

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

        /// 打开了推送提醒
        notificationList.add(msg);
      });
    });
  }

  void _startupJpush() async {
    print("初始化jpush");
    await FlutterJPush.startup();
    print("初始化jpush成功");
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: const Text('JPush Example'),
        ),
        body: new IndexedStack(
          children: <Widget>[
            new Info(
              notificationList: notificationList,
              registrationId: registrationId,
              isConnected: isConnected,
            ),
            new TagSet(),
            new AliasSet(),
            new PushTest()
          ],
          index: _index,
        ),
        bottomNavigationBar: new BottomNavigationBar(
          items: <BottomNavigationBarItem>[
            new BottomNavigationBarItem(
                title: new Text("Info"), icon: new Icon(Icons.info)),
            new BottomNavigationBarItem(
                title: new Text("Tag"), icon: new Icon(Icons.tag_faces)),
            new BottomNavigationBarItem(
                title: new Text("Alias"), icon: new Icon(Icons.nature)),
          ],
          onTap: (int index) {
            setState(() {
              _index = index;
            });
          },
          currentIndex: _index,
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_jpush: ^0.0.4

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

Alternatively, your editor might support flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_jpush/flutter_jpush.dart';
  
Version Uploaded Documentation Archive
0.0.4 Sep 12, 2018 Go to the documentation of flutter_jpush 0.0.4 Download flutter_jpush 0.0.4 archive
0.0.3 Sep 11, 2018 Go to the documentation of flutter_jpush 0.0.3 Download flutter_jpush 0.0.3 archive
0.0.2 Sep 6, 2018 Go to the documentation of flutter_jpush 0.0.2 Download flutter_jpush 0.0.2 archive
0.0.1 Sep 6, 2018 Go to the documentation of flutter_jpush 0.0.1 Download flutter_jpush 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
52
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
70
Overall:
Weighted score of the above. [more]
70
Learn more about scoring.

We analyzed this package on Sep 18, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.12.3
  • Flutter: 0.8.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Suggestions

The description is too short.

Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Package is pre-v0.1 release.

While there is nothing inherently wrong with versions of 0.0.*, it usually means that the author is still experimenting with the general direction of the API.

Fix lib/flutter_jpush.dart.

Analysis of lib/flutter_jpush.dart reported 1 hint:

line 298 col 9: The value of the local variable 'map' isn't used.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.48.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11
meta 1.1.6
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8