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"?><pluginxmlns= "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> <enginename= "Cordova"version= ">=3.0.0" /> </Engines> <Js-modulesrc= "Www/notifysrv.js"name= "Notifysrv"> <clobbersTarget= "Notify" /> </Js-module> <Platformname= "Android"> <Source-filesrc= "Src/android/notifysrvplugin.java"Target-dir= "Src/com/elon/cordova/plugin" /> <Config-fileTarget= "Res/xml/config.xml"Parent="/*"> <featurename= "Notifysrvplugin"> <paramname= "Android-package"value= "Com.elon.cordova.plugin.NotifysrvPlugin"/> </feature> </Config-file> <Config-fileTarget= "Androidmanifest.xml"Parent="/*"> <uses-permissionAndroid:name= "Android.permission.VIBRATE" /> </Config-file> </Platform></plugin>
Notifysrvplugin.java
PackageCom.elon.cordova.plugin;ImportOrg.apache.cordova.CordovaPlugin;ImportOrg.apache.cordova.CallbackContext;ImportOrg.apache.cordova.CordovaWebView;ImportOrg.apache.cordova.CordovaInterface;Importandroid.app.Notification;ImportAndroid.app.NotificationManager;Importandroid.app.PendingIntent;ImportOrg.json.JSONArray;Importorg.json.JSONException;ImportOrg.json.JSONObject;ImportAndroid.content.Context;ImportAndroid.support.v4.app.NotificationCompat; Public classNotifysrvpluginextendsCordovaplugin { Public Static FinalString TAG = "Notifysrvplugin"; Public Static FinalString iconname = "icon";//Icon Res name PublicNotificationmanager nm; PublicContext M_context; Public voidInitialize (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 BooleanExecute (String action, Jsonarray args, Callbackcontext callbackcontext)throwsjsonexception {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); intIconresid = M_context.getresources (). Getidentifier (Iconname, "drawable", M_context.getpackagename ()); Notification Notification=NewNotificationcompat.builder (M_context). Setcontenttitle (title). Setcontenttext (text) . SetDefaults (Notification.default_all)//Set default ringtone, vibrate etc. setsmallicon (iconresid). Setcontentintent (M_pen dingintent). Setautocancel (true) //. Setlargeicon (Abitmap). Build (); Nm.notify (1, notification); Callbackcontext.success (); return true; } return false; }}
Notifysrv.js
varArgscheck = require (' Cordova/argscheck '));varexec = require (' cordova/exec ');varNotify =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"); ElseEXEC (Success, error,' Notifysrvplugin ', ' send ', message);};varnotify =NewNotify (); 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) { | | "Failure"); });
Cordova Android notify message notification plugin