FCM:已发送但未收到的消息



试图自己解决这个问题。 花了大约一个小时左右仍然没有结果。

给我找了以前关于FCM的项目的旧代码。 但是代码曾经并且仍在其应用程序上运行。 尽管我设法将代码转移到我的新项目中。 但它在这里不起作用。

现在我知道 APN 既奇怪又复杂。 但这对我来说更像是一个记忆的情况。

我做过的事情: - 将我的个人.p12上传到我的火力基地项目 - 在应用程序功能中启用了"推送通知" - 在appdelegate上导入并使用了用户通知框架.swift

以下是我的应用程序代表的外观:

import UIKit
import Firebase
import FirebaseFirestore
import StoreKit
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {
var window: UIWindow?

override init() {
super.init()
FirebaseApp.configure()
// not really needed unless you really need it FIRDatabase.database().persistenceEnabled = true
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
Auth.auth().signInAnonymously() { (authResult, error) in
// ...
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()
}


Messaging.messaging().delegate = self
UIApplication.shared.statusBarStyle = .default

let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "gateway") as! gatewayViewController
window!.rootViewController = viewController
return true
}
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 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[gcmMessageIDKey] {
//      print("Message ID: (messageID)")
//  }
// Print full message.
print(userInfo)
}
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[gcmMessageIDKey] {
//    print("Message ID: (messageID)")
//}//
// Print full message.
print(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}

好的,使用此代码,您可以获得设备的Firebase注册令牌,我复制了我的代码并在Cloud Functions上使用它来发送测试消息,这是我的CF的样子:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.helloWorld = functions.https.onRequest((request, response) => {
var registrationToken = 'f8fWx_sANVM:APA91bEd46drxiBvHLZd5YKVClQr91oubzJKOyXE1LNgxOsi3ihUw31yEJL6prHKm-A83B1N1sr2GOff3P9tUsRNhCpG7_VMRlDUDfthIcwkDUgzKPV5NZtlo6pcpxsvD9ZgYlPqibNp';
var payload = {
notification: {
title: "just published new Word",
body: "Hii",
}
};

// registration token.
admin.messaging().sendToDevice(registrationToken, payload)
.then(function(response) {
// See the MessagingDevicesResponse reference documentation for
// the contents of response.
return console.log("Successfully sent message:", response);
})
.catch(function(error) {
console.log("Error sending message:", error);
}); 

});

好的,到目前为止,在点击helloWorldurl后,我的控制台得到这个:

已成功发送消息: { 结果: [ { 消息 ID: '0:1537714204565821%b3b8835bb3b8835b' } ], canonicalRegistrationTokenCount: 0, 失败计数:0, 成功计数: 1, 多播 ID: 8154206809408282000 }

函数执行耗时 60002 毫秒,完成状态:"超时">

上次在我以前的项目中,最好花了 20 毫秒。我仍然想不通。非常感谢您的帮助

您使用的是 HTTP(S( 触发的云函数,这意味着您的代码必须发送响应。由于您的代码不这样做,因此该函数运行 60 秒,然后被云函数环境终止。这意味着您支付的时间比实际需要的时间多,因此您需要修复它。

例如:

// registration token.
admin.messaging().sendToDevice(registrationToken, payload)
.then(function(response) {
// See the MessagingDevicesResponse reference documentation for
// the contents of response.
//return console.log("Successfully sent message:", response);
res.status(200).send(response);
})
.catch(function(error) {
console.log("Error sending message:", error);
}); 

这将删除云函数日志中的消息,并确保您只为实际需要的时间付费。

相关内容

  • 没有找到相关文章

最新更新