Giter Site home page Giter Site logo

mobile-dvlper / pushnotificationhandler Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gtsifrikas/pushnotificationhandler

0.0 1.0 0.0 25 KB

A simple library to register(optional) and handle push notifications in your app.

License: MIT License

Ruby 26.18% Swift 73.82%

pushnotificationhandler's Introduction

PushNotificationHandler

Version License Platform

Features

  • Register your app for push notifications
  • Subcribe your UIViewController(s) for new push notifications

Communication

  • If you found a bug, open an issue.
  • If you have a feature request, open an issue.
  • If you want to contribute, submit a pull request.

Installation

PushNotificationHandler is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "PushNotificationHandler"

Then, run the following command:

$ pod install

Usage

Registering your app for push notifications

In your AppDelegate.swift file

import PushNotificationHandler
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
    PushNotificationHandler.sharedInstance.registerForPushNotifications(application: application)
    PushNotificationHandler.sharedInstance.handleApplicationStartWith(application: application, launchOptions: launchOptions)
        
    PushNotificationHandler.sharedInstance.registerNewAPNSTokenHandler { (tokenData, token, error) -> (Void) in
        if let validToken = token {
            print("Device Token:", validToken)
        } else {
            print("Device token error: " + (error?.localizedDescription ?? "Strange things are happening"))
        }
    }
}

Also implement the methods as following

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
    PushNotificationHandler.sharedInstance.application(application, didReceiveRemoteNotification: userInfo)
}
func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
    PushNotificationHandler.sharedInstance.application(application, didRegister: notificationSettings)
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    PushNotificationHandler.sharedInstance.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    PushNotificationHandler.sharedInstance.application(application, didFailToRegisterForRemoteNotificationsWithError: error)
}

Subscribe a UIViewController for push notifications

In your UIViewController file

import PushNotificationHandler

Your controller must implement PushNotificationSubscriber protocol

override func viewDidLoad() {
    super.viewDidLoad()
    PushNotificationHandler.sharedInstance.subscribeForPushNotifications(subscriber: self)
}
deinit {
    PushNotificationHandler.sharedInstance.unsubscribeForPushNotifications(subscriber: self)
}
func newPushNotificationReceived(aps: [AnyHashable : Any]) {
    // Do something with the push notification
}

That's it!

Author

George Tsifrikas

PushNotificationHandler is available under the MIT license. See the LICENSE file for more info.

pushnotificationhandler's People

Contributors

txaiwieser avatar gtsifrikas avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.