这个是系统推送。。。
就是你客户端输什么。。。手机推一个通知栏出来这样
就是你客户端输什么。。。手机推一个通知栏出来这样
花了不少的时间研究phonegap,希望能够找出一个能够取代轮询的推送方式,尝试了pushwoosh之后发现有两个问题,一个是手机必须集成有andriod apis的组件,其次不知道是不是什么原因总是有account失败的错误,实在是没有办法,想想即便解决了,第一个问题依然难以解决。
后来在江哥的推荐下找到了一个系统推送的phonegap插件
好了,首先我们来介绍一下用法:
1.(两个最关键的文件(.java和.js组件))
/* * * Copyright (C) 2011 Dmitry Savchenko <dg.freak@gmail.com> * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * */ package com.tricedesigns; import org.apache.cordova.api.PluginResult.Status; import org.json.JSONArray; import org.json.JSONException; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.util.Log; import com.phonegap.api.Plugin; import com.phonegap.api.PluginResult; public class StatusBarNotification extends Plugin { // Action to execute public static final String NOTIFY = "notify"; public static final String CLEAR = "clear"; /** * Executes the request and returns PluginResult * * @param action Action to execute * @param data JSONArray of arguments to the plugin * @param callbackId The callback id used when calling back into JavaScript * * @return A PluginRequest object with a status * */ @Override public PluginResult execute(String action, JSONArray data, String callbackId) { String ns = Context.NOTIFICATION_SERVICE; mNotificationManager = (NotificationManager) ctx.getSystemService(ns); context = ctx.getApplicationContext(); PluginResult result = null; if (NOTIFY.equals(action)) { try { String title = data.getString(0); String body = data.getString(1); Log.d("NotificationPlugin", "Notification: " + title + ", " + body); showNotification(title, body); result = new PluginResult(Status.OK); } catch (JSONException jsonEx) { Log.d("NotificationPlugin", "Got JSON Exception " + jsonEx.getMessage()); result = new PluginResult(Status.JSON_EXCEPTION); } } else if (CLEAR.equals(action)){ clearNotification(); } else { result = new PluginResult(Status.INVALID_ACTION); Log.d("NotificationPlugin", "Invalid action : "+action+" passed"); } return result; } /** * Displays status bar notification * * @param contentTitle Notification title * @param contentText Notification text * */ public void showNotification( CharSequence contentTitle, CharSequence contentText ) { int icon = R.drawable.nofication; long when = System.currentTimeMillis(); Notification notification = new Notification(icon, contentTitle, when); //notification.flags |= Notification.FLAG_NO_CLEAR; //Notification cannot be clearned by user Intent notificationIntent = new Intent((Context) ctx, ctx.getClass()); PendingIntent contentIntent = PendingIntent.getActivity((Context) ctx, 0, notificationIntent, 0); notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); mNotificationManager.notify(1, notification); } /** * Removes the Notification from status bar */ public void clearNotification() { mNotificationManager.cancelAll(); } private NotificationManager mNotificationManager; private Context context; }
.js文件
/* * * Copyright (C) 2011 Dmitry Savchenko <dg.freak@gmail.com> * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * */ /** * * Constructor */ var NotificationMessenger = { notify:function(title, body) { return cordova.exec(null, null, 'StatusBarNotification','notify', [title, body]); }, clear:function() { return cordova.exec(null, null, 'StatusBarNotification', 'clear', []); }, }
3.记得放入一张notification.png的图片在res下的drawable文件夹下
4.修改plugin.xml(添加一条记录)
<plugin name="StatusBarNotification" value="com.tricedesigns.StatusBarNotification"/>
5.定义调用的函数
function aaa(){ NotificationMessenger.notify("CF西西", "Phonegap Andriod 交流 QQ 群 250395324欢迎您的加入"); }
6.ok,找个按钮触发这个aaa函数,然后运行一下你会看到如下效果(项目代码可去我的qq群共享下载哦250395324)