【问题标题】:how to trigger push notifications of firebase from an ios device using swift3如何使用swift3从ios设备触发firebase的推送通知
【发布时间】:2018-03-06 08:53:34
【问题描述】:

我是新的 iOS 开发者。

我正在使用 firebase,我正在尝试制作一个聊天应用程序,当他/她收到任何消息时,我需要在该应用程序中通知用户。 为此,我尝试了 firebase 推送通知,但是当其他用户发送消息时无法触发它们。 我发现的唯一方法是使用 firebase 控制台发送推送通知,但它不能满足我的要求。 我刚刚配置了本地通知。 请指导我如何在不使用 Firebase 控制台的情况下触发推送通知。

【问题讨论】:

  • 您可以使用firebase.google.com/docs/database/ios/start。例如,如果用户 A 收到消息,firebase 会自动向设备发送事件。在这种情况下,您可以创建自己的通知。
  • @DanielQ 所以你建议我使用 ios 的本地通知,当 firebase 的观察方法起作用时会自动触发。?我试过这个,但无法让它工作,因为我找不到一种方法来触发除日历、位置和时间之外的任何事件的本地通知。请告诉我该怎么做
  • @DanialQ 先生,正如我所提到的,我已经实现了本地通知,但是如果没有计时器、位置或日历触发器,我无法找到触发它们的方法。在建议的教程中使用时间触发器。但我想触发我的数据更改通知(在 firebase 的观察方法中)。

标签: ios firebase swift3 push-notification firebase-cloud-messaging


【解决方案1】:

苦苦挣扎了近1个月终于找到了解决办法。

这些是基本步骤

  1. 首先你需要确保你有一个活跃的苹果开发者帐户

  2. 只需启用 Firebase 推送通知 here ie the link of youtube video for this step

  3. 现在您的应用已设置为 Firebase 远程通知,但我们只能从 Firebase 控制台触发它们,所以这是棘手的部分。 here is the link of video to enable firebase console on your mac
  4. 第一次很高兴看到这个video,因为在这个视频中,他们将学习在 node.js 中编写代码并将其部署到 firebase。
  5. 现在,如果有人想要创建 API 而不是触发触发器,那么这里有一段 API 代码,它通过从 firebase 获取他的 FCM 令牌来向其他用户发送通知... 您可以根据需要进行修改

我发现很难以确切的形式发布代码,但代码从这里开始

const functions = require('firebase-functions');

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

const ref=admin.database().ref();
exports.sendNotification=functions.https.onRequest((req,res) =>{

    const id = req.query.id
    const title = req.query.title
    const message = req.query.message
    const key = req.query.key
    // admin.database.ref()

    const payload ={

        notification: {
            title: title,
            body: message,
            //badge: '1',
            sound: 'default',
        }
    };

    console.log('Param [key] is '+key);

    const keys = [] ;

    ref.child('keys').once('value')
    .then(snap => {
        snap.forEach(childSnap => {

            const loopKey=childSnap.val().key;
            keys.push(loopKey);

        })

        return keys;
    })
    .then(keys=>{

        //console.log('Keys in database : '+keys.join());

        if(keys.indexOf(key)>-1)
        {
            if(!key || !id || !message)
            {
                res.status(400).send('Bad request');
            }
            else{
                ref.child('users').child(id).child('fcmToken').once('value')
                .then(snapshot => {
                    if (snapshot.val()){
                        const token = snapshot.val()
                        console.log(token);
                        return admin.messaging().sendToDevice(token,payload).then(response =>{
                            res.status(200).send('Notification sent')
                        });
                    }   
                });
            }
        }
        else
        {
            console.log("In-valid key : "+key);

            res.status(400).send('Bad request');
        }   
    })
    .catch(error => {
        res.send(error)
    });

});

到此结束

这是将你的 fcm 存储到数据库的功能

func postToken(token: String, id: String){
    print("FCM Token \(token)")
    let ref = Database.database().reference().child("users").child(id)
    ref.child("fcmToken").setValue(token)
}

这是我用来触发这个 API 的函数

func sendNotification(id: String, title: String, message: String){
    var url = "your URL"
    var urlComponents = NSURLComponents(string: url)
    urlComponents?.queryItems = [
        URLQueryItem(name: "key", value: self.apiKey),
        URLQueryItem(name: "id", value: id),
        URLQueryItem(name: "title", value: title),
        URLQueryItem(name: "message", value: message)
    ]

    Alamofire.request((urlComponents?.url)!).responseJSON { (response) in
        print(response.response)
        print(response.response)
        print(response.result)
    }

}

上面的API是根据我的数据库结构写的。您可以根据自己的结构轻松更改它。

完成所有操作后,您将能够在点击 URL 后发送通知

希望它会给您一个好主意,让人们根据您的需要使用自己的通知。

【讨论】:

    【解决方案2】:

    我能够使用本文中的 PushNotificationSender 类从我的代码发送推送通知 - https://www.iosapptemplates.com/blog/ios-development/push-notifications-firebase-swift-5

    【讨论】:

      猜你喜欢
      • 2017-12-07
      • 2017-10-29
      • 1970-01-01
      • 2016-10-06
      • 1970-01-01
      • 1970-01-01
      • 2020-12-24
      • 2023-03-16
      • 1970-01-01
      相关资源
      最近更新 更多