从Onesignal发送推送通知可以正常工作,但FCM。
【我在做什么】
我正在按照来自Firebase的已发布文档,使用FCM实施推送通知。
【我尝试过的东西】
·检查所需的证书(使用Onesignal进行验证。从Onesignal接收推送通知可以正常工作)
・最新文件。
・证明书的密码正确。
・在开发环境中进行测试。(在临时环境中不起作用。)
・清除了所有与xcode相关的缓存。
【我想知道的事情】
如何使用FCM接收推送通知以及到目前为止我缺少的内容。
【代码在下面】※代码审查对我有很大的帮助,对我非常有用!
import UIKit
import AVFoundation
import Firebase
import FBSDKCoreKit
import FBSDKLoginKit
import FacebookCore
import FirebaseDatabase
import UserNotifications
import FirebaseMessaging
import OneSignal
import CallKit
import PushKit
import SkyWay
@UIApplicationMain AppDelegate类:UIResponder,UIApplicationDelegate,MessagingDelegate,CXProviderDelegate,PKPushRegistryDelegate {
override init() {
super.init()
FirebaseApp.configure()
print("\(UUIDs)========UUIDs=======")
print("\(UUID())========UUID=======")
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// PushKit
let registry = PKPushRegistry(queue: nil)
registry.delegate = self
registry.desiredPushTypes = [PKPushType.voIP]
// Converted to Swift 4 by Swiftify v4.1.6781 - https://objectivec2swift.com/
FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
let token = Messaging.messaging().fcmToken
print("FCM token: \(token ?? "")")
UserDefaults.standard.set(token, forKey: "FCM_TOKEN")
print("Firebase registration token: \(token)")
Messaging.messaging().delegate = self
application.registerForRemoteNotifications()
Messaging.messaging().isAutoInitEnabled = true
let onesignalInitSettings = [kOSSettingsKeyAutoPrompt: false]
OneSignal.initWithLaunchOptions(launchOptions,
appId: "2e92****************a7f",
handleNotificationAction: nil,
settings: onesignalInitSettings)
OneSignal.inFocusDisplayType = OSNotificationDisplayType.notification;
OneSignal.promptForPushNotifications(userResponse: { accepted in
print("User accepted notifications: \(accepted)")
})
return true
}
// Handle updated push credentials
func pushRegistry(registry: PKPushRegistry!, didUpdatePushCredentials credentials: PKPushCredentials!, forType type: String!) {
// Register VoIP push token (a property of PKPushCredentials) with server
}
func application(application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
print("sdfksjkjfksajkdnslfs")
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
UserDefaults.standard.set(fcmToken, forKey: "FCM_TOKEN")
print("Firebase registration token: \(fcmToken)")
// TODO: If necessary send token to application server.
// Note: This callback is fired at each app startup and whenever a new token is generated.
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("Received data message: \(remoteMessage.appData)")
}
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return SDKApplicationDelegate.shared.application(application,
open: url,
sourceApplication: sourceApplication,
annotation: annotation)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
// Print message ID.
if let messageID = userInfo["gcm.message_id"] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// Print message ID.
if let messageID = userInfo["gcm.message_id"] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
}
@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
if let messageID = userInfo["gcm.message_id"] {
print("Message ID: \(messageID)")
completionHandler([.alert, .sound])
}
print(userInfo)
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
if let messageID = userInfo["gcm.message_id"] {
print("Message ID: \(messageID)")
}
print(userInfo)
}
}