我正在开发应用程序,因为我必须接收来自GCM的通知,我已成功收到通知但它只显示最近的通知但我想在通知栏中显示所有通知,每当它打开时我都会在通知上消失吧这些都不适合我,请帮帮我。
我的代码在下面给出
@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);
}
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).setNumber(++numMessages);
// 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);
}
答案 0 :(得分:2)
尝试以下
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);