[SWIFT] Send push notifications from your iOS app via Firebase Cloud Messaging

Introduction

Currently, I'm wondering if it's possible to create a chat-like mechanism (server story) with an application that I'm developing personally and deliver push notifications to specified users and topics when data is added to the Firebase Database. I tried it.

The original story is Send push notifications from iOS client in Firebase.

I did it in Python because Node.js can't (deploy implementation).

environment

--Python 3.5.2 (I forgot to put 3.6)

Preparation on the iOS side

Push notification preparation

AppDelegate.swift


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound],
                                                  categories: nil)
    application.registerUserNotificationSettings(settings)
    application.registerForRemoteNotifications()
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    //APNS registration
    #if DEBUG
    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: .sandbox)
    #else
    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: .prod)
    #endif
}

func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
    //Sign up for a topic
    FIRMessaging.messaging().subscribe(toTopic: "/topics/news")
}

Do a hoge for later push notifications!

Add data to Firebase Database

Hoge.swift


class Hoge: NSObject {
    class func create() {
        let ref = FIRDatabase.database().reference()

        let data: [String : String] = ["title": "title", "body": "Text"]
        ref("message").childByAutoId().setValue(data, withCompletionBlock: { (error, reference) in
            if error != nil {
                print("Success")
            } else {
                print("Failure")
            }
        }
    }
}

THE miscellaneous, but it's like this

Code to monitor Firebase Database

requirements.txt


docutils==0.13.1
gcloud==0.17.0
googleapis-common-protos==1.5.0
httplib2==0.9.2
jws==0.1.3
lockfile==0.12.2
oauth2client==3.0.0
protobuf==3.2.0rc1
pyasn1==0.1.9
pyasn1-modules==0.0.8
pycryptodome==3.4.3
Pyrebase==3.0.27
python-daemon==2.1.2
python-jwt==2.0.1
requests==2.11.1
requests-toolbelt==0.7.0
rsa==3.4.2
six==1.10.0

app.py


import daemon
import json
import pyrebase
import requests

config = {
    # GoogleService-info.plist API_KEY
    "apiKey": "API_KEY",
    "authDomain": "PROJECT_NAME",
    "databaseURL": "https://PROJECT_NAME.firebaseio.com/",
    #May not be needed
    "storageBucket": "gs://PROJECT_NAME.appspot.com"
}

fcm_header = {
    'Content-type': 'application/json; charset=UTF-8',
    #See the image below this code!
    'Authorization': 'key=API_KEY'
}

firebase = pyrebase.initialize_app(config)

def stream_handler(message):
    if message['data'] == None:
        return
    if message['path'] == '/':
        return

    if 'title' in message['data']:
        title = message['data']['title']
    else:
        title = 'Delivery'

    if 'body' in message['data']:
        body = message['data']['body']
    else:
        body = ''

    payload = {'to': '/topics/news', 
               'priority': 'high', 
               'notification': {
                   'title': title,
                   'body': body,
                   'badge': 1,
                   'sound': 'default'
               }
              }

    r = requests.post('https://fcm.googleapis.com/fcm/send', headers = fcm_header, data = json.dumps(payload))
    print(r.json())

if __name__ == '__main__':
    db = firebase.database()
    dc = daemon.DaemonContext()
    with dc:
        stream = db.child("message").stream(stream_handler)

API_KEY location

スクリーンショット_2017-01-18_0_46_34.png

(Please protect your privacy !!)

Description of monitoring code

The Python package uses Pyrebase and python-daemon.

$ pip install pyrebase python-daemon

I'm going to look at the / message directory in the Firebase Database.

if message['data'] == None:
    return
if message['path'] == '/':
    return

This is an exception handling because an error occurred (?) When there was no data in / message. Even though a push notification is sent when the monitoring code is started, the notification is coming to the iOS side, but the alert is not displayed! I put it in because there was something like that.

All you have to do is run the script on the server side. By the way, using python-daemon will daemonize the script! Thank you very much!

$ python app.py

Execution sample

Firebase Database sample

Firebase Databaseサンプル

Notification result

47ad6b91-a6cd-ff84-c153-f0abd580a820.png

Recommended Posts

Send push notifications from your iOS app via Firebase Cloud Messaging
Implemented iOS push notifications in Firebase
Send push notifications to iOS apps in Python
Send a message from the server to your Chrome extension using Google Cloud Messaging for Chrome
Send push notifications to iOS apps with Python2 (with sample code)