Code Monkey home page Code Monkey logo

nativescript-onesignal's Introduction

NativeScript-OneSignal

A Nativescript plugin that wraps the iOS and Android OneSignal Push Notifications SDK.

Contributors

OneSignal-iOS-SDK

OneSignal-Android-SDK

Supported Platforms

  • iOS 9+ incl 14.2
  • Android

Installation

tns plugin add nativescript-onesignal

iOS

Enable background notifications and add Push Cotifications in Xcode project.

Android

Add your google-services.json file to /App_Resources/Android/

Usage

JavaScript and TypeScript

var TnsOneSignal = require('nativescript-onesignal').TnsOneSignal

iOS

TnsOneSignal is the native iOS OneSignal class.

In your app.js:

var TnsOneSignal = require('nativescript-onesignal').TnsOneSignal

if (application.ios) {
        const MyDelegate = (function (_super) {
            __extends(MyDelegate, _super);
            function MyDelegate() {
                _super.apply(this, arguments);
            }
            MyDelegate.prototype.applicationDidFinishLaunchingWithOptions = function (application, launchOptions) {

                console.log("Application data: " + application);
                try {
                    console.log('Onesignal starts here:', TnsOneSignal);
                    TnsOneSignal.initWithLaunchOptions({ kOSSettingsKeyAutoPrompt: true, kOSSettingsKeyInAppLaunchURL: true, kOSSettingsKeyInFocusDisplayOption: 2, kOSSettingsKeyProvidesAppNotificationSettings: true });
                    TnsOneSignal.setAppId("xxxxxxx-xxxxxx-xxxxxxxx-xxxxxxxx-xxxxxxxxxxxxxx");

                    // It is important to call this for any user response. Default iOS option (allow and deny) will be provided.
                    TnsOneSignal.promptForPushNotificationsWithUserResponse(funCallback());
                    function funCallback(permission) {
                        console.log("Permission selection callback:" + permission);
                    }

                } catch (error) {
                    console.log("Error: ", error)
                }
                return true;
            };
            MyDelegate.prototype.applicationDidBecomeActive = function (application) {
                console.log("Application is activated: " + application);
            };
            MyDelegate.ObjCProtocols = [UIApplicationDelegate];

            return MyDelegate;
        })(UIResponder);

        application.ios.delegate = MyDelegate;
    }

Android

TnsOneSignal is the native Android com.onesignal.OneSignal class.

In your app.js:

var TnsOneSignal = require('nativescript-onesignal').TnsOneSignal

if (application.android) {
    application.on(application.launchEvent, function (args) {
        try {

            TnsOneSignal.startInit(application.android.context).setNotificationOpenedHandler(new TnsOneSignal.NotificationOpenedHandler({

                notificationOpened: function (result) { // once notification is opened..
		// Catch payload objects sent from OneSignal dasboard
                    var obj = JSON.parse(result.notification.payload.rawPayload);
                    console.log(obj.alert);
                    var obj2 = JSON.parse(obj.custom);
                    // Onesignal example key pairs as payload
                    console.log("received example key param 1: " + obj2.a.key1);
                    console.log("received example key param 2: " + obj2.a.key2);

                }
		
            })).init();
            TnsOneSignal.setInFocusDisplaying(TnsOneSignal.OSInFocusDisplayOption.Notification);
            TnsOneSignal.startInit(application.android.context).init();
        } catch (error) {
            console.log('error', error);
        }
    })

}

API Reference

iOS API Reference

Android API Reference

Typescript Typings

iOS

Android - In the works...

Demo

npm run setup
# iOS
npm run demo.ios
# Android
npm run demo.android

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.