Recently in the study with Cordova (PHONEGAP) combined with Sencha touch to develop the application, want to implement an Android message notification function, which can be achieved through the Cordova plug-in.
The plugin directory structure is as follows:
Notifyplugin
- Plugin.xml
- Www/notifysrv.js
- Src/android/notifysrvplugin.java
- Libs/android-support-v4.jar
Write Plugin.xml First
<?xml version= "1.0" encoding= "UTF-8"? ><plugin xmlns= "http://apache.org/cordova/ns/plugins/1.0" id= " Com.elon.cordova.plugin "version=" 0.0.1 "> <name>NotifysrvPlugin</name> <description> Notifysrvplugin description</description> <author>elon</author> <license>apache 2.0 License </license> <engines> <engine name= "Cordova" version= ">=3.0.0"/> </engines> < ; Js-module src= "Www/notifysrv.js" name= "Notifysrv" > <clobbers target= "Notify"/> </js-module> <platform name= "Android" > <source-file src= "Src/android/notifysrvplugin.java" target-dir= "src/com/ Elon/cordova/plugin "/> <config-file target=" res/xml/config.xml "parent="/* "> <feature name= "Notifysrvplugin" > <param name= "android-package" value= "Com.elon.cordova.plugin.NotifysrvPlugin"/> </feature> </config-file> <config-file target= "Androidmanifest.xml" parent= "/*" > <uses-permission android:name= "Android.permission.VIBRATE"/> </config-file> </platform></plugin>
Notifysrvplugin.java
Package Com.elon.cordova.plugin;import Org.apache.cordova.cordovaplugin;import Org.apache.cordova.CallbackContext ; Import Org.apache.cordova.cordovawebview;import Org.apache.cordova.cordovainterface;import Android.app.notification;import Android.app.notificationmanager;import Android.app.pendingintent;import Org.json.jsonarray;import Org.json.jsonexception;import Org.json.jsonobject;import Android.content.Context;import Android.support.v4.app.notificationcompat;public class Notifysrvplugin extends Cordovaplugin {public static final Stri ng TAG = "Notifysrvplugin"; public static final String iconname = "icon";//icon res name public notificationmanager nm; Public Context M_context; public void Initialize (Cordovainterface Cordova, Cordovawebview WebView) {super.initialize (Cordova, WebView); M_context = This.cordova.getActivity (). Getapplicationcontext (); NM = (Notificationmanager) m_context.getsystemservice (Android.content.Context.NOTIFICATION_SERVICE); } @Override Public Boolean execute (String action, Jsonarray args, Callbackcontext callbackcontext) throws Jsonex ception {if ("Send". Equals (Action)) {String title = args.getstring (0); String Text = args.getstring (1); Pendingintent m_pendingintent=pendingintent.getactivity (this.cordova.getActivity (), 0, This.cordova. Getactivity (). Getintent (), 0); int iconresid = M_context.getresources (). Getidentifier (Iconname, "drawable", M_context.getpackagename ()); Notification Notification = new Notificationcompat.builder (m_context). Setcontenttitle (title). Setco Ntenttext (text). SetDefaults (Notification.default_all)//Set default ringtone, vibrate etc. setsmallicon (ICONRESID) . Setcontentintent (M_pendingintent). Setautocancel (True)//. Setlargeicon (Abitmap). Bui LD (); Nm.notify (1, notification); CallbacKcontext.success (); return true; } return false; }}
Notifysrv.js
var Argscheck = require (' Cordova/argscheck '); var exec = require (' cordova/exec '); var Notify = function () {}; Notify.prototype.send = function (message, success, error) { //argscheck.checkargs (' AFF ', ' notify.send ', arguments ); Console.log ("Send notification[" +message[1]+ "]"); if (!message) error && error ("Please input message"); else EXEC (success, error, ' Notifysrvplugin ', ' send ', message);}; var notify = new Notify (); module.exports = notify;
Ways to add plugins to the Cordova project
Enter CMD, enter the Cordova project folder, and enter the following command
Cordova plugin Add [plugin directory]
How to use this plugin:
var msg = ["New Message title", "New message Content"]; Notify.send (Msg,function () { Console.log ("Success"); },function (msg) { console.log (msg | | "Failure"); });
Cordova Android notify message notification plugin