ios - 推送通知不会出现在设备上
问题描述
我已经编写了很多代码,在其中设置通知并尝试将通知从一个用户发送给另一个用户,但现在这是我的问题,没有任何通知出现在其他用户的设备上。该应用程序不会崩溃,我也没有构建失败,所以我真的不知道我错在哪里。提前感谢您的回答,这是我的代码...
// 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)
}
})
}
解决方案
推荐阅读
- python - 在 pandas 中查询数据,其中点按 hexbin 函数分组
- html - R-markdown 中 html 的 CSS
- python - 如何以简洁的方式计算这个总和?
- python - Margin=True False 结果在数据透视表中
- laravel - Beanstalkd 队列要么失败,要么无限运行
- c# - 如果字符串不存在,如何获取消息?
- python - 在 for 循环字符串中切换单词
- python - python.exe 在 pandas.from_json 上崩溃而没有错误
- wordpress - 在直接管理员中运行放置在 public_html 文件夹中的 vue.js 页面
- javascript - 如何在函数中使用 for 循环填充两个下拉菜单?