2

を追加しました。Firebaseを設定しました。 データの読み取り、書き込み、削除ができました。 セットアッププッシュ通知もあり、Firebaseコンソールから受信します。Firebaseデータベースでプッシュ通知を受信する子供が私のIOSアプリケーションで

私は、Firebaseデータベースに新しいデータを追加するときにプッシュ通知を受け取ることができませんでした。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    FirebaseApp.configure() 
    // Messaging.messaging().delegate = self 
    Messaging.messaging().shouldEstablishDirectChannel = true  

    //Device Token for Push 
    // iOS 10 support 
    if #available(iOS 10, *) { 
     UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in } 
     application.registerForRemoteNotifications() 
    } 
     // iOS 7 support 
    else { 
     application.registerForRemoteNotifications(matching: [.badge, .sound, .alert]) 
    } 
    return true 
} 

は、私は私のデータベースノードの一つに加入しようとするが、私はfirebaseガイドに従って、自分のプロジェクトに設定firebase機能を持った後に何かが

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 
    // Convert token to string 
    let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)}) 
    print("APNs device token: \(deviceTokenString)") 
    //Messaging.messaging().setAPNSToken(deviceToken, type: MessagingAPNSTokenType.sandbox) 
    Messaging.messaging().subscribe(toTopic: "/topics/news") 

    // Persist it in your backend in case it's new 
    UserDefaults.standard.set(deviceTokenString, forKey: "PushDeviceTokenString") 
} 
+0

私はあなたが[Firebaseのためのクラウド機能](https://firebase.google.com/products/functions/?gclid=EAIaIQobChMI_OaSl92R1QIVgpW9Ch1NzgVjEAAYASAAEgIvb_D_BwE)を見ているべきだと思います。 –

+0

ええ、私はそれを得ました。 npmとノード.jsを介してfirebaseツールをインストールしました。次のステップは、firebase loginを実行することです。しかし、私の端末はfirebase:コマンドが見つかりませんでした。 –

+1

私はすべてが走ってしまった。ありがとう。 –

答えて

0

を変更したときに私はプッシュ通知を取得していません。

私がしなければならないことは、イベントをキャッチして目的の機能を実行するサーバー側の機能を作成して展開することでした。

//Firebase functions setup 
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('/news/{id}').onWrite(event => { 
    //get the snapshot of the written data 
    const snapshot = event.data; 
    //create a notofication 
    const payload = { 
     notification: { 
      title: snapshot.child("title").val(), 
      body: snapshot.child("message").val(), 
      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 'fcmToken' 
    //and all user of my app will receive the notification 
    return admin.database().ref('fcmToken').once('value').then(allToken => { 
     if (allToken.val()){ 
      const token = Object.keys(allToken.val()); 
      return admin.messaging().sendToDevice(token, payload).then(response => { 
       console.log("Successfully sent message:", response); 
      }); 
     } 
    }); 
}); 
関連する問題