2

我是新的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

回答

0

終於找到奮鬥了將近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

希望它會給一個不錯的主意,你的人根據自己的需要使用自己的通知工作崗位後發送通知。