【发布时间】:2019-09-17 08:03:57
【问题描述】:
我已经实现了有关实现推送通知的所有步骤,但是当我测试通知时,控制台它不会出现在 iPhone 上,但是当我使用来自“https://pushtry.com/”的密码 n 令牌的生产 apns 证书 n 进行测试时,它工作正常。我无法纠正为什么会这样。我还启用了功能部分的推送通知。谢谢 下面是我的应用委托代码
mport Firebase
import FirebaseMessaging
import FirebaseInstanceID
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder,UIApplicationDelegate,MessagingDelegate,UNUserNotificationCenterDelegate{
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
GMSServices.provideAPIKey("AIzaSyB5HWsalLBmBFjLC3NjTUF2KB7i5zvz45k")
GMSPlacesClient.provideAPIKey("AIzaSyB5HWsalLBmBFjLC3NjTUF2KB7i5zvz45k")
let notificationCenter = UNUserNotificationCenter.current()
// It is For Notification Allow Authentications
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (isGranted, err) in
guard isGranted else { return }
self.getNotificationSettings()
if err != nil {
//Something bad happend
print("Erroo ocured......")
} else {
print(" Success.........All good")
UNUserNotificationCenter.current().delegate = self
Messaging.messaging().delegate = self // it is very important to Generate Firebase registration token, otherwise "didReceiveRegistrationToken" method will not called.
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}
FirebaseApp.configure()
return true
}
func getNotificationSettings() {
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
print("Notification settings: \(settings)")
guard settings.authorizationStatus == .authorized else { return }
DispatchQueue.main.async(execute: { UIApplication.shared.registerForRemoteNotifications() })
}
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String)
{
print("Firebase registration token::::::: \(fcmToken)")
UserDefaults.standard.set(fcmToken, forKey: "deviceToken")
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("Received data message: \(remoteMessage.appData)")
}
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
NSLog("[RemoteNotification] didRefreshRegistrationToken: \(fcmToken)")
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
// Print the error to console (you should alert the user that registration failed)
print("APNs registration failed: \(error)")
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
//NSLog("[RemoteNotification] applicationState: \(applicationStateString) didReceiveRemoteNotification for iOS9: \(userInfo)")
if UIApplication.shared.applicationState == .active {
//TODO: Handle foreground notification
} else {
//TODO: Handle background notification
}
// Print full message.
print(userInfo)
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// show the notification alert (banner), and with sound
completionHandler([.alert, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
// tell the app that we have finished processing the user’s action / response
let application = UIApplication.shared
if(application.applicationState == .active){
print("user tapped the notification bar when the app is in foreground")
}
if(application.applicationState == .inactive)
{
print("user tapped the notification bar when the app is in background")
}
completionHandler()
}
func applicationWillResignActive(_ application: UIApplication) {
}
func applicationDidEnterBackground(_ application: UIApplication) {
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationWillTerminate(_ application: UIApplication) {
self.saveContext()
}
}
【问题讨论】:
标签: ios swift firebase firebase-cloud-messaging