我正在开发一个应用程序,我必须从gcm服务器接收通知我已成功收到通知,这里有两个问题。
1)我成功阅读了多个通知,但所有消息通知只显示一条消息,仅显示最后一个数据。
2)每当打开通知时,通知栏上的Notication图标都不会消失。我写了删除通知的代码,它不起作用请帮帮我。
public class GcmIntentService extends IntentService{
Context context;
public static int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public static final String TAG = "GCM Demo";
public GcmIntentService() {
super("GcmIntentService");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
Bundle extras = intent.getExtras();
String msg = intent.getStringExtra("message");
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.
MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_DELETED.equals(messageType)) {
sendNotification("Deleted messages on server: " +
extras.toString());
// If it's a regular GCM message, do some work.
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_MESSAGE.equals(messageType)) {
// This loop represents the service doing some work.
for (int i=0; i<5; i++) {
Log.i(TAG, "Working... " + (i+1)
+ "/5 @ " + SystemClock.elapsedRealtime());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
// Post notification of received message.
//sendNotification("Received: " + extras.toString());
sendNotification(msg);
Log.i(TAG, "Received: " + extras.toString());
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent myintent = new Intent(this, ReceiveActivity.class);
myintent.putExtra("message", msg);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
myintent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_gcm)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
/* NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();
String[] events = new String[6];
// Sets a title for the Inbox style big view
inboxStyle.setBigContentTitle("Event tracker details:");
int numMessages = 0;
// Moves events into the big view
for (int i=0; i < events.length; i++) {
inboxStyle.addLine(events[i]);
}
mBuilder.setStyle(inboxStyle);
*/
AudioManager am = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
/* Even if the mode is set to "Sound & Vibration" in the phone,
* the status code that getRingerMode() returns is RINGER_MODE_NORMAL.
*/
switch (am.getRingerMode())
{
case AudioManager.RINGER_MODE_VIBRATE:
mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
break;
case AudioManager.RINGER_MODE_NORMAL:
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
break;
default:
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
}
mBuilder.setContentIntent(contentIntent);
// TO clear notification
mNotificationManager.notify(NOTIFICATION_ID++, mBuilder.build());
mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_stat_gcm)
.setAutoCancel(true)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mNotificationManager.cancel(NOTIFICATION_ID);
}
}
我的接收活动
package com.technowellServices.locationfind;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;
public class ReceiveActivity extends Activity {
TextView name;
TextView deal;
TextView valid;
TextView address;
JSONObject json;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receive);
Intent intent = getIntent();
name = (TextView) findViewById(R.id.name);
deal = (TextView) findViewById(R.id.deal);
valid = (TextView) findViewById(R.id.valid);
address = (TextView)findViewById(R.id.address);
String message = intent.getExtras().getString("message");
try {
json = new JSONObject(message);
String stime = json.getString("name");
name.setText(stime);
String slecturename = json.getString("deal");
deal.setText(slecturename);
String sroom = json.getString("valid");
valid.setText(sroom);
String sfaculty = json.getString("address");
address.setText(sfaculty);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
请添加此方法并在此方法中传递您的通知ID。 在您的主要活动的resume方法中调用此方法..
public void CancelNotification(Context ctx, int notifyId) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) ctx
.getSystemService(ns);
nMgr.cancel(notifyId);
}
我希望它对你有用。
答案 1 :(得分:0)
请检查此代码:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Event tracker")
.setContentText("Events received")
NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();
String[] events = new String[6];
// Sets a title for the Inbox style big view
inboxStyle.setBigContentTitle("Event tracker details:");
...
// Moves events into the big view
for (int i=0; i < events.length; i++) {
inboxStyle.addLine(events[i]);
}
// Moves the big view style object into the notification object.
mBuilder.setStyle(inBoxStyle);
...
// Issue the notification here.
请检查
我希望它对你有所帮助。
答案 2 :(得分:0)
这是不正确的:
mNotificationManager.notify(NOTIFICATION_ID++, mBuilder.build());
mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_stat_gcm)
.setAutoCancel(true)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mNotificationManager.cancel(NOTIFICATION_ID);
您无需为取消通知创建其他通知构建器。您只需在调用mBuilder.setAutoCancel(true)
之前为原始构建器调用mNotificationManager.notify
。
因此,您应该用以下代码替换该代码:
mBuilder.setAutoCancel(true)
mNotificationManager.notify(NOTIFICATION_ID++, mBuilder.build());