flutter_极光推送android

flutter极光推送集成Android端

  • 前言
  • 集成需知
  • 上源码

前言:对于flutter的学习我也是刚学,中间遇到很多关于代码的问题,我是抽时间将公司的一个正在开发的Android项目转过来,这是我个人的计划,公司不知道的。所以中间会边搞其他边学习,现在我主要解决的问题就是加密解密,极光推送,地图导航,地图定位,线路规划,小车轨迹运动。期间也碰到好的博客,给大家分享下。
flutter_mvp
代码快捷键
dart语法

集成需知:对于flutter问题解答最好去flutter中文网,掘金上有很多分享,但很多都是皮毛,大概看下就好。我说下自己遇到的坑和解决方法,1.dart语法要不断学习深究,因为现在flutter的插件库很少,很多要去看插件源码才会调用里面的函数。2.经常遇到编译错误时就不要热加载,重新运行才会出结果。3.多敲代码,复制粘贴会降低你的理解,毕竟dart语法在flutter中的使用还是很新的。

上源码:上flutter中文网仓库里搜,不管加密解密,地图,推送都有,但很多版本迭代还甚少,地图可以用高德,推送就极光,微信一套都有了。
编写代码第一步
1.在libs包下创建文件夹jpush,在文件夹下创建dart文件JpushPage.dart,下面先贴全部源码,再细说

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

void main() {
  runApp(new MaterialApp(
    title: 'Jpush_test1',
    theme: new ThemeData(
      primarySwatch: Colors.blue,
    ),
    home: new JpushApp(),
  ));
}

class JpushApp extends StatefulWidget {
  @override
  _JpushAppState createState() => new _JpushAppState();
}

class _JpushAppState extends State<JpushApp> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _startupJpush();
    _setPushTag();
    _addEventHandler();
    _getRegisterID();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Jpush_test1'),
      ),
      body: Column(
        children: <Widget>[
          Text('极光后台来的msg=$myMsg'),
        ],
      ),
    );
  }

  JPush jPush = new JPush();
  String registerId = null;
  String myMsg;

  _startupJpush() {
    jPush.setup(appKey: "04c4b9de0a45f596e107a860", debug: true);
  }

  _getRegisterID() async {
    registerId = await jPush.getRegistrationID();
    print('registerid=' + registerId);
    return registerId;
  }

  _setPushTag() {
    List<String> tags = List<String>();
    tags.add("jason");
    jPush.setTags(tags);
  }

  _addEventHandler() {
// Future<dynamic>event;
    jPush.addEventHandler(onReceiveNotification: (Map<String, dynamic> event) {
      print('addOnreceive>>>>>>$event');
      //addOnreceive>>>>>>{extras: {cn.jpush.android.ALERT_TYPE: 7, cn.jpush.android.EXTRA: {},
      // cn.jpush.android.MSG_ID: 255447735, cn.jpush.android.ALERT: we are the best!, cn.jpush.android.NOTIFICATION_ID: 255447735}, alert: we are the best!, title: flutter_mvp}
      String msg="$event";
      _refreshData(msg);
    }, onOpenNotification: (Map<String, dynamic> event) {
      print('addOpenNoti>>>>>$event'); //分别测试过无效的
      print(event.toString());
    }, onReceiveMessage: (Map<String, dynamic> event) {
      print('addReceiveMsg>>>>>$event'); //无效的
      print(event.toString());
    });
  }

  //更新某个控件显示
  _refreshData(String msg) {
    setState(() {
      myMsg = msg;
    });
  }
}

1.去网站搜flutter极光推送,看里面的配置教程:a.flutter项目导入极光库 jpush_flutter: ^0.0.9 ,b.android项目的buildgradle文件下(在整个项目下打开Android项目的包,找到MainActivity后打开会有蓝色字体(open for editing in android studio)点击后独立打开Android项目这时编辑文件不会报红。。。),c.输入包名对应的极光appkey,

