Firebase 不显示通知

Posted

技术标签:

【中文标题】Firebase 不显示通知【英文标题】:Notifications aren't displaying with Firebase 【发布时间】: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 控制台中,它表示通知的状态为“已完成”

编辑 - 在 Xcode 中添加了我的能力图像

【问题讨论】:

您是否禁用了 swizzling? 抱歉那是什么 该代码自动将您应用的实例 ID 令牌与来自 Apple 的应用的 APNs 令牌相关联。默认情况下它是打开的,这不太可能是问题。 @ArthurThompson 我在哪里检查这个? 您能否实施 didRegisterForRemoteNotifications 并确认您已成功生成 APNs 令牌?在您的 info.plist 文件中关闭了 Swizzling。但默认情况下它是打开的,所以这不太可能是你的问题。确认您的 APNs 令牌会提供更多信息。 【参考方案1】:

我找出了它这样做的原因!正如@Collinizer 所说,Apple 和 APNS 存在问题,但现在一切正常!我在使用 OneSignal 时添加了推送通知,它们的工作就像做梦一样!

感谢所有帮助过的人:)

【讨论】:

【参考方案2】:

您需要配置 APNS 设备令牌设置,这对于使用 Firebase 云消息传递 (FCM) 推送通知至关重要。

首先让我们稍微退后一步,首先看看我们是否至少能获得令牌成功。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool 

FIRApp.configure()
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 application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) 
let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
var tokenString = ""

for i in 0..<deviceToken.length 
    tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])



FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Unknown)
print("Device Token:", tokenString)

【讨论】:

并且...确保 Bundle ID 与您在 GoogleService-Info 中设置的 ID 相同。并确保这些正确且匹配:i.stack.imgur.com/p5N2F.png。目标是首先测试令牌。 是的,证书设置正确,我的 firebase 仪表板设置正确。我的整个项目只是回到了它的一个非常旧的(第一个版本);-;意思是我刚刚丢失了所有的新作品,即使它被保存了...... 哦,请确保在测试这个 AppDelegate.swift 时,您已经注释掉了 ViewController.swift 中不必要的任何内容,即任何不是样板的内容..跨度> 感谢您的帮助,但我将使用 One Signal 代替推送通知:l

以上是关于Firebase 不显示通知的主要内容,如果未能解决你的问题,请参考以下文章

Firebase 不显示通知

Firebase消息通知在Android pie上强制终止后不显示通知

火力基地 |远程通知不显示,swift 3.0

cordova、Firebase、FCM 插件 - 在 iOS 的通知栏中不显示通知

Firebase 云消息在超出窗口焦点时不显示通知

Flutter Firebase如何控制通知在应用程序处于前台时不显示