cordova的android notify消息通知插件
最近在学习用CORDOVA(PHONEGAP)结合SENCHA TOUCH开发应用,想实现一个安卓下的消息通知功能,这个可以通过CORDOVA的插件来实现。
插件目录结构如下:
notifyplugin
- plugin.xml
- www/notifysrv.js
- src/android/NotifysrvPlugin.java
- libs/android-support-v4.jar
先编写plugin.xml

<?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 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.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;

将插件加入cordova工程的办法
进入CMD,进入cordova工程文件夹,然后输入如下命令
cordova plugin add [插件目录]
使用本插件的方法:

var msg = ["新消息标题","新的消息内容"];
Notify.send(msg,function(){
console.log("成功");
},function(msg){
console.log(msg || "失败");
});

cordova的android notify消息通知插件的更多相关文章
- Android Notification 消息通知 相关资料.md
目录 Android Notification 消息通知 相关资料 Android 5.0 Lollipop (API 21)无法正常显示通知图标,只能看到一个白色方块或灰色方块的问题 解决方案 参考 ...
- PNotify – 简单易用的 JS 通知,消息提示插件
PNotify 是一个 JavaScript 通知插件,前身为 Pines Notify.它旨在提供无与伦比的灵活性,同时很容易使用.它可以提供无阻塞的通知,允许用户无需关闭通知或者提示信息就可以点击 ...
- Android中的消息通知(NotificationManager和Notification)
下面来谈谈notification,这个notification一般用在电话,短 信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个通知,这时手从上方滑动状态栏就可以展开并处理这 ...
- Android消息通知(notification)和PendingIntent传值
通知栏的自定义布局:转:http://blog.csdn.net/vipzjyno1/article/details/25248021 拓展 实现自定义的通知栏效果: 这里要用到RemoteViews ...
- Android消息通知-Notification
Android中常用的消息提醒,一种是Toast弹出提醒内容,一种是AlterDialog弹出框来提醒用户,还有一种就是消息通知的,用Android经常收到各种通知就是Notifation.Notif ...
- Android学习系列(7)--App消息通知机制
有人说,程序员很安静,但我不完全同意,程序员的聒噪,是藏在代码后面,是藏在程序后面.这篇文章是Android开发人员的必备知识,是我特别为大家整理和总结的,不求完美,但是有用. 1.消息推送机制 ...
- Android移动APP开发笔记——Cordova(PhoneGap)通过CordovaPlugin插件调用 Activity 实例
引言 Cordova(PhoneGap)采用的是HTML5+JavaScript混合模式来开发移动手机APP,因此当页面需要获取手机内部某些信息时(例如:联系人信息,坐标定位,短信等),程序就需要调用 ...
- jenkins 构建后发送钉钉消息通知(插件)
钉钉,越来越多的公司采用,那么我们在持续集成中,也可以直接选择钉钉插件的,在之前的博客中 ,对发送的钉钉消息进行了定制,那样的话会开启一个新的任务, 其实今天呢,我们可以直接安装一个插件就可以发送了, ...
- 强大的响应式jQuery消息通知框和信息提示框插件
lobibox是一款功能很强大的jQuery消息通知框和信息提示框插件.这个插件分为两个部分:消息通知框和信息提示框.它能很好的结合Bootstrap使用. 信息提示框 lobibox的信息提示框能够 ...
随机推荐
- HLG1744组合数学问题与lucas定理运用
The figure below shows Pascal's Triangle: Baby H divides Pascal's Triangle into some Diagonals, like ...
- Linux Haproxy 安装和部署
一.Haproxy 安装 下载地址 http://pan.baidu.com/s/1mggViXE cd /usr/local tar xzvf haproxy-.tar.gz cd haproxy- ...
- 一些Linux的路径
系统引导时启动 /etc/rc.d/rc.local
- Google Code Jam 2014 Round 1B Problem B
二进制数位DP,涉及到数字的按位与操作. 查看官方解题报告 #include <cstdio> #include <cstdlib> #include <cstring& ...
- poj1166
爆搜就可以过,不过我用了迭代加深. 注意每个操作最多进行4次 #include <cstdio> #include <cstdlib> using namespace std; ...
- NSUrlConnection 和 NSUrlRequest 的关系
开始看到这2个名字,总感觉NSUrlConnection才是主要的网络请求类,其实不是,先看官方文档 An NSURLConnection object lets you load the conte ...
- Android Your content must have a ListView whose id attribute is 'android.R.id.list'错误的解决办法
在Android开发中,ListView有着很重要的地位,使用的场合也非常的多 错误提示:Your content must have a ListView whose id attribute is ...
- Gym 100851E Easy Problemset (模拟题)
Problem E. Easy ProblemsetInput file: easy.in Output file: easy.outPerhaps one of the hardest problems ...
- JSTL分类查询
index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UT ...
- c# 正则表达式 匹配回车
1 "." 匹配除 "\n" 之外的任何单个字符,一般用".*?"匹配不包括回车的任意字符. 2 我们在用正则表达式分析html或者是xml ...