首页 > 解决方案 > 推送通知不会出现在设备上

问题描述

我已经编写了很多代码,在其中设置通知并尝试将通知从一个用户发送给另一个用户,但现在这是我的问题,没有任何通知出现在其他用户的设备上。该应用程序不会崩溃,我也没有构建失败,所以我真的不知道我错在哪里。提前感谢您的回答,这是我的代码...

// AppDelegate.swift 中关于推送通知的部分

static let NOTIFICATION_URL = "https://gcm-http.googleapis.com/gcm/send"
static var DEVICEID = String()
static let SERVERKEY = "AAAAuIcRiYI:APA91bHA8Q4IJBG9dG4RY9YOZ3v4MlcVZjmYs3XhiMY3xpQm3bTSjrINUiImaE0t17Y6mghR2vN1ezJTMSVFmHlgOUBX8KQZEckgwCkc1tlMdpm_UjxobmrHf3GbvwrKtHVZsJR-v1GG"

#available(iOS 10.0, *){
        UNUserNotificationCenter.current().delegate = self
        // Request Autorization.
        let option : UNAuthorizationOptions = [.alert,.badge,.sound]
        UNUserNotificationCenter.current().requestAuthorization(options: option) { (bool, err) in
        }
    }
    else{
        let settings : UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert,.badge,.sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }
    application.registerForRemoteNotifications()
    UIApplication.shared.applicationIconBadgeNumber = 0

func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
    guard  let newToken = InstanceID.instanceID().token() else {return}
    AppDelegate.DEVICEID = newToken
    connectToFCM()
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

    let notification =  response.notification.request.content.body
    print(notification)
    completionHandler()
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    guard let token = InstanceID.instanceID().token() else {return}

    AppDelegate.DEVICEID = token
    print(token)
    connectToFCM()
}

func connectToFCM()
{
    Messaging.messaging().shouldEstablishDirectChannel = true
}

//messages.swift中关于通知的部分,我在用户发送消息后调用该函数,接收消息的人同时收到通知。

fileprivate func setupPushNotification(fromDevice:String)
{

    Database.database().reference().child("users").child(userID).observeSingleEvent(of: .value, with: { (snapshot) in

        let value = snapshot.value as? NSDictionary
        let device = value?["device"] as? String ?? ""

    guard let message = self.authorText else {return}
    let title = "You received a new message !"
    let body = "from \(message)"
    let toDeviceID = device
    var headers:HTTPHeaders = HTTPHeaders()

    headers = ["Content-Type":"application/json","Authorization":"key=\(AppDelegate.SERVERKEY)"

    ]
    let notification = ["to":"\(toDeviceID)","notification":["body":body,"title":title,"badge":1,"sound":"default"]] as [String:Any]

    Alamofire.request(AppDelegate.NOTIFICATION_URL as URLConvertible, method: .post as HTTPMethod, parameters: notification, encoding: JSONEncoding.default, headers: headers).responseJSON { (response) in
        print(response)
    }
    })

}

标签: iosswiftfirebaseapple-push-notifications

解决方案


推荐阅读