生成证书就不多说了 按旧的教程来 : https://www.jianshu.com/p/cc952ea07a08?mType=Group
集成代码:
```
import UIKit
import Flutter
import UserNotifications
@UIApplicationMain
class AppDelegate: FlutterAppDelegate{
override func application(
_application:UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey:Any]?
) ->Bool{
registerAppNotificationSettings(launchOptions: launchOptions as [NSObject:AnyObject]?)
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
override func application(_application:UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken:Data) {
let deviceTokenString = deviceToken.reduce("",{$0+String(format:"%02x",$1)})
NSLog("deviceTokenString : %@", deviceTokenString);
}
override func application(_application:UIApplication, didFailToRegisterForRemoteNotificationsWithError error:Error){
NSLog("deviceTokenString error %@", error.localizedDescription);
}
private func registerAppNotificationSettings(launchOptions: [NSObject: AnyObject]?) {
if#available(iOS10.0, *) {
letnotifiCenter =UNUserNotificationCenter.current()
notifiCenter.delegate=self
lettypes =UNAuthorizationOptions(arrayLiteral: [.alert, .badge, .sound])
notifiCenter.requestAuthorization(options: types) { (flag, error)in
ifflag {
NSLog("iOS request notification success")
}else{
NSLog(" iOS 10 request notification fail")
}
}
}else { //iOS8,iOS9注册通知
let setting =UIUserNotificationSettings(types: [.alert, .badge, .sound], categories:nil)
UIApplication.shared.registerUserNotificationSettings(setting)
}
UIApplication.shared.registerForRemoteNotifications()
}
//iOS10新增:处理前台收到通知的代理方法
@available(iOS 10.0, *)
funcuserNotificationCenter(center:UNUserNotificationCenter, willPresentNotification notification:UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) ->Void){
letuserInfo = notification.request.content.userInfo
print("userInfo10:\(userInfo)")
completionHandler([.sound,.alert])
}
//iOS10新增:处理后台点击通知的代理方法
@available(iOS 10.0, *)
funcuserNotificationCenter(center:UNUserNotificationCenter, didReceiveNotificationResponse response:UNNotificationResponse, withCompletionHandler completionHandler: () ->Void){
let userInfo = response.notification.request.content.userInfo
print("userInfo10:\(userInfo)")
completionHandler()
}
funcapplication(application:UIApplication, didReceiveRemoteNotification userInfo: [NSObject:AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) ->Void) {
print("收到新消息Active\(userInfo)")
ifapplication.applicationState==UIApplicationState.active{
// 代表从前台接受消息app
}else{
// 代表从后台接受消息后进入app
UIApplication.shared.applicationIconBadgeNumber = 0
}
completionHandler(.newData)
}
}
```
xcode中的项目配置:
backgroundModes 开启远程推送
capability 添加推送服务
特别注意 要选中all 或者当前编译的模式(debug release profile) 去添加capability , 否则会报错:
<未找到应用程序的“aps-environment”的授权字符串>
在编译后会生成以下文件
然后就可以拿到token 测试推送了
网友评论