[转]com.devicepush.cordova-phonegap Device Push Notification Plugin

本文转自:https://www.npmjs.com/package/com.devicepush.cordova-phonegap

 

Device Push Notification Plugin

This plugin is for use with Cordova, and allows your application to receive push notifications on Android and iOS devices.

Important - Push notifications are intended for real devices. The registration process will fail on the iOS simulator. Notifications can be made to work on the Android Emulator, however doing so requires installation of some helper libraries, as outlined here, under the section titled "Installing helper libraries and setting up the Emulator".

Automatic Installation

Below are the methods for installing this plugin automatically using command line tools. For additional info, take a look at the Plugman Documentation and Cordova Plugin Specification.

This requires phonegap/cordova 5.0+

  • Android
  • iOS
  • WP8

The plugin can be installed via the Cordova command line interface:

  1. Navigate to the root folder for your phonegap project.

  2. Run the command:

cordova plugin add com.devicepush.cordova-phonegap

or

phonegap plugin add com.devicepush.cordova-phonegap

The plugin can be installed via PhoneGap Build:

  1. Open config.xml file of your project.

  2. Add this line:

<gap:plugin name="com.devicepush.cordova-phonegapsource="npm/>

If you want to specify a particular version of the plugin you can add the version attribute to the gap tag.

<gap:plugin name="com.devicepush.cordova-phonegapsource="npmversion="0.3.8/>

Plugin API

Add *.devicepush.com domain in the config.xml file:

<access origin="*.devicepush.com/>
<allow-navigation href="*.devicepush.com/>

When the device is ready, you must call the register function.

    var app {
        initialize: function({
            this.bindEvents();
        },
        bindEvents: function({
            document.addEventListener('deviceready'this.onDeviceReadyfalse);
        },
        onDeviceReady: function({
            app.receivedEvent('deviceready');
            devicePush.register({
                idUser'USER_ID'// Your User ID in Device Push 
                idApplication'APPLICATION_ID'// Aplication ID in Device Push 
                positiontrue // Activate or deactivate gps position record. Default value is false 
                additionalData{// Currently in development 
            });
        },
        receivedEvent: function(id{}
    };

You can get the device id or token of the device.

    document.addEventListener("deviceRegistered", successDeviceRegisteredfalse);
 
    function successDeviceRegistered(evt){
        console.log("Device Id+ evt.devicePushId);
        var id = evt.devicePushId;
        console.log("Device Token+ evt.devicePushToken);
        var tokenDevice = evt.devicePushToken;
    }

With this ID you can send notification from your server.

You can manage notifications received with the next method

    document.addEventListener('notificationReceived', successNotificationReceivedfalse);
 
    function successNotificationReceived(evt){
        // evt.data.message,  
        // evt.data.title,  
        // evt.data.count,  
        // evt.data.sound,  
        // evt.data.additionalData 
        console.log(evt.data.message// data is your text sent 
    }

To show a dynamic and floating notification, you have to add the following function into the function successNotificationReceived.

    devicePush.showNotification(evt.data.message);

You can activate or deactivate gps position record.

    devicePush.setPosition(true)//Active gps position record, true o false. Default value is false. 

To activate the segmentation of notifications, you will have to send additional user data, such as personal data.

    // Currently in development 
    devicePush.putAdditionalData({
        additionalData{
            name'',
            surnames'',
            age'',
            gender''
        
    });

You can see more information about this at: http://www.devicepush.com/documentation-push-notification/

Looking at the above message handling code for Android, a few things bear explanation. Your app may receive a notification while it is active (INLINE). If you background the app by hitting the Home button on your device, you may later receive a status bar notification. Selecting that notification from the status will bring your app to the front and allow you to process the notification (BACKGROUND). Finally, should you completely exit the app by hitting the back button from the home page, you may still receive a notification. Touching that notification in the notification tray will relaunch your app and allow you to process the notification (COLDSTART). In this case the coldstart flag will be set on the incoming event. You can look at the foreground flag on the event to determine whether you are processing a background or an in-line notification. You may choose, for example to play a sound or show a dialog only for inline or coldstart notifications since the user has already been alerted via the status bar.

Since the Android notification data models are much more flexible than that of iOS, there may be additional elements beyond message. You can access those elements and any additional ones via the payload element. This means that if your data model should change in the future, there will be no need to change and recompile the plugin.

Testing

The notification system consists of several interdependent components.

posted on 2016-04-08 09:16  freeliver54  阅读(533)  评论(0编辑  收藏  举报

导航