Firebase: регистрационные токены, предоставляемые sendToDevice (), должны быть непустой строкой или непустым массивом - PullRequest
0 голосов
/ 13 ноября 2018

Использование FCM для отправки push-уведомлений на мое устройство iOS. Просто не мог заставить его работать. Удалось получить токен, но по какой-то причине он не был принят sendToDevice () Код swift работает нормально, получая сообщения от консоли Firebase. Пожалуйста помоги! Я просто хочу очень простое push-уведомление! Новый заказ от клиента и шеф-повара получает push-уведомление - желательно со звуком и значком. Спасибо всем!

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

//register to onWrite event of my node news
exports.sendPushNotification = functions.database.ref('/Customer/Orders/{id}').onCreate((snap, context) => {

    //get the snapshot of the written data
    const snapShot = snap.val();
    console.log(snapShot);
    //create a notification
    const payload = {
        notification: {
            title: "New Order",
            body: "Please Check",
            badge: '1',
            sound: 'default',
        }
    };

    //send a notification to all fcmToken that are registered
    //In my case the users device token are stored in a node called 'fcmTokens'
    //and all user of my app will receive the notification
    return admin.database().ref('/Customer/fcmTokens').once('value').then(allToken => {
        const token = Object.keys(allToken.val());
        console.log(token);
            return admin.messaging().sendToDevice(token, payload)
    });
});

Console.log (token) выглядит следующим образом

Ошибка: «Ошибка: токены регистрации, предоставленные sendToDevice (), должны быть непустой строкой или непустым массивом».

Мой код Appledelegate.swift - прямо с Google's Firebase GitHub

import UIKit
import Firebase
import FirebaseMessaging
import FirebaseInstanceID
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

  var window: UIWindow?
  let gcmMessageIDKey = "gcm.message_id"

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    FirebaseApp.configure()
    Messaging.messaging().delegate = self
    Database.database().isPersistenceEnabled = true
    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()

    // [END register_for_notifications]
    return true
    }

    // [START receive_message]
    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)
    }
    // [END receive_message]
    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("Unable to register for remote notifications: \(error.localizedDescription)")
    }

    // This function is added here only for debugging purposes, and can be removed if swizzling is enabled.
    // If swizzling is disabled then this function must be implemented so that the APNs token can be paired to
    // the FCM registration token.
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        print("APNs token retrieved: \(deviceToken)")

        // With swizzling disabled you must set the APNs token here.
        // Messaging.messaging().apnsToken = deviceToken
    }
}

// [START ios_10_message_handling]
@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {

    // Receive displayed notifications for iOS 10 devices.
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        let userInfo = notification.request.content.userInfo

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

        // Change this to your preferred presentation option
        completionHandler([])
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo
        // Print message ID.
        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID: \(messageID)")
        }

        // Print full message.
        print(userInfo)

        completionHandler()
    }
}
// [END ios_10_message_handling]

extension AppDelegate : MessagingDelegate {
    // [START refresh_token]
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
        print("Firebase registration token: \(fcmToken)")
        let ref = Database.database().reference().child("Customer").child("fcmTokens")
        ref.setValue(["fcmToken" : "\(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.
    }
    // [END refresh_token]
    // [START ios_10_data_message]
    // Receive data messages on iOS 10+ directly from FCM (bypassing APNs) when the app is in the foreground.
    // To enable direct data messages, you can set Messaging.messaging().shouldEstablishDirectChannel to true.
    func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
        print("Received data message: \(remoteMessage.appData)")
    }
    // [END ios_10_data_message]
}

1 Ответ

0 голосов
/ 10 апреля 2019

если код такой

Console.log(token) 

и то, что вы видите в консоли, это

{ fcmToken: 'eRAAQEJAowI:APA91bHhZAa-kBhynq5b8_LkOhzYcp3v06H4IB5NgRp6k-2tZl67nF_eWuVHxL07KlzDVRfaINyoXBWDAJDFIFN23415NCHU-gZvK1LKw6Rwx8zz9S-' }

тогда токен - это некий объект, состоящий из поля

fcmToken

и значение

'eRAAQEJAowI:APA91bHhZAa-kBhynq5b8_LkOhzYcp3v06H4IB5NgRp6k-2tZl67nF_eWuVHxL07KlzDVRfaINyoXBWDAJDFIFN23415NCHU-gZvK1LKw6Rwx8zz9S-'

Вам нужно извлечь значение из токена, чтобы в итоге вы получили всего

'eRAAQEJAowI:APA91bHhZAa-kBhynq5b8_LkOhzYcp3v06H4IB5NgRp6k-2tZl67nF_eWuVHxL07KlzDVRfaINyoXBWDAJDFIFN23415NCHU-gZvK1LKw6Rwx8zz9S-'

Я собираюсь угадать

token.fcmToken

Вы можете проверить это здесь https://www.w3schools.com/code/tryit.asp?filename=G2Y6TKTR4ZD7

...