顾海波

Merge remote-tracking branch 'origin/master'

1 import Flutter 1 import Flutter
2 import UIKit 2 import UIKit
  3 +import UserNotifications
3 4
  5 +@objc(FlutterPushPlugin)
4 public class FlutterPushPlugin: NSObject, FlutterPlugin { 6 public class FlutterPushPlugin: NSObject, FlutterPlugin {
5 - public static func register(with registrar: FlutterPluginRegistrar) {  
6 - let channel = FlutterMethodChannel(name: "flutter_push", binaryMessenger: registrar.messenger())  
7 - let instance = FlutterPushPlugin()  
8 - registrar.addMethodCallDelegate(instance, channel: channel)  
9 - } 7 + public static func register(with registrar: FlutterPluginRegistrar) {
  8 + let channel = FlutterMethodChannel(name: "ewin:flutter_push", binaryMessenger: registrar.messenger())
  9 + let instance = FlutterPushPlugin()
  10 + registrar.addMethodCallDelegate(instance, channel: channel)
  11 + }
10 12
11 - public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {  
12 - switch call.method {  
13 - case "getPlatformVersion":  
14 - result("iOS " + UIDevice.current.systemVersion)  
15 - default:  
16 - result(FlutterMethodNotImplemented) 13 + @objc public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
  14 + switch call.method {
  15 + case "getPlatformVersion":
  16 + result("iOS " + UIDevice.current.systemVersion)
  17 + case "init":
  18 + print("init初始化")
  19 + // 初始化
  20 + UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, _ in
  21 + if granted {
  22 + print("有通知权限")
  23 + DispatchQueue.main.async {
  24 + UIApplication.shared.registerForRemoteNotifications()
  25 + }
  26 + } else {
  27 + print("无用通知权限")
  28 + }
  29 + }
  30 + result(true)
  31 + case "checkPermission":
  32 + print("checkPermission检测权限")
  33 + UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, _ in
  34 + if granted {
  35 + print("有通知权限")
  36 + } else {
  37 + print("无用通知权限")
  38 + }
  39 + }
  40 + result(true)
  41 + case "getPermission":
  42 + print("getPermission跳转设置获取权限")
  43 + if #available(iOS 15.0, *) {
  44 + // iOS 15 及以上版本,直接跳转到通知设置页面
  45 + if let url = URL(string: "App-Prefs:NOTIFICATIONS_ID") {
  46 + UIApplication.shared.open(url, options: [:], completionHandler: nil)
  47 + }
  48 + } else {
  49 + // iOS 15 以下版本,跳转到应用的设置页面
  50 + if let url = URL(string: UIApplication.openSettingsURLString) {
  51 + UIApplication.shared.open(url, options: [:], completionHandler: nil)
  52 + }
  53 + }
  54 + result(true)
  55 + default:
  56 + result(FlutterMethodNotImplemented)
  57 + }
17 } 58 }
18 - }  
19 } 59 }