2.在JpushPage文件中鼠标右键run_>JpushPage.
3.编写代码后,进入极光后台推送消息,代码里的注释你们可以自己试下,有些我还不明白的,onOpenNotification,onReceiveMessage不起作用,LocalNotification还没试过,希望其他码农搞下给分更详细的,我这里设置的tag=jason,推送的消息=we are the best!,

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,下面是Flutter集成极光推送的步骤: 1. 在极光官网注册账号并创建应用,获取AppKey。 2. 在Flutter项目中添加极光推送插件,如:jpush_flutter。 3. 在AndroidManifest.xml文件中添加以下代码: ```xml <!-- 极光推送权限 --> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 极光推送服务 --> <service android:name="cn.jpush.android.service.PushService" android:enabled="true" android:exported="false" android:process=":pushcore" > <intent-filter> <action android:name="cn.jpush.android.intent.REGISTER" /> <action android:name="cn.jpush.android.intent.REPORT" /> <action android:name="cn.jpush.android.intent.PushService" /> <action android:name="cn.jpush.android.intent.PUSH_TIME" /> </intent-filter> </service> <receiver android:name="cn.jpush.android.service.PushReceiver" android:enabled="true" android:exported="false" > <intent-filter android:priority="1000"> <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" /> <category android:name="你的包名" /> </intent-filter> <intent-filter> <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED_PROXY" /> <category android:name="你的包名" /> </intent-filter> <intent-filter> <action android:name="cn.jpush.android.intent.REGISTRATION" /> <category android:name="你的包名" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.USER_PRESENT" /> <category android:name="你的包名" /> </intent-filter> <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> <category android:name="你的包名" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.PACKAGE_ADDED" /> <data android:scheme="package" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.PACKAGE_REMOVED" /> <data android:scheme="package" /> </intent-filter> </receiver> ``` 4. 在iOS项目中,到极光官网下载JPush SDK并导入到项目中,在AppDelegate.m文件中添加以下代码: ```objective-c #import "JPUSHService.h" // iOS 10 及以上需导入 UserNotifications.framework #import <UserNotifications/UserNotifications.h> // Override point for customization after application launch. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 初始化 JPush SDK [JPUSHService setupWithOption:launchOptions appKey:@"your_appkey" channel:nil apsForProduction:NO]; return YES; } // 注册APNs成功并上报DeviceToken - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { [JPUSHService registerDeviceToken:deviceToken]; } // 实现注册APNs失败接口 - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error); } // 添加处理APNs通知回调方法 #ifdef NSFoundationVersionNumber_iOS_9_x_Max // iOS 10之前收到通知 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { // Required, iOS 7 Support [JPUSHService handleRemoteNotification:userInfo]; completionHandler(UIBackgroundFetchResultNewData); } // iOS 10之前收到本地通知 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { // Required, iOS 7 Support [JPUSHService showLocalNotificationAtFront:notification identifierKey:nil]; } // iOS 10及以上收到通知 - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler { NSDictionary * userInfo = notification.request.content.userInfo; if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { [JPUSHService handleRemoteNotification:userInfo]; } completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound); } // iOS 10及以上收到本地通知 - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler { NSDictionary * userInfo = response.notification.request.content.userInfo; if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { [JPUSHService handleRemoteNotification:userInfo]; } completionHandler(); } #endif ``` 5. 在Flutter代码中,初始化JPush并监听推送事件: ```dart import 'package:jpush_flutter/jpush_flutter.dart'; // 初始化JPush JPush jpush = new JPush(); // 监听推送事件 jpush.addEventHandler( onReceiveNotification: (Map<String, dynamic> message) async { print("Flutter onReceiveNotification: $message"); }, onOpenNotification: (Map<String, dynamic> message) async { print("Flutter onOpenNotification: $message"); }, onReceiveMessage: (Map<String, dynamic> message) async { print("Flutter onReceiveMessage: $message"); }, ); // 启动JPush jpush.setup( appKey: "your_appkey", // 极光官网申请的AppKey channel: "developer-default", // 渠道,默认值为“developer-default” production: false, // 是否生产环境 debug: true, // 是否开启调试模式 ); ``` 以上就是Flutter集成极光推送的步骤,希望能对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值