zhuguoming

【需求】ios推送初始化

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 + DispatchQueue.main.async {
  21 + UIApplication.shared.registerForRemoteNotifications()
  22 + }
  23 + result(true)
  24 + case "checkPermission":
  25 + print("checkPermission检测权限")
  26 + UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, _ in
  27 + if granted {
  28 + print("有通知权限")
  29 + } else {
  30 + print("无用通知权限")
  31 + }
  32 + }
  33 + result(true)
  34 + case "getPermission":
  35 + print("getPermission跳转设置获取权限")
  36 + if #available(iOS 15.0, *) {
  37 + // iOS 15 及以上版本,直接跳转到通知设置页面
  38 + if let url = URL(string: "App-Prefs:NOTIFICATIONS_ID") {
  39 + UIApplication.shared.open(url, options: [:], completionHandler: nil)
  40 + }
  41 + } else {
  42 + // iOS 15 以下版本,跳转到应用的设置页面
  43 + if let url = URL(string: UIApplication.openSettingsURLString) {
  44 + UIApplication.shared.open(url, options: [:], completionHandler: nil)
  45 + }
  46 + }
  47 + result(true)
  48 + default:
  49 + result(FlutterMethodNotImplemented)
  50 + }
17 } 51 }
18 - }  
19 } 52 }