3

I have a Firebase Swift chat app where I'd like to send a push notification to a specific user. I have already captured and have access to the user's device token.

All the references mention having to have a 'web app' to manage this, but I haven't managed to find any specific examples of this.

  1. Is it necessary to have a web app to manage push notifications for Firebase?

  2. If so, are there any examples of how this can work?

Thank you.

XCode Warrier
  • 765
  • 6
  • 20
  • You can either use [Cloud Functions](https://firebase.google.com/docs/functions/) or your own server. Example using Cloud Functions: https://firebase.google.com/docs/functions/use-cases#notify_users_when_something_interesting_happens – nathan Aug 14 '17 at 16:03
  • I have already answered this in another question, feel free to check if out in case you need some help on this. https://stackoverflow.com/questions/37481992/firebase-chat-push-notifications/37684936#37684936 – ZassX Aug 15 '17 at 10:05
  • @ZassX thank you for taking the time to answer but your linked answer is wrong - I now have this working with Cloud Functions (thanks @nathan) – XCode Warrier Aug 15 '17 at 10:24
  • @XCodeWarrier No problem. I didn't say it was the right answer, it is an alternative you can consider using. Just as an idea. Glad you solved it! PS: You should update your answer and paste your solution here so it can help other with same problem. :) – ZassX Aug 15 '17 at 12:03

2 Answers2

8
  • First do the all configuration for Firebase Cloud Messaging. Can follow this link https://firebase.google.com/docs/cloud-messaging/ios/client
  • Now you need to access Firebase registration token for your associated user whom you want to send a push notification. You can get this token by following below steps:
    1. Add 'Firebase/Messaging' pod to your project.
    2. import FirebaseMessaging in your AppDelegate.swift file.
    3. Conform MessagingDelegate protocol to your AppDelegate class.
    4. Write didRefreshRegistrationToken delegate method and get your user's registration token:
  • Now you are ready to send push notification to your specific user. Send notification as below:

    func sendPushNotification(payloadDict: [String: Any]) {
       let url = URL(string: "https://fcm.googleapis.com/fcm/send")!
       var request = URLRequest(url: url)
       request.setValue("application/json", forHTTPHeaderField: "Content-Type")
       // get your **server key** from your Firebase project console under **Cloud Messaging** tab
       request.setValue("key=enter your server key", forHTTPHeaderField: "Authorization")
       request.httpMethod = "POST"
       request.httpBody = try? JSONSerialization.data(withJSONObject: payloadDict, options: [])
       let task = URLSession.shared.dataTask(with: request) { data, response, error in
          guard let data = data, error == nil else {
            print(error ?? "")
            return
          }
          if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
            print("statusCode should be 200, but is \(httpStatus.statusCode)")
            print(response ?? "")
          }
          print("Notfication sent successfully.")
          let responseString = String(data: data, encoding: .utf8)
          print(responseString ?? "")
       }
       task.resume()
    }
    
  • Now call above function as:

    let userToken = "your specific user's firebase registration token"
    let notifPayload: [String: Any] = ["to": userToken,"notification": ["title":"You got a new meassage.","body":"This message is sent for you","badge":1,"sound":"default"]]
    self.sendPushNotification(payloadDict: notifPayload)
    
Ashvini
  • 342
  • 3
  • 11
  • This works very well for sending notifications without making a web app. Very helpful! – Mikkel Cortnum May 10 '21 at 15:18
  • This still works? When I do the func nothing happens. Edit. The console says: Notfication sent successfully. {"multicast_id":269539832962150,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"1657369222891"}]}. But no push on my iPhone. – submariner Jul 09 '22 at 12:17
0

No, it is not necessary to have a web app to manage push notifications with Firebase

Look at the answer from Ashvini, it gives you a method to send messages directly from your iOS app, to other devices.

You can also use the Firebase Console to manually send push messages to multiple users at the same time.

Here is the documentation for sending messages through a web app/server. Firebase Documentation

Mikkel Cortnum
  • 481
  • 4
  • 11