本文转自: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.
- The Android implementation uses Google's GCM (Google Cloud Messaging) service.
- The iOS version is based on Apple APNS Notifications.
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:
-
Navigate to the root folder for your phonegap project.
-
Run the command:
or
PhoneGap Build Support
The plugin can be installed via PhoneGap Build:
-
Open config.xml file of your project.
-
Add this line:
If you want to specify a particular version of the plugin you can add the version attribute to the gap tag.
Plugin API
Whitelist
Add *.devicepush.com domain in the config.xml file:
To register a new device
When the device is ready, you must call the register function.
To get id or token device
You can get the device id or token of the device.
With this ID you can send notification from your server.
To manager a notification received
You can manage notifications received with the next method
To show a dynamic and floating notification, you have to add the following function into the function successNotificationReceived.
To activate or not gps position record
You can activate or deactivate gps position record.
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.
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.