Cordova's android notify message and notification plug-in, cordovaandroid

Source: Internet
Author: User

Cordova's android notify message and notification plug-in, cordovaandroid

Recently, I learned how to use CORDOVA (PHONEGAP) in combination with SENCHA TOUCH to develop an application. To implement the Message notification function in Android, this function can be implemented through the CORDOVA plug-in.

The directory structure of the plug-in is as follows:

Notifyplugin

  • Plugin. xml
  • Www/notifysrv. js
  • Src/android/NotifysrvPlugin. java
  • Libs/android-support-v4.jar

Compile 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. icationicationmanager; 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 = "yysrvplugin"; public static final String iconname = "icon"; // icon res name public icationicationmanager nm; public Context m_context; public void initialize (CordovaInterface cordova, CordovaWebView webView) {super. initialize (cordova, webView); m_context = this. cordova. getActivity (). getApplicationContext (); nm = (icationicationmanager) 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 icationicationcompat. builder (m_context ). setContentTitle (title ). setContentText (text ). setDefaults (Notification. DEFAULT_ALL) // set the default ringtone, vibrate, and so on. setSmallIcon (iconResID ). setContentIntent (m_PendingIntent ). setAutoCancel (true )//. setLargeIcon (aBitmap ). build (); nm. Y (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;

How to add plug-ins to the cordova Project

Go to CMD, enter the cordova project folder, and enter the following command

Cordova plugin add [plug-in Directory]

How to use this plug-in:

Var msg = ["New Message Title", "New message content"]; Optional Y. send (msg, function () {console. log ("succeeded") ;}, function (msg) {console. log (msg | "failed ");});

 


Steps for status bar notification in android

Private icationicationmanager = (NotificationManager) getSystemService (NOTIFICATION_SERVICE); // This attribute will be used later to wake up notifications

Directly write a method for you, which is the step and the parameter meaning:
TickerText text when a notification is prompted
ContentTitle: The title displayed when you drag the status bar
ContentText: The content displayed when you drag the status bar
Id is an identifier and may contain multiple notifications.
ResId image resource to notify the previous image
Private void showNotification (String tickerText, String contentTitle,
String contentText, int id, int resId ){
// Create a notification object. The image resource is in front of it, and the following parameter is the current time, indicating that the notification will be received immediately.
Notification notification = new Notification (resId,
TickerText, System. currentTimeMillis ());
// PendingIntent indicates the content executed by intent after you click the notification.
PendingIntent contentIntent = PendingIntent. getActivity (this, 0,
GetIntent (), 0 );
// Notification display content
Notification. setLatestEventInfo (this, contentTitle, contentText,
ContentIntent );

// Notification. defaults = defaults; it is set to vibrate, except for vibrate, but must be called before wakeup.

// Wake up
Icationicationmanager. notify (id, notification );

}

Do not ask!

How to display multiple Android notifications

Void android. app. icationicationmanager. Y (int id, Notification notification) Id should not be set to the same

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.