基础组件库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mob_push_util.dart 4.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. try {
  39. MobPushNotifyMessage message = new MobPushNotifyMessage.fromJson(result);
  40. Map<String, dynamic> map = message.extrasMap;
  41. if(map.containsKey("skip_identifier")){
  42. //公共跳转
  43. RouterUtil.route(SkipModel.fromJson(map), map, context);
  44. }
  45. } catch (e) {
  46. Logger.log(e);
  47. RouterUtil.goBackHomePage(context);
  48. }
  49. print("类型1: " + result.toString());
  50. break;
  51. //点击弹窗事件
  52. case 2:
  53. try {
  54. MobPushNotifyMessage message = new MobPushNotifyMessage.fromJson(result);
  55. Map<String, dynamic> map = message.extrasMap;
  56. //公共跳转
  57. RouterUtil.route(SkipModel.fromJson(map), map, context);
  58. print("类型2: " + map.toString());
  59. } catch (e) {
  60. Logger.log(e);
  61. RouterUtil.goBackHomePage(context);
  62. }
  63. break;
  64. }
  65. }
  66. void _onErrorPush(Object event) {
  67. print('>>>>>>>>>>>>>>>>>>>>>>>>>>>onError:' + event.toString());
  68. }
  69. MobpushPlugin.addPushReceiver(_onEventPush, _onErrorPush);
  70. }
  71. //设置别名
  72. static setAlias(String alias) {
  73. MobpushPlugin.setAlias(alias).then((Map<String, dynamic> aliasMap) {
  74. Logger.log(aliasMap);
  75. String res = aliasMap['res'];
  76. String error = aliasMap['error'];
  77. String errorCode = aliasMap['errorCode'];
  78. print(">>>>>>>>>>>>>>>>>>>>>>>>>>> setAlias -> res: $res error: $error errorCode: $errorCode");
  79. });
  80. }
  81. //获取别名
  82. static getAlias() {
  83. MobpushPlugin.getAlias().then((Map<String, dynamic> aliasMap) {
  84. Logger.log(aliasMap);
  85. String res = aliasMap['res'];
  86. String error = aliasMap['error'];
  87. print(
  88. ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> getAlias -> res: $res error: $error>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
  89. });
  90. }
  91. //删除别名
  92. static deleteAlias() {
  93. MobpushPlugin.deleteAlias().then((Map<String, dynamic> aliasMap) {
  94. Logger.log(aliasMap);
  95. String res = aliasMap['res'];
  96. String error = aliasMap['error'];
  97. print(">>>>>>>>>>>>>>>>>>>>>>>>>>> deleteAlias -> res: $res error: $error");
  98. });
  99. }
  100. //设置可以推送
  101. static setCanPush() {
  102. if (Platform.isIOS) {
  103. //设置远程推送环境,向用户授权(仅 iOS)
  104. MobpushPlugin.setCustomNotification();
  105. // 开发环境 false, 线上环境 true
  106. MobpushPlugin.setAPNsForProduction(true);
  107. }
  108. //上传隐私协议许可
  109. MobpushPlugin.updatePrivacyPermissionStatus(true);
  110. }
  111. //停止推送
  112. static stopPush() {
  113. MobpushPlugin.stopPush();
  114. }
  115. //(6)重新打开推送服务
  116. static restartPush() {
  117. MobpushPlugin.restartPush();
  118. }
  119. }