如何使用swift3从ios设备触发firebase的推送通知

如何使用swift3从ios设备触发firebase的推送通知,第1张

概述我是新的iOS开发人员. 我正在使用firebase,我正在尝试创建一个聊天应用程序,我需要在收到任何消息时通知用户. 为此,我尝试了firebase推送通知,但是当其他用户发送消息时无法触发它们. 我找到的唯一方法是使用firebase控制台发送推送通知,但它不符合我的要求. 我刚配置了本地通知. 请指导我如何在不使用firebase控制台的情况下触发推送通知. 终于在挣扎了近1个月后找到了解决 我是新的iOS开发人员.

我正在使用firebase,我正在尝试创建一个聊天应用程序,我需要在收到任何消息时通知用户.
为此,我尝试了firebase推送通知,但是当其他用户发送消息时无法触发它们.
我找到的唯一方法是使用firebase控制台发送推送通知,但它不符合我的要求.
我刚配置了本地通知.
请指导我如何在不使用firebase控制台的情况下触发推送通知.

解决方法 终于在挣扎了近1个月后找到了解决方案.

这些是基本步骤

>尽快确保您拥有一个活跃的Apple开发者帐户
>只需启用firebase推送通知
here ie the link of youtube video for this step
>现在您的应用程序已设置为firebase远程通知,但我们只能从firebase控制台触发它们,所以这里是棘手的部分. here is the link of video to enable firebase console on your mac
>这也是第一次看到video会很好,因为在这个视频中他们将学会在node.Js中编写代码并将其部署到firebase.
>现在,如果有人想制作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后发送通知

希望它会给你一个好主意,让你根据自己的需要使用自己的通知.

总结

以上是内存溢出为你收集整理的如何使用swift3从ios设备触发firebase的推送通知全部内容,希望文章能够帮你解决如何使用swift3从ios设备触发firebase的推送通知所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/999817.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-21
下一篇2022-05-21

发表评论

登录后才能评论

评论列表(0条)

    保存