gpt4 book ai didi

python - 无法从 iOS 上的服务器获取 FCM 推送通知(适用于 Android)

转载 作者:行者123 更新时间:2023-11-29 05:11:26 27 4
gpt4 key购买 nike

我是 iOS/Swift 新手,无法使用推送通知。我已将服务器后端配置为在发生某些操作时将通知推送到我的应用程序。我已经通过 FCM 配置了数据通知,因为我需要通知中的一些自定义数据来打开一个事件/ View 或另一个。这是用于发送通知的代码(python/django):

registration_id = profiletarget.device_token
message_title = "Ha llegado tu turno"
message_body = "Entra y escribe en el relato para el que estás en cola"
data_message = {
"title" : "¿Listo para escribir?",
"body" : "Ha llegado tu turno para escribir en el relato. Recuerda que tienes un minuto para aceptar tu turno y 3 para escribir.",
"bookid" : booktarget.pk,
"multimediaurl" : multimediaused.url
}
result = push_service.notify_single_device(registration_id=registration_id, data_message=data_message)

这段代码中的所有内容都正常工作,因为我在 Android 上正确地获取了它们。但在 iOS 上...我无法让它工作。

我已经获取了通知 token ,我已将其发布到我的服务器,我已使用 FCM 控制台上的通知发送器发送测试推送通知(iPhone 获取它们),但不是来 self 的自定义方法的通知,它没有不显示任何东西。服务器方法有什么问题还是我遗漏了什么?

这是快速代码:

import UIKit
import KeychainSwift
import Firebase
import FirebaseMessaging

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {

var window : UIWindow?;
var storyboard : UIStoryboard?;
var token = ""
let gcmMessageIDKey = "gcm.message_id"


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
self.storyboard = UIStoryboard(name: "Main", bundle: Bundle.main);
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "PT Sans", size: 12)!], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "PT sans", size: 12)!], for: .selected)
let keychain = KeychainSwift()
token = keychain.get("token") ?? ""
if (token != ""){
print("log-token: ", token)
print("log-redirection: ", "no redirection needed!")
FirebaseApp.configure()
Messaging.messaging().delegate = self
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 })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}

application.registerForRemoteNotifications()

} else {
print("log-token: ", "noToken")
print("log-redirection: ", "redirection to LoginController")
window?.rootViewController = self.storyboard?.instantiateViewController(withIdentifier: "loginView");
}
return true
}

func application(
_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
) {
let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
let token = tokenParts.joined()
print("Device Token: \(token)")
postNotificationToken(token: token)
}

func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")

let dataDict:[String: String] = ["token": fcmToken]
NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
// TODO: If necessary send token to application server.
// Note: This callback is fired at each app startup and whenever a new token is generated.
}

func postNotificationToken(token:String) {
var request = URLRequest(url: URL(string: "https://myurl?myparam="+token)!)
request.httpMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("Bearer "+self.token, forHTTPHeaderField: "myauth")

let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: { data, response, error -> Void in
let httpURLResponse = response as? HTTPURLResponse;
if (httpURLResponse?.statusCode == 200){
let string = String.init(data: data!, encoding: String.Encoding.utf8)
print(string)
} else {
print(httpURLResponse?.statusCode)
}
})

task.resume()
}



func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("Received data message: \(remoteMessage.appData)")
}

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler([.alert, .badge, .sound])
}


// MARK: UISceneSession Lifecycle


}

这是我的目标能力:

Project target capabilities

这是我的开发者帐户中 APN 通知的 key :

APN Key

使用 FCM 控制台,通知就可以了:

FCM Console

感谢您阅读这篇长文。任何事情都会有所帮助!

最佳答案

好的,我已经将一些答案和其他帖子中找到的一些代码混合在一起了。首先,我使用的是 APN key ,而不是 APN 证书。

其次,我正在检查用户必须发送通知的操作系统(iOS/Android),以便我可以配置不同的通知结构。这是 python/django 中的通知系统,使用 PyFCM 库并将 iOS 通知作为警报发送,正如我在 post 上找到的那样。 :

if devicetype == "and":
registration_id = profiletarget.device_token
message_title = "default-title"
message_body = "default-body"
data_message = {
"title" : "title",
"body" : "body",
"bookid" : booktarget.pk,
"multimediaurl" : multimediaused.url
}
result = push_service.notify_single_device(registration_id=registration_id, data_message=data_message)
else:
registration_id = profiletarget.device_token
message_title = "title"
message_body = "body"
data_message = {
"bookid" : booktarget.pk,
"multimediaurl" : multimediaused.url,
}
result = push_service.notify_single_device(registration_id=registration_id,
message_title=message_title,
message_body=message_body,
data_message=data_message,
extra_kwargs={"apns_push_type": "alert"}
)

在 Xcode 中,我只需提供推送通知和后台模式 - 远程通知的广告功能,如问题图片中所示。

在代码中,我错过了与此方法对应的 Firebase 教程的某些部分:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification

// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)

// Print message ID.
if let messageID = userInfo["body"] {
print("Notif metod 1")
// gotoWritting(bookid: messageID)
}

// Print full message.
print("Notif metod 1")
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification

// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)

// Print message ID.
if let messageID = userInfo["body"]{
print("Notif metod 2")
// gotoWritting(bookid: messageID)
}

// Print full message.
print(userInfo["bookid"]!)
gotoWritting(bookid: Int(userInfo["bookid"]! as! String) ?? 90)
completionHandler(UIBackgroundFetchResult.newData)
}

第二个是由通知点击触发的(后台、前台和应用程序关闭...),一旦点击,我可以使用一些通知参数重定向用户。

我无法在以前的版本上进行测试,但希望它可以在 iOS 9 上运行(如果您知道这是否有效,请告诉我)。

感谢所有提供帮助的人!

关于python - 无法从 iOS 上的服务器获取 FCM 推送通知(适用于 Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59642602/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com