基础组件库
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 

122 rader
3.8 KiB

  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'package:flutter/material.dart';
  4. import 'package:mobpush_plugin/mobpush_custom_message.dart';
  5. import 'package:mobpush_plugin/mobpush_notify_message.dart';
  6. import 'package:mobpush_plugin/mobpush_plugin.dart';
  7. import 'package:zhiying_base_widget/utils/contants.dart';
  8. import 'package:zhiying_comm/zhiying_comm.dart';
  9. class MobPushUtil {
  10. //接受推送监听
  11. static addPushReceiver() {
  12. void _onEventPush(Object event) {
  13. print('>>>>>>>>>>>>>>>>>>>>>>>>>>>我的onEvent:' + event.toString());
  14. Logger.log(event.toString());
  15. BuildContext context = navigatorKey.currentState.overlay.context;
  16. Map<String, dynamic> eventMap = json.decode(event);
  17. Map<String, dynamic> result = eventMap['result'];
  18. int action = eventMap['action'];
  19. switch (action) {
  20. case 0:
  21. MobPushCustomMessage message = new MobPushCustomMessage.fromJson(result);
  22. showDialog(
  23. context: context,
  24. child: AlertDialog(
  25. content: Text(message.content),
  26. actions: <Widget>[
  27. FlatButton(
  28. onPressed: () {
  29. Navigator.pop(context);
  30. },
  31. child: Text("确定"),
  32. )
  33. ],
  34. ));
  35. break;
  36. //接受到推送弹出通知事件
  37. case 1:
  38. MobPushNotifyMessage message = new MobPushNotifyMessage.fromJson(result);
  39. print("类型1: " + result.toString());
  40. break;
  41. //点击弹窗事件
  42. case 2:
  43. try {
  44. MobPushNotifyMessage message = new MobPushNotifyMessage.fromJson(result);
  45. Map<String, dynamic> map = message.extrasMap;
  46. //公共跳转
  47. RouterUtil.route(SkipModel.fromJson(map), map, context);
  48. print("类型2: " + map.toString());
  49. } catch (e) {
  50. Logger.log(e);
  51. RouterUtil.goBackHomePage(context);
  52. }
  53. break;
  54. }
  55. }
  56. void _onErrorPush(Object event) {
  57. print('>>>>>>>>>>>>>>>>>>>>>>>>>>>onError:' + event.toString());
  58. }
  59. MobpushPlugin.addPushReceiver(_onEventPush, _onErrorPush);
  60. }
  61. //设置别名
  62. static setAlias(String alias) {
  63. MobpushPlugin.setAlias(alias).then((Map<String, dynamic> aliasMap) {
  64. Logger.log(aliasMap);
  65. String res = aliasMap['res'];
  66. String error = aliasMap['error'];
  67. String errorCode = aliasMap['errorCode'];
  68. print(">>>>>>>>>>>>>>>>>>>>>>>>>>> setAlias -> res: $res error: $error errorCode: $errorCode");
  69. });
  70. }
  71. //获取别名
  72. static getAlias() {
  73. MobpushPlugin.getAlias().then((Map<String, dynamic> aliasMap) {
  74. Logger.log(aliasMap);
  75. String res = aliasMap['res'];
  76. String error = aliasMap['error'];
  77. print(
  78. ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> getAlias -> res: $res error: $error>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
  79. });
  80. }
  81. //删除别名
  82. static deleteAlias() {
  83. MobpushPlugin.deleteAlias().then((Map<String, dynamic> aliasMap) {
  84. Logger.log(aliasMap);
  85. String res = aliasMap['res'];
  86. String error = aliasMap['error'];
  87. print(">>>>>>>>>>>>>>>>>>>>>>>>>>> deleteAlias -> res: $res error: $error");
  88. });
  89. }
  90. //设置可以推送
  91. static setCanPush() {
  92. if (Platform.isIOS) {
  93. //设置远程推送环境,向用户授权(仅 iOS)
  94. MobpushPlugin.setCustomNotification();
  95. // 开发环境 false, 线上环境 true
  96. MobpushPlugin.setAPNsForProduction(true);
  97. }
  98. //上传隐私协议许可
  99. MobpushPlugin.updatePrivacyPermissionStatus(true);
  100. }
  101. //停止推送
  102. static stopPush() {
  103. MobpushPlugin.stopPush();
  104. }
  105. //(6)重新打开推送服务
  106. static restartPush() {
  107. MobpushPlugin.restartPush();
  108. }
  109. }