如何使用swift3

问题描述:

我是新的iOS开发者在iOS装置触发火力的推送通知。如何使用swift3

我用的火力点,我试图在我需要的时候,他/她收到任何消息通知用户聊天应用。 为此,我尝试了Firebase推送通知,但无法在其他用户发送消息时触发它们。 我发现的唯一方法是使用Firebase控制台发送推送通知,但它不符合我的要求。 我刚刚配置了本地通知。 请指导我如何在不使用Firebase控制台的情况下触发推送通知。

+0

您可以使用https://firebase.google.com/docs/database/ios/start。例如,如果用户A收到消息,则Firebase将自动发送事件以进行设置。在这种情况下,您可以创建自己的通知。 –

+0

@hmail整合Firebase Fallow https://firebase.google.com/docs/database/ios/启动此代码并引用此https:// github。com/firebase/quickstart-ios/tree/master/messaging/MessagingExampleSwift – naga

+0

@DanielQ所以你建议我使用ios的本地通知,当firebase的观察方法工作时它会自动触发。 我试过这个,但无法使它工作,因为我找不到一个方法来触发除日历,位置和时间以外的任何事件的本地通知。 请告诉我我该怎么做 – hmali

终于找到奋斗了将近1个月后的解决方案。

这些是基本的步骤

  1. 杉杉关闭所有你需要确保你有一个积极的苹果开发人员占

  2. 只是使火力推送通知 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中写代码,并将其部署到火力点。
  5. 现在,如果有人想做出,而不是把一个触发那么这里的API是API的代码从火力让他的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

希望它会给一个不错的主意,你的人根据自己的需要使用自己的通知工作岗位后发送通知。