FCM:消息已发送但未收到

时间:2018-09-23 15:03:46

标签: swift firebase firebase-cloud-messaging google-cloud-functions

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

给我有关FCM的先前项目的旧代码。但是该代码仍在并且仍在其应用程序上运行。尽管我设法将代码转移到我的新项目中。但它在这里行不通。

现在,我知道APN很奇怪而且很复杂。但这对我来说更是一种记忆。

我所做的事情: -将我的个人.p12上传到我的Firebase项目 -在应用程序功能中启用了“推送通知” -在appdelegate.swift上导入并使用了UserNotifications框架

这是我的 AppDelegate 的样子:

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);
  }); 



 });

到目前为止,点击 helloWorld 网址后,我的控制台已经收到:

  

成功发送了消息:{结果:[{messageId:'0:1537714204565821%b3b8835bb3b8835b'}],     canonicalRegistrationTokenCount:0,     failureCount:0,     successCount:1     multicastId:8154206809408282000}

     

函数执行耗时60002毫秒,状态为:“超时”

上次我上一个项目的时间最好是20毫秒。我仍然不知道这一点。非常感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您正在使用HTTP(S)触发的Cloud Function,这意味着您的代码必须发送响应。由于您的代码无法执行此操作,因此该函数运行60秒钟,然后被Cloud Functions环境终止。这意味着您付出的时间比实际需要的时间多,因此您需要解决它。

例如:

// 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);
  }); 

这将消除Cloud Functions日志中的消息,并确保您仅支付实际需要的时间。