All Projects → shial4 → VaporGCM

shial4 / VaporGCM

Licence: MIT license
A simple Android GCM/FCM library for Swift/Vapor

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to VaporGCM

mysql-provider
MySQL provider for the Vapor web framework.
Stars: ✭ 31 (+24%)
Mutual labels:  vapor, vapor-provider
wkhtmltopdf
Generate and return PDFs from Vapor views
Stars: ✭ 53 (+112%)
Mutual labels:  vapor, vapor-provider
sendgrid
SendGrid-powered mail backend for Vapor
Stars: ✭ 66 (+164%)
Mutual labels:  vapor, vapor-provider
JSONAPISerializer
JSONAPISerializer for Server Side Swift
Stars: ✭ 21 (-16%)
Mutual labels:  vapor, vapor-swift
template
A Vapor template for convenient and fast scaffolding 🏎
Stars: ✭ 33 (+32%)
Mutual labels:  vapor, vapor-swift
VaporCRUDRouter
A Rails-inspired extension to Vapor's routing system
Stars: ✭ 58 (+132%)
Mutual labels:  vapor, vapor-swift
OrderSystem
An independent micro-service that takes orders in and processes payments.
Stars: ✭ 16 (-36%)
Mutual labels:  vapor, vapor-swift
fcmpush
Firebase Cloud Messaging API wrapper for Ruby, suppot HTTP v1 API including access_token auto refresh feature.
Stars: ✭ 44 (+76%)
Mutual labels:  push-notifications, fcm-notifications
leaf-markdown
Markdown renderer for Vapor
Stars: ✭ 51 (+104%)
Mutual labels:  vapor, vapor-provider
VaporElasticsearch
A Vapor/Swift Elasticsearch client
Stars: ✭ 26 (+4%)
Mutual labels:  vapor, vapor-swift
xcode-leaf-color-schemer
https://ashokgelal.com/2017/01/19/leaf_color_schemer_xcode/?ref=github
Stars: ✭ 26 (+4%)
Mutual labels:  vapor, vapor-swift
apns
Vapor APNS for iOS
Stars: ✭ 59 (+136%)
Mutual labels:  vapor, push-notifications
Lingo-Vapor
Vapor provider for Lingo - the Swift localization library
Stars: ✭ 45 (+80%)
Mutual labels:  vapor, vapor-provider
puffery
A SwiftUI iOS App and Vapor Server to send push notifications fueled by Siri Shortcuts.
Stars: ✭ 17 (-32%)
Mutual labels:  vapor, vapor-swift
fluent-provider
A provider for including Fluent in Vapor applications
Stars: ✭ 13 (-48%)
Mutual labels:  vapor, vapor-provider
web-template
A starting point for web applications
Stars: ✭ 42 (+68%)
Mutual labels:  vapor
fcm
Golang client library for Firebase Cloud Messaging.
Stars: ✭ 22 (-12%)
Mutual labels:  push-notifications
Console Kit
💻 APIs for creating interactive CLI tools.
Stars: ✭ 252 (+908%)
Mutual labels:  vapor
pushnotification
Push notifications with Spring Boot and OneSignal
Stars: ✭ 25 (+0%)
Mutual labels:  push-notifications
claim-reporter-pwa-poc
Example PWA application with Angular 6 and backend with Node
Stars: ✭ 39 (+56%)
Mutual labels:  push-notifications

VaporGCM

Language Vapor GitHub license Build Status CircleCI codecov

VaporGCM is a simple, yet elegant, Swift library that allows you to send Android/iOS Push Notifications using HTTP protocol in Linux & macOS. Created for Vapor. Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you reliably deliver messages at no cost. Firebase Cloud Messaging

🔧 Installation

A quick guide, step by step, about how to use this library.

1- Add VaporGCM to your project

Add the following dependency to your Package.swift file:

.Package(url:"https://github.com/shial4/VaporGCM.git", majorVersion: 0, minor: 2)

And then make sure to regenerate your xcode project. You can use vapor xcode -y command, if you have the Vapor toolbox installed.

🚀 Usage

1- Send Message

It's really easy to get started with the VaporGCM library! First you need to import the library, by adding this to the top of your Swift file:

import VaporGCM

The easiest way to setup VaporGCM is to create object for example in your main.swift file. Like this:

import Vapor
import VaporGCM

let drop = Droplet()
let gcm = VaporGCM(forDroplet: drop, serverKey: "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...")

