admin
2022-01-20 d8ef9a783b9e0b2a495f02fdf3daaf27ef49e99d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import 'dart:convert';
import 'dart:io';
import 'dart:ui';
 
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:fluwx_no_pay/fluwx_no_pay.dart';
import 'package:flutter/services.dart';
import 'package:jpush_flutter/jpush_flutter.dart';
import '../api/http.dart';
import '../model/user/user_info.dart';
import '../utils/event_bus_util.dart';
import '../utils/pageutils.dart';
import '../utils/user_util.dart';
 
import 'global.dart';
 
class PushUtil {
  static final JPush _jpush = JPush();
 
  static init(BuildContext context) async {
    try {
      _jpush.addEventHandler(
          onReceiveNotification: (Map<String, dynamic> message) async {
        print("flutter onReceiveNotification: $message");
      }, onOpenNotification: (Map<String, dynamic> message) async {
        var extra = message["extras"]["cn.jpush.android.EXTRA"];
        extra = jsonDecode(extra);
        // //打开页面
        // var type = extra["type"];
        //
        // if (type == "sos") {
        //   //SOS
        //   NavigatorUtil.navigateToNextPagePush(SOSPage(title: ""));
        // } else if (type == "locationInvite") {
        //   //请求定位
        //   eventBus.fire(LocationInviteEventBus());
        // }
      }, onReceiveMessage: (Map<String, dynamic> message) async {
        var data = jsonDecode(message["message"]);
        print("flutter onReceiveMessage: $data");
        var type = data["type"];
        // if (type == "sos") {
        //   //SOS
        //   NavigatorUtil.navigateToNextPagePush(SOSPage(title: ""));
        // } else if (type == "locationInvite") {
        //     eventBus.fire(LocationInviteEventBus());
        // }
      }, onReceiveNotificationAuthorization:
              (Map<String, dynamic> message) async {
        print("flutter onReceiveNotificationAuthorization: $message");
      });
    } on PlatformException {}
 
    _jpush.setup(
      appKey: "987d3d50f209994fa522d001", //你自己应用的 AppKey
      channel: "developer-default",
      production: false,
      debug: true,
    );
    _jpush.applyPushAuthority(
        NotificationSettingsIOS(sound: true, alert: true, badge: true));
 
    // Platform messages may fail, so we use a try/catch PlatformException.
    _jpush.getRegistrationID().then((rid) {
      print("flutter get registration id : $rid");
      UserApiUtil.uploadPushRegId(context, rid);
    });
 
    //如果登录了就设置用户的uid
    bool isLogin = await UserUtil.isLogin();
    if (isLogin) {
      UserInfo? user = await UserUtil.getUserInfo();
      setAlias(user!.id!.toString());
    }
  }
 
  ///添加alias
  static Future setAlias(String alias) async {
    try {
      await _jpush.setAlias(alias);
    } catch (e) {}
  }
 
  //删除alias
  static Future removeAlias() async {
    await _jpush.deleteAlias();
  }
}