Skip to content

Titanium Appcelerator Library for OneSignal Push Notifications Service

License

Notifications You must be signed in to change notification settings

williamrijksen/com.williamrijksen.onesignal

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Titanium OneSignal Build Status

This module gives you the possibility to integrate OneSignal into you're Appcelerator Android or iOS-application. It's even possible to target people by registering tags.

Where to get it:

Please check https://github.com/williamrijksen/com.williamrijksen.onesignal/releases to download latest releases of the module.

Generate Credentials

Before setting up the Titanium SDK, you must generate the appropriate credentials for the platform(s) you are releasing on:

Follow Guide

Setup

  1. Integrate the module into the modules folder and define them into the tiapp.xml file:

    <modules>
      <module platform="iphone" version="2.1.1">com.williamrijksen.onesignal</module>
      <module platform="android" version="2.1.1">com.williamrijksen.onesignal</module>
    </modules>
  2. Configure your app into the App Settings panel for the right Platform (Android and/or iOS).

  3. To use OneSignal on iOS devices, register the OneSignal-appId into tiapp.xml:

    <property name="OneSignal_AppID" type="string">[App-id]</property>
  4. To use OneSignal on Android devices, register some meta-data as well:

    <meta-data android:name="onesignal_app_id"
                   android:value="[App-id]" />
  5. To use rich notifications on iOS 10 you need to add an extension to your app. To do so see:

Usage

  1. Register device for Push Notifications

        // This registers your device automatically into OneSignal
        var onesignal = require('com.williamrijksen.onesignal');
  2. On iOS you'll need to request permission to use notifications:

        onesignal.promptForPushNotificationsWithUserResponse(function(obj) {
            alert(JSON.stringify(obj));
        });
  3. To add the possibility to target people for notifications, send a tag:

        onesignal.sendTag({ key: 'foo', value: 'bar' });
  4. Delete tag:

        onesignal.deleteTag({ key: 'foo' });
  5. Get tags:

        onesignal.getTags(function(e) {
            if (!e.success) {
                Ti.API.error("Error: " + e.error);
                return
            }
    
            Ti.API.info(Ti.Platform.osname === "iphone"? e.results : JSON.parse(e.results));
        });
  6. Set External User ID:

        onesignal.setExternalUserId('your_db_user_id');
  7. Remove External User ID:

        onesignal.removeExternalUserId();
  8. Get if user is subscribed (Boolean):

        var subscribed = onesignal.retrieveSubscribed();
  9. Get One Signal Player ID (String):

        var res = onesignal.retrievePlayerId();
  10. Get One Signal Token (String):

        var token = onesignal.retrieveToken();
  11. Get Permission Subscription State (iOS-only for now):

        var res = onesignal.getPermissionSubscriptionState();
        /* res example:
            {
                "subscriptionStatus": {
                    "userSubscriptionSetting": true,
                    "subscribed": false,
                    "userId": "123-123-123-123-123456789",
                    "pushToken": null
                },
                "permissionStatus": {
                    "status": 2,
                    "provisional": false,
                    "hasPrompted": true
                },
                "emailSubscriptionStatus": {
                    "emailAddress": null,
                    "emailUserId": null
                }
            }
        */
  12. postNotification (iOS-only for now):

        //You can use idsAvailable for retrieving a playerId
        onesignal.postNotification({
            message:'Titanium test message',
            playerIds:["00000000-0000-0000-0000-000000000000"]
        });
  13. Set log level (iOS-only for now):

        onesignal.setLogLevel({
            logLevel: onesignal.LOG_LEVEL_DEBUG,
            visualLevel: onesignal.LOG_LEVEL_NONE
        });
  14. Opened listener:
    The returned content is matching the available payload on OneSignal:

    onesignal.addEventListener('notificationOpened', function (evt) {
        alert(evt);
        if (evt) {
            var title = '';
            var content = '';
            var data = {};
    
            if (evt.title) {
                title = evt.title;
            }
    
            if (evt.body) {
                content = evt.body;
            }
    
            if (evt.additionalData) {
                if (Ti.Platform.osname === 'android') {
                    // Android receives it as a JSON string
                    data = JSON.parse(evt.additionalData);
                } else {
                    data = evt.additionalData;
                }
            }
        }
        alert("Notification opened! title: " + title + ', content: ' + content + ', data: ' + evt.additionalData);
    });
  15. Received listener: The returned content is matching the available payload on OneSignal:

    onesignal.addEventListener('notificationReceived', function(evt) {
        console.log(' ***** Received! ' + JSON.stringify(evt));
    });

Cheers!

Build yourself

iOS

If you already have Titanium installed, skip the first 2 steps, if not let's install Titanium locally.

  1. brew install yarn --without-node to install yarn without relying on a specific Node version
  2. In the root directory execute yarn install
  3. Step into the ios directory
  4. If you want to update the OneSignal SDK:
  • Run carthage update
  • Drag and drop the OneSignal.framework from Carthage/Build/iOS to platform
  1. Alter the titanium.xcconfig to build with the preferred SDK
  2. To build the module execute rm -rf build && ../node_modules/.bin/ti build -p ios --build-only

Android

  1. brew install yarn --without-node to install yarn without relying on a specific Node version
  2. In the root directory execute yarn install
  3. Step into the android directory
  4. Copy build.properties.dist to build.properties and edit to match your environment
  5. To build the module execute rm -rf build && mkdir -p build/docs && ../node_modules/.bin/ti build -p android --build-only

Google Play Services

Since Titanium 7.x this module relies on https://github.com/appcelerator-modules/ti.playservices

If you still need to support Titanium 6.x and you need to change the used Google Play Services version, execute the following actions:

  1. Install the Google Play Services on your system:

    sdkmanager "extras;google;m2repository"
  2. Fetch the 4 needed *.aar files from the SDK path extras/google/m2repository/com/google/android/gms

    • base
    • basement
    • gcm
    • idd
    • location

    For the version you want use.

  3. Extract the *.aar file, and rename the classes.jar to google-play-services-<part>.jar.

  4. Update the used jars in the lib folder.

  5. Update the res folder with the one from the google-play-services-basement.jar