【发布时间】:2016-07-18 20:05:32
【问题描述】:
所以我刚刚为我的应用实现(很好地尝试)推送通知。我已经整理出了所有的证书,并且所有的东西都在 Xcode 中运行。我在开发部分将我的 .p12 文件上传到 Firebase,甚至下载了配置文件并将其重新安装到我的项目中。
这是我的AppDelegate.swift 文件中的代码
更新代码:
import UIKit
import Firebase
import FirebaseMessaging
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var storyboard: UIStoryboard?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
FIRApp.configure()
// Override point for customization after application launch.
self.storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let currentUser = FIRAuth.auth()?.currentUser
if currentUser != nil
{
self.window?.rootViewController = self.storyboard?.instantiateViewControllerWithIdentifier("tBVC")
}
else
{
self.window?.rootViewController = self.storyboard?.instantiateViewControllerWithIdentifier("loginScreen")
}
return true
}
func registerForPushNotifications(application: UIApplication) {
let notificationSettings = UIUserNotificationSettings(
forTypes: [.Badge, .Sound, .Alert], categories: nil)
application.registerUserNotificationSettings(notificationSettings)
}
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
if notificationSettings.types != .None {
application.registerForRemoteNotifications()
}
}
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
print(userInfo)
print("MessageID: \(userInfo["gcm_message_id"]!)")
//
}
}
当应用程序在后台运行时,不会显示任何通知,即使我的设备已锁定,也不会显示任何通知。弹出警报询问该应用是否有权发送通知,我说是。
我关注了这个tutorial
知道为什么我的通知没有显示吗? 在 Firebase 控制台中,它表示通知的状态为“已完成”
【问题讨论】:
-
您是否禁用了 swizzling?
-
抱歉那是什么
-
该代码自动将您应用的实例 ID 令牌与来自 Apple 的应用的 APNs 令牌相关联。默认情况下它是打开的,这不太可能是问题。
-
@ArthurThompson 我在哪里检查这个?
-
您能否实施 didRegisterForRemoteNotifications 并确认您已成功生成 APNs 令牌?在您的 info.plist 文件中关闭了 Swizzling。但默认情况下它是打开的,所以这不太可能是你的问题。确认您的 APNs 令牌会提供更多信息。
标签: ios swift firebase firebase-cloud-messaging firebase-notifications