最近在學習用CORDOVA(PHONEGAP)結合SENCHA TOUCH開發應用,想實現一個安卓下的消息通知功能,這個能夠經過CORDOVA的插件來實現。java
插件目錄結構以下:android
notifypluginweb
先編寫plugin.xmlapache
<?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.javajson
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 String 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 JSONException { 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) .setContentText(text) .setDefaults(Notification.DEFAULT_ALL) //設置默認鈴聲,震動等 .setSmallIcon(iconResID) .setContentIntent(m_PendingIntent) .setAutoCancel(true) // .setLargeIcon(aBitmap) .build(); nm.notify(1, notification); callbackContext.success(); return true; } return false; } }
notifysrv.jsapp
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;
將插件加入cordova工程的辦法ide
進入CMD,進入cordova工程文件夾,而後輸入以下命令學習
cordova plugin add [插件目錄]ui
使用本插件的方法:this
var msg = ["新消息標題","新的消息內容"]; Notify.send(msg,function(){ console.log("成功"); },function(msg){ console.log(msg || "失敗"); });