Flutter - 本地推送

使用庫:flutter_local_notifications

在pubspec.yaml中導(dǎo)入:(由于JPush有bug 暫時注釋 等待修復(fù))

dependencies:
  # jpush_flutter: 0.0.8
  # jpush_flutter:
  #   git:
  #     url: git://github.com/jpush/jpush-flutter-plugin.git
  #     ref: master

  #本地推送
  flutter_local_notifications: ^1.1.5+1

示例代碼:

import 'package:flutter/material.dart';
void main() => runApp(LocalNotification());

//本地推送
// import 'package:jpush_flutter/jpush_flutter.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';

class LocalNotification extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: LocalNotificationPage(title: 'Flutter Demo Home Page'),
    );
  }
}

class LocalNotificationPage extends StatefulWidget {
  LocalNotificationPage({Key key, this.title}) : super(key: key);
  final String title;
  @override
  LocalNotificationPageState createState() => LocalNotificationPageState();
}

class LocalNotificationPageState extends State<LocalNotificationPage> {
  int _counter = 0;

  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
      new FlutterLocalNotificationsPlugin();
  // JPush jpush = new JPush();

  void _incrementCounter() async {
    // var fireDate = DateTime.fromMillisecondsSinceEpoch(
    //     DateTime.now().millisecondsSinceEpoch);
    // var localNotification = LocalNotification(
    //     id: 234,
    //     title: '測試本地推送',
    //     buildId: 1,
    //     content: '我是本地推送的消息',
    //     fireTime: fireDate,
    //     subtitle: 'ios 消息推送', // 該參數(shù)只有在 iOS 有效
    //     badge: 5, // 該參數(shù)只有在 iOS 有效
    //     extra: {"fa": "0"} // 設(shè)置 extras ,extras 需要是 Map<String, String>
    //     );
    // jpush.sendLocalNotification(localNotification).then((res) {});

    var scheduledNotificationDateTime =
        new DateTime.now().add(new Duration(seconds: 5));
    var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
        'your other channel id',
        'your other channel name',
        'your other channel description');
    var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
    NotificationDetails platformChannelSpecifics = new NotificationDetails(
        androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
    await flutterLocalNotificationsPlugin.schedule(
        0,
        'scheduled title',
        'scheduled body',
        scheduledNotificationDateTime,
        platformChannelSpecifics);

    setState(() {
      _counter++;
    });
  }

  Future onDidReceiveLocalNotification(
      int i, String payload, String payloa3, String payload2) async {
    print('onDidReceiveLocalNotification');
  }

  Future onSelectNotification(String payload) async {
    print('onSelectNotification');
    if (payload != null) {
      debugPrint('notification payload: ' + payload);
    }
    // await Navigator.push(
    //   context,
    // new MaterialPageRoute(builder: (context) => new SecondScreen(payload)),
    // );
  }

  @override
  Widget build(BuildContext context) {
    var initializationSettingsAndroid =
        new AndroidInitializationSettings('app_icon');
    var initializationSettingsIOS = IOSInitializationSettings(
        onDidReceiveLocalNotification: onDidReceiveLocalNotification);
    var initializationSettings = InitializationSettings(
        initializationSettingsAndroid, initializationSettingsIOS);
    flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onSelectNotification: onSelectNotification);
    // jpush.setup(
    //   appKey: "替換成你自己的 appKey",
    //   channel: "theChannel",
    //   production: false,
    //   debug: false, // 設(shè)置是否打印 debug 日志
    // );
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), 
    );
  }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末匹中,一起剝皮案震驚了整個濱河市适滓,隨后出現(xiàn)的幾起案子诸尽,更是在濱河造成了極大的恐慌为障,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,104評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件便脊,死亡現(xiàn)場離奇詭異胶台,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)署咽,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評論 3 399
  • 文/潘曉璐 我一進(jìn)店門近顷,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人宁否,你說我怎么就攤上這事窒升。” “怎么了慕匠?”我有些...
    開封第一講書人閱讀 168,697評論 0 360
  • 文/不壞的土叔 我叫張陵饱须,是天一觀的道長。 經(jīng)常有香客問我台谊,道長蓉媳,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,836評論 1 298
  • 正文 為了忘掉前任锅铅,我火速辦了婚禮酪呻,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘盐须。我一直安慰自己玩荠,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,851評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著阶冈,像睡著了一般闷尿。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上女坑,一...
    開封第一講書人閱讀 52,441評論 1 310
  • 那天填具,我揣著相機(jī)與錄音,去河邊找鬼堂飞。 笑死灌旧,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的绰筛。 我是一名探鬼主播枢泰,決...
    沈念sama閱讀 40,992評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼铝噩!你這毒婦竟也來了衡蚂?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,899評論 0 276
  • 序言:老撾萬榮一對情侶失蹤骏庸,失蹤者是張志新(化名)和其女友劉穎毛甲,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體具被,經(jīng)...
    沈念sama閱讀 46,457評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡玻募,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,529評論 3 341
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了一姿。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片七咧。...
    茶點故事閱讀 40,664評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖叮叹,靈堂內(nèi)的尸體忽然破棺而出艾栋,到底是詐尸還是另有隱情,我是刑警寧澤蛉顽,帶...
    沈念sama閱讀 36,346評論 5 350
  • 正文 年R本政府宣布蝗砾,位于F島的核電站,受9級特大地震影響携冤,放射性物質(zhì)發(fā)生泄漏悼粮。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,025評論 3 334
  • 文/蒙蒙 一曾棕、第九天 我趴在偏房一處隱蔽的房頂上張望扣猫。 院中可真熱鬧,春花似錦睁蕾、人聲如沸苞笨。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽瀑凝。三九已至,卻和暖如春臭杰,著一層夾襖步出監(jiān)牢的瞬間粤咪,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評論 1 272
  • 我被黑心中介騙來泰國打工渴杆, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留寥枝,地道東北人。 一個月前我還...
    沈念sama閱讀 49,081評論 3 377
  • 正文 我出身青樓磁奖,卻偏偏與公主長得像囊拜,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子比搭,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,675評論 2 359