问题
Here is my situation: I generated a production push notification certificate. On Apple Developer Portal the certificate appears as:
I have an Apple Dev Certificate and an Apple Distribution Certificate:
I create an archive and distribute my app with Ad Hoc using Xcode 11.3.1. Here is the Ad Hoc summary:
I have enabled push notifications but they don't work. I can't receive notifications when app is in the background. When I used an iPhone Distribution certificate in Xcode 10.3 notifications worked with the same code. What am I missing? Thx!!
回答1:
Here is the process of integrating push notification, follow it step by step:
First login to your apple developer account -> Identifiers -> click your app identifier and enable push notification. After enabling notification it will ask you for CertificateSigningRequest.certSigningRequest, You need to open Keychain access and open click Certificate Assistant -> Request Certificate From Certificate Authority
After creating that certificate you need to add that certificates in apple account. And download development.cer and aps.cer certificate from there.
After download these certificate just click on both certificate and this will open your keychain Access. Now left click on those certificate and export .p12 certificate that will ask you to generate password for that certificate.
Now open your firebase account and go to settings -> cloud messaging -> add your production.p12 and development.p12 certificate there.
Now back to xcode Go to app target -> Signin and capabilities -> add push notifications and add background modes, check background fetch and Remote notification.
Come to the code now add pods and install
pod 'Firebase/Analytics'
pod 'Firebase/Messaging'
Open your AppDelegate import these
import FirebaseAnalytics
import Firebase
import FirebaseMessaging
Add the delegate MessagingDelegate
Initiate let gcmMessageIDKey = "gcm.message_id"
Add these in didFinishLaunchingWithOptions
method
FirebaseApp.configure()
Messaging.messaging().delegate = self
//Added push notification
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)
}
application.registerForRemoteNotifications()
Messaging.messaging().isAutoInitEnabled = true
After adding add these methods in app delegate and you are ready to receive push notifications:
//Push Notifications
func application(application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
Messaging.messaging().apnsToken = deviceToken as Data
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
InstanceID.instanceID().instanceID { (result, error) in
if let error = error {
print("Error fetching remote instance ID: \(error)")
} else if let result = result {
print("Remote instance ID token: \(result.token)")
// self.instanceIDTokenMessage.text = "Remote InstanceID token: \(result.token)"
}
}
print(userInfo)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
print(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
completionHandler([])
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
print(userInfo)
completionHandler()
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
let dataDict:[String: String] = ["token": fcmToken]
NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
UserDefaults.standard.set(fcmToken, forKey: "FCMToken")
UserDefaults.standard.synchronize()
}
func messaging(_ messaging: Messaging, did remoteMessage: MessagingRemoteMessage) {
print("Received data message: \(remoteMessage.appData)")
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("Received data message: \(remoteMessage.appData)")
}
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken as Data
}
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
let dataDict:[String: String] = ["token": fcmToken]
}
来源:https://stackoverflow.com/questions/60157101/push-notifications-dont-work-xcode-11-3-1