After you have the VaporGCM instance, we can go ahead and create notification object. To do that we need to use GCMPayload

let gcmPayload = GCMPayload()

Creating object is simple but it comes with multiple properties to configure:

  1. title title Indicates notification title. This field is not visible on iOS phones and tablets.
  2. body body Indicates notification body text.
  3. icon icon Indicates notification icon. Default value is set to myicon
  4. sound sound Indicates a sound to play when the device receives the notification.
  5. tag tag Indicates whether each notification message results in a new entry on the notification center on . If not set, each request creates a new notification. If set, and a notification with the same tag is already being shown, the new notification replaces the existing one in notification center.
  6. color color Indicates color of the icon, expressed in #rrggbb format
  7. clickAction clickAction The action associated with a user click on the notification.
  8. bodyLocKey bodyLocKey Indicates the key to the body string for localization.
  9. bodyLocArgs bodyLocArgs Indicates the string value to replace format specifiers in body string for localization.
  10. titleLocKey titleLocKey Indicates the key to the title string for localization.
  11. titleLocArgs titleLocArgs Indicates the string value to replace format specifiers in title string for localization.

Despite of notification object we can add custom data to our message. To do that we will need Vapor JSON object

After we've created the notification object it's time to actually send the push message.

let data = JSON([
"score":"5x1",
"time":"15:10"
])

We can now create message bject which is required to send notification to device

let message = PushMessage(gcmPayload: gcmPayload, data: data)

Message object do not require Notification or JSON object. We can as well send empty message.

let message = try? PushMessage()

We can create message just with notification peyload

let message = try? PushMessage(gcmPayload: gcmPayload)

Or just with data payload

let message = try? PushMessage(data: data)

Now we can send the message with notification payload and data payload to just one device, using:

let message = try! PushMessage(gcmPayload: gcmPayload, data: data)
let response = try? gcm.send(message, to: "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...")

Sending message to device return Vapor Response object. Thanks to that we can interprate response by ourselfs and do futher steps.

VaporGCM allows us to send message to multiple device list at once by using:

try! gcm.send(message, to: ["","",""], responseHandler: { (token, response, error) in
    guard error == nil else {
        print("Something wrong happen")
        return
    }
    guard response?.status.statusCode == 200 else {
        print("Error from server")
        return
    }
    print("Device token: \(token)")
})

Sending message to multiple device return response by responseHandler for each device received and identyfied by token.

Done! To summarize

import Vapor
import VaporGCM

let drop = Droplet()
let gcm = VaporGCM(forDroplet: drop, serverKey: "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...")

let gcmPayload = GCMPayload()
let data = JSON([
    "score":"5x1",
    "time":"15:10"
])

let message = try! PushMessage(gcmPayload: gcmPayload, data: data)
let response = try? gcm.send(message, to: "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...")
try! gcm.send(message, to: ["","",""], responseHandler: { (token, response, error) in
    guard error == nil else {
        print("Something wrong happen")
        return
    }
    guard response?.status.statusCode == 200 else {
        print("Error from server")
        return
    }
    print("Device token: \(token)")
})

2- Device Group

Before sending messages to a device group, you must:

  • Obtain registration tokens for each device you want to add to the group.
  • Create the notification_key, which identifies the device group by mapping a particular group (typically a user) to all of the group's associated registration tokens.

Basic management of device groups — creating and removing groups, and adding or removing devices — is performed via the:

let group = DeviceGroup(operation: .create,
                            name: "appUser-Chris",
                            registrationIds: ["4", "8", "15", "16", "23", "42"])

Where DeviceGroupOperation can be:

case create
case add
case remove

Sending Device Group Message. This message will add devices with ids to exsisting group with name appUser-Chris A successful request returns a notificationKey. Save the notificationKey and the corresponding name to use in subsequent operations.

let group = DeviceGroup(operation: .add,
                                name: "appUser-Chris",
                                registrationIds: ["16", "9"])
        let response = try? gcm.sendDeviceGroup(group, forProject: "SENDER_ID")
        if let json = response?.json, response?.status.statusCode == 200 {
            let notificationKey: String = try! json.extract("notification_key")
            print(notificationKey)
        }

A successful request returns a notification_key inside JSON Save the notification_key and the corresponding name to use in subsequent operations.

Contributing

Be welcome to contribute to this project! :)

Questions

You can join the Vapor slack. Or you can create an issue on GitHub.

License

This project was released under the MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].