我用手机的差距推插件正常工作对我来说,以及从我的服务器越来越味精。我有我想要两个问题,如果有一个人能更新code或在一段code的需要改变质量要求必须是AP preciated。
我跟着这个链接显示通知时,应用程序在后台ü可以看到它。
的http://devgirl.org/2012/10/25/tutorial-android-push-notifications-with-phonegap/
Regardinf这个文件我收到放,但两件事情没有doind正如我在我的两个问题提了。
我显示消息时,收到消息计数不确定的。
我想,如果我的应用程序在后台运行,所以状态栏拿出像其他的应用程序,通知。
的index.html
<!DOCTYPE HTML>< HTML和GT; < HEAD> <标题> com.PhoneGap.c2dm< /标题> < /头> <身体GT; <脚本类型=文/ JavaScript的字符集=utf-8SRC =科尔多瓦-2.2.0.js>< / SCRIPT> <脚本类型=文/ JavaScript的字符集=utf-8SRC =jquery_1.5.2.min.js>< / SCRIPT> <脚本类型=文/ JavaScript的字符集=utf-8SRC =JS / main.js>< / SCRIPT> <脚本类型=文/ JavaScript的字符集=utf-8SRC =JS / service.js>< / SCRIPT> <脚本类型=文/ JavaScript的SRC =PushNotification.js>< / SCRIPT> <脚本类型=文/ JavaScript的> VAR pushNotification; 功能onDeviceReady(){ $(#应用程序状态-UL)追加('<立GT; deviceready事件接收到的LT; /李>'); 尝试 { pushNotification = window.plugins.pushNotification; 如果(device.platform =='机器人'|| device.platform =='的Android'){ $(#应用程序状态-UL)追加('<立GT;注册Android< /李>'); pushNotification.register(successHandler,的ErrorHandler,{senderID:XXXXXXX,欧洲央行:onNotificationGCM}); //需要! }其他{ $(#应用程序状态-UL)追加('<立GT;注册iOS和LT; /李>'); pushNotification.register(tokenHandler,的ErrorHandler,{徽章:真正的,声音:真,警告:真,欧洲央行:onNotificationAPN}); //需要! } } 赶上(错误) { TXT =有此页面上的错误。\\ n \\ n; TXT + =错误描述:+ err.message +\\ n \\ n; 警报(TXT); } } //办理的iOS APNS通知 功能onNotificationAPN(五){ 如果(e.alert){ $(#应用程序状态-UL)追加('<立GT;推通知:'+ e.alert +'< /李>'); navigator.notification.alert(e.alert); } 如果(e.sound){ VAR SND =新媒体(e.sound); snd.play(); } 如果(e.badge){ pushNotification.setApplicationIconBadgeNumber(successHandler,e.badge); } } //手柄GCM通知为Android 功能onNotificationGCM(五){ $(#应用程序状态-UL)追加('<立GT;活动 - GT&; RECEIVED:'+ e.event +'< /李>'); 开关(e.event) { 情况下注册: 如果(e.regid.length大于0) { $(#应用程序状态-UL)追加('<立GT;注册 - > REGID:'+ e.regid +< /李>); //你的GCM推送服务器需要知道REGID,才可以推动这个装置 //这里是你可能想给它发送REGID供以后使用。 } 打破; 案消息: //如果设置此标志,这个通知发生,而我们是在前台。 //你可能想播放声音,以获得用户的关注,扔了一个对话框,等等。 如果(e.foreground) { $(#应用程序状态-UL)追加('<立GT; - INLINE NOTIFICATION--'+'< /李>'); //如果通知包含soundname,播放。 VAR my_media =新媒体(/ android_asset /网络/+ e.soundname); my_media.play(); } 其他 {//否则,我们推出了因为用户感动在通知栏中的通知。 如果(e.coldstart) $(#应用程序状态-UL)追加('<立GT; - COLDSTART NOTIFICATION--'+'< /李>'); 其他 $(#应用程序状态-UL)追加('<立GT; - 背景NOTIFICATION--'+'< /李>'); } $(#应用程序状态-UL)追加('<李>消息 - >味精:'+ e.payload.message +'< /李>'); $(#应用程序状态-UL)追加('<李>消息 - > MSGCNT:'+ e.payload.msgcnt +'< /李>'); 打破; 案错误: $(#应用程序状态-UL)追加('<立GT;错误 - >味精:'+ e.msg +'< /李>'); 打破; 默认: $(#应用程序状态-UL)追加('<李>活动 - >未知,收到一个事件,我们不知道它为< /李>'); 打破; } } 功能tokenHandler(结果){ $(#应用程序状态-UL)追加('<立GT;令牌:'+结果+'< /李>'); //你的iOS推送服务器需要知道令牌才可以推动这个装置 //这里是你可能想给它发送令牌以备后用。 } 功能successHandler(结果){ $(#应用程序状态-UL)追加('<立GT;成功:'+结果+'< /李>'); } 功能的ErrorHandler(错误){ $(#应用程序状态-UL)追加('<立GT;错误:'+误差+'< /李>'); } document.addEventListener(deviceready',onDeviceReady,真正的); < / SCRIPT> < DIV ID =家> < DIV ID =应用程序状态-DIV> < UL ID =应用程序状态-UL> <立GT;科尔多瓦PushNotification插件演示< /李> < / UL> <按钮ID =sendRegistrationID>保存注册ID< /按钮> <按钮ID =getRegistrationID>获得注册ID< /按钮> < / DIV> < / DIV> < /身体GT;< / HTML>
GCMINtentService.js
包com.plugin.gcm;进口的java.util.List;进口com.google.android.gcm.GCMBaseIntentService;进口org.json.JSONException;进口org.json.JSONObject;进口android.annotation.Sup pressLint;进口android.app.ActivityManager;进口android.app.ActivityManager.RunningTaskInfo;进口android.app.Notification;进口android.app.NotificationManager;进口android.app.PendingIntent;进口android.content.Context;进口android.content.Intent;进口android.os.Bundle;进口android.support.v4.app.NotificationCompat;进口android.util.Log;@燮pressLint(NewApi)公共类GCMIntentService扩展GCMBaseIntentService { 公共静态最终诠释NOTIFICATION_ID = 237; 私有静态最后弦乐TAG =GCMIntentService; 公共GCMIntentService(){ 超级(GCMIntentService); } @覆盖 公共无效onRegistered(上下文的背景下,字符串REGID){ Log.v(TAG,onRegistered:+ REGID); JSON的JSONObject; 尝试 { JSON =新的JSONObject()把(事件,注册)。 json.put(REGIDREGID); Log.v(TAGonRegistered:+ json.toString()); //发送JSON数据,上面的事件的JavaScript应用应设置为味精类型 //在这种情况下,这是注册ID PushPlugin.sendJavascript(JSON); } 赶上(JSONException E) { //不发送信息给用户,JSON失败 Log.e(TAG,onRegistered:JSON异常); } } @覆盖 公共无效onUnregistered(上下文的背景下,字符串REGID){ Log.d(TAGonUnregistered - REGID:+ REGID); } @燮pressWarnings(德precation) @覆盖 保护无效的onMessage(上下文的背景下,意图意图){ Log.d(TAG的onMessage - 背景:+背景); //从消息中提取有效载荷 捆绑额外= intent.getExtras(); 如果(临时演员!= NULL) { PushPlugin.sendExtras(临时演员); //发送通知,如果有消息,而不是在前台 如果(PushPlugin.isInForeground()及!&放大器;!extras.getString(消息),长度()= 0){ createNotification(背景下,临时演员); } } } 公共无效createNotification(上下文的背景下,捆绑演员) { NotificationManager mNotificationManager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 字符串的appName = getAppName(本); 意图notificationIntent =新意图(这一点,PushHandlerActivity.class); notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); notificationIntent.putExtra(pushBundle,临时演员); 的PendingIntent contentIntent = PendingIntent.getActivity(这一点,0,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder mBuilder = 新NotificationCompat.Builder(上下文) .setDefaults(Notification.DEFAULT_ALL) .setSmallIcon(context.getApplicationInfo()图标) .setWhen(System.currentTimeMillis的()) .setContentTitle(extras.getString(标题)) .setTicker(extras.getString(标题)) .setContentIntent(contentIntent); 字符串消息= extras.getString(信息); 如果(消息!= NULL){ mBuilder.setContentText(消息); }其他{ mBuilder.setContentText(<丢失邮件内容和GT;); } 串msgcnt = extras.getString(msgcnt); 如果(msgcnt!= NULL){ mBuilder.setNumber(的Integer.parseInt(msgcnt)); } mNotificationManager.notify((字符串)的appName,NOTIFICATION_ID,mBuilder.build()); } 公共静态无效cancelNotification(上下文的背景下) { NotificationManager mNotificationManager =(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.cancel((字符串)getAppName(上下文),NOTIFICATION_ID); } 私人静态字符串getAppName(上下文的背景下) { CharSequence中的appName = 背景 .getPackageManager() .getApplicationLabel(context.getApplicationInfo()); 返回(字符串)的appName; } @覆盖 公共无效onerror的(上下文的背景下,字符串ErrorID中){ Log.e(TAG的onError - ErrorID中:+ ErrorID中); }}
解决方案
本教程是最好的状态栏通知。
只是注释掉
//如果(!flag.isEmpty()){// flagVal =的Integer.parseInt(标志);//}
在 StatusBarNotification.java
新科尔多瓦库版本。
i used phone gap push plugin it works fine for me, as well as getting msg from my server. i have two problems that i want if some one can updated code or where piece of code need to change requirments must be appreciated.
i followed this link showing notification when app is in background u can see it .
http://devgirl.org/2012/10/25/tutorial-android-push-notifications-with-phonegap/
Regardinf this document i recieved put but two things not doind as i mention in my two problems.
i am showing message count undefined when msg recieved.
i want that if my app is running in background so status bar come up like other app that for notification.
index.html
<!DOCTYPE HTML>
<html>
<head>
<title>com.PhoneGap.c2dm</title>
</head>
<body>
<script type="text/javascript" charset="utf-8" src="cordova-2.2.0.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery_1.5.2.min.js"></script>
<script type="text/javascript" charset="utf-8" src="js/main.js"></script>
<script type="text/javascript" charset="utf-8" src="js/service.js"></script>
<script type="text/javascript" src="PushNotification.js"></script>
<script type="text/javascript">
var pushNotification;
function onDeviceReady() {
$("#app-status-ul").append('<li>deviceready event received</li>');
try
{
pushNotification = window.plugins.pushNotification;
if (device.platform == 'android' || device.platform == 'Android') {
$("#app-status-ul").append('<li>registering android</li>');
pushNotification.register(successHandler, errorHandler, {"senderID":"XXXXXXX","ecb":"onNotificationGCM"}); // required!
} else {
$("#app-status-ul").append('<li>registering iOS</li>');
pushNotification.register(tokenHandler, errorHandler, {"badge":"true","sound":"true","alert":"true","ecb":"onNotificationAPN"}); // required!
}
}
catch(err)
{
txt="There was an error on this page.\n\n";
txt+="Error description: " + err.message + "\n\n";
alert(txt);
}
}
// handle APNS notifications for iOS
function onNotificationAPN(e) {
if (e.alert) {
$("#app-status-ul").append('<li>push-notification: ' + e.alert + '</li>');
navigator.notification.alert(e.alert);
}
if (e.sound) {
var snd = new Media(e.sound);
snd.play();
}
if (e.badge) {
pushNotification.setApplicationIconBadgeNumber(successHandler, e.badge);
}
}
// handle GCM notifications for Android
function onNotificationGCM(e) {
$("#app-status-ul").append('<li>EVENT -> RECEIVED:' + e.event + '</li>');
switch( e.event )
{
case 'registered':
if ( e.regid.length > 0 )
{
$("#app-status-ul").append('<li>REGISTERED -> REGID:' + e.regid + "</li>");
// Your GCM push server needs to know the regID before it can push to this device
// here is where you might want to send it the regID for later use.
}
break;
case 'message':
// if this flag is set, this notification happened while we were in the foreground.
// you might want to play a sound to get the user's attention, throw up a dialog, etc.
if (e.foreground)
{
$("#app-status-ul").append('<li>--INLINE NOTIFICATION--' + '</li>');
// if the notification contains a soundname, play it.
var my_media = new Media("/android_asset/www/"+e.soundname);
my_media.play();
}
else
{ // otherwise we were launched because the user touched a notification in the notification tray.
if (e.coldstart)
$("#app-status-ul").append('<li>--COLDSTART NOTIFICATION--' + '</li>');
else
$("#app-status-ul").append('<li>--BACKGROUND NOTIFICATION--' + '</li>');
}
$("#app-status-ul").append('<li>MESSAGE -> MSG: ' + e.payload.message + '</li>');
$("#app-status-ul").append('<li>MESSAGE -> MSGCNT: ' + e.payload.msgcnt + '</li>');
break;
case 'error':
$("#app-status-ul").append('<li>ERROR -> MSG:' + e.msg + '</li>');
break;
default:
$("#app-status-ul").append('<li>EVENT -> Unknown, an event was received and we do not know what it is</li>');
break;
}
}
function tokenHandler (result) {
$("#app-status-ul").append('<li>token: '+ result +'</li>');
// Your iOS push server needs to know the token before it can push to this device
// here is where you might want to send it the token for later use.
}
function successHandler (result) {
$("#app-status-ul").append('<li>success:'+ result +'</li>');
}
function errorHandler (error) {
$("#app-status-ul").append('<li>error:'+ error +'</li>');
}
document.addEventListener('deviceready', onDeviceReady, true);
</script>
<div id="home">
<div id="app-status-div">
<ul id="app-status-ul">
<li>Cordova PushNotification Plugin Demo</li>
</ul>
<button id="sendRegistrationID">Save Registration ID</button>
<button id="getRegistrationID">Get Registration ID</button>
</div>
</div>
</body>
</html>
GCMINtentService.js
package com.plugin.gcm;
import java.util.List;
import com.google.android.gcm.GCMBaseIntentService;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
@SuppressLint("NewApi")
public class GCMIntentService extends GCMBaseIntentService {
public static final int NOTIFICATION_ID = 237;
private static final String TAG = "GCMIntentService";
public GCMIntentService() {
super("GCMIntentService");
}
@Override
public void onRegistered(Context context, String regId) {
Log.v(TAG, "onRegistered: "+ regId);
JSONObject json;
try
{
json = new JSONObject().put("event", "registered");
json.put("regid", regId);
Log.v(TAG, "onRegistered: " + json.toString());
// Send this JSON data to the JavaScript application above EVENT should be set to the msg type
// In this case this is the registration ID
PushPlugin.sendJavascript( json );
}
catch( JSONException e)
{
// No message to the user is sent, JSON failed
Log.e(TAG, "onRegistered: JSON exception");
}
}
@Override
public void onUnregistered(Context context, String regId) {
Log.d(TAG, "onUnregistered - regId: " + regId);
}
@SuppressWarnings("deprecation")
@Override
protected void onMessage(Context context, Intent intent) {
Log.d(TAG, "onMessage - context: " + context);
// Extract the payload from the message
Bundle extras = intent.getExtras();
if (extras != null)
{
PushPlugin.sendExtras(extras);
// Send a notification if there is a message and not in foreground
if (!PushPlugin.isInForeground() && extras.getString("message").length() != 0) {
createNotification(context, extras);
}
}
}
public void createNotification(Context context, Bundle extras)
{
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String appName = getAppName(this);
Intent notificationIntent = new Intent(this, PushHandlerActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.putExtra("pushBundle", extras);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(context.getApplicationInfo().icon)
.setWhen(System.currentTimeMillis())
.setContentTitle(extras.getString("title"))
.setTicker(extras.getString("title"))
.setContentIntent(contentIntent);
String message = extras.getString("message");
if (message != null) {
mBuilder.setContentText(message);
} else {
mBuilder.setContentText("<missing message content>");
}
String msgcnt = extras.getString("msgcnt");
if (msgcnt != null) {
mBuilder.setNumber(Integer.parseInt(msgcnt));
}
mNotificationManager.notify((String) appName, NOTIFICATION_ID, mBuilder.build());
}
public static void cancelNotification(Context context)
{
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel((String)getAppName(context), NOTIFICATION_ID);
}
private static String getAppName(Context context)
{
CharSequence appName =
context
.getPackageManager()
.getApplicationLabel(context.getApplicationInfo());
return (String)appName;
}
@Override
public void onError(Context context, String errorId) {
Log.e(TAG, "onError - errorId: " + errorId);
}
}
解决方案
This tutorial is the best for status bar notification.
Just comment out the
// if (!flag.isEmpty()){
// flagVal = Integer.parseInt(flag);
//}
in StatusBarNotification.java
for new cordova lib version.