ios - 如何通过 FireBase 更新使用推送通知

标签 ios swift xcode firebase

我目前正在尝试设置推送通知,尽管 xcode 认为代码中没有问题,但如果有人可以看到任何问题或对其他可能出现问题的地方提出建议,那么该功能将无法正常工作,我们将不胜感激。

 //
//  AppDelegate.swift
//  PushMyNotifs
//
//  Created by Jack Wallace on 24/6/17.
//  Copyright © 2017 JackWallace. All rights reserved.
//

import UIKit
import Firebase
import FirebaseInstanceID
import FirebaseMessaging
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, 
UNUserNotificationCenterDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {


    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 })
        // For iOS 10 data message (sent via FCM
        UNUserNotificationCenter.current().delegate = self
    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }

    application.registerForRemoteNotifications()


    FirebaseApp.configure()



    NotificationCenter.default.addObserver(self, selector: #selector(self.tokenRefreshNotification(notification:)), name: NSNotification.Name.InstanceIDTokenRefresh, object: nil)

    return true

}

func applicationDidEnterBackground(_ application: UIApplication) {

    Messaging.messaging().shouldEstablishDirectChannel = false

}

func tokenRefreshNotification(notification: NSNotification){

    let refreshedToken = InstanceID.instanceID().token()!

    print("InstanceID token: \(refreshedToken)")

    connectToFCM()

}



func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {

    print("Firebase registration token: \(fcmToken)")

}



func connectToFCM(){

    Messaging.messaging().shouldEstablishDirectChannel = false

    }

 }

最佳答案

您设置了证书吗?

这是一个很好的例子: push-notifications-in-firebase

关于ios - 如何通过 FireBase 更新使用推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44735380/

相关文章:

ios - 为什么返回 View 时获取的数据不消失?

inheritance - Swift 语言中的抽象函数

arrays - Swift 通过在每个索引处获取 max() 从两个 int 数组创建一个 Int 数组

ios - 如何知道 CollectionView 中 Header 的特定部分是否滚出屏幕边界?

ios - 如何在 Xcode 堆栈跟踪中查找 NSInternalInconsistencyException 的来源

ios - 使用 NSPredicate 获取 ID 的最大计数

ios - AVAsset(url : url). isPlayable 减慢 Collection View

ios - 使 UIImage 在@IB_Designable 上的新位置重绘

ios - Swift 此类对于键错误不符合键值编码

ios - 发现意外的 Mach-O header 代码 : 0x72613c21 on submitting to App Store