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

Device Push Notification Plugin

DESCRIPTION

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".

Contents

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+

Supported Platforms

  • Android
  • iOS

Platforms Under Development

  • WP8

Cordova and PhoneGap CLI

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

PhoneGap Build Support

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-phonegap" source="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-phonegap" source="npm" version="0.3.8" />

Plugin API

Whitelist

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

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

To register a new device

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

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

To get id or token device

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

    document.addEventListener("deviceRegistered", successDeviceRegistered, false);
 
    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.

To manager a notification received

You can manage notifications received with the next method

    document.addEventListener('notificationReceived', successNotificationReceived, false);
 
    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);

To activate or not gps position record

You can activate or deactivate gps position record.

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

To put additional user data for segmentation

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.

最新文章

  1. BPM始终服务于人,落脚于人
  2. 利用线上数据验证系统 Gor
  3. ThinkPad L440 FN键设置
  4. [ActionScript 3.0] as3可以通过CDATA标签声明多行字符串
  5. Centos6.4 为用户添加sudo功能
  6. (转)iOS Wow体验 - 第五章 - 利用iOS技术特性打造最佳体验
  7. C/C++中经常使用的字符串处理函数和内存字符串函数
  8. python3 获取阿里云ECS 实例及监控的方法
  9. 原型那些事 - JavaScript深入浅出(三)
  10. python实现列表倒叙打印
  11. Springboot 系列(十三)使用邮件服务
  12. H5 俄罗斯方块Demo
  13. 15、TypeScript-函数
  14. 001.[python学习]写在前面的
  15. ORM(二)
  16. h5活动页开发总结记录
  17. MSVC 12: compiler error in boost/type_traits/common_type.hpp
  18. World Cup 996B(排队模拟)
  19. Mybatis-Plus 实战完整学习笔记(一)------简介
  20. Ant在MyEclipse中的配置总结

热门文章

  1. 自己总结的web前端知识体系大全【欢迎补充】
  2. JavaScript学习笔记-用于模式匹配的String方法
  3. 关于EJB的理解
  4. runtime之玩转成员变量
  5. iOS 上传新版本到AppStore时报错ITMS-90034
  6. zendstudio文件编码修改问题
  7. Extjs4.1中图片数据源
  8. 配置mysql远程访问权限,大家可能掉过的那些坑~
  9. Symantec Backup Exec 报&quot;Access denied to directory xxx&quot; Error Code E0008488
  10. linux pstack命令总结