FlutterPushPlugin.swift
2.42 KB
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
import Flutter
import UIKit
import UserNotifications
@objc(FlutterPushPlugin)
public class FlutterPushPlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "ewin:flutter_push", binaryMessenger: registrar.messenger())
let instance = FlutterPushPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
}
@objc public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
case "getPlatformVersion":
result("iOS " + UIDevice.current.systemVersion)
case "init":
print("init初始化")
// 初始化
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, _ in
if granted {
print("有通知权限")
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
result(true)
} else {
print("无用通知权限")
result(false)
}
}
case "checkPermission":
print("checkPermission检测权限")
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, _ in
if granted {
print("有通知权限")
result(true)
} else {
print("无用通知权限")
result(false)
}
}
case "getPermission":
print("getPermission跳转设置获取权限")
if #available(iOS 15.0, *) {
// iOS 15 及以上版本,直接跳转到通知设置页面
if let url = URL(string: "App-Prefs:NOTIFICATIONS_ID") {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
} else {
// iOS 15 以下版本,跳转到应用的设置页面
if let url = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
result(true)
default:
result(FlutterMethodNotImplemented)
}
}
}