gou*_*n93 10 notifications android notificationmanager
我在我的应用上使用推送通知.我曾经在推送通知到来时显示通知,如果我发送另一个通知(不清除先前的通知),它会替换新通知.
这是我使用的代码
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.ic_launcher;
CharSequence tickerText = "New notification Pending";
long time = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, time);
notification.flags = Notification.DEFAULT_LIGHTS
| Notification.FLAG_AUTO_CANCEL;
// Context context = getApplicationContext();
CharSequence contentTitle = "Notifications";
CharSequence contentText = newMessage;
Intent notificationIntent = new Intent(this, LoginActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText,
contentIntent);
mNotificationManager.notify(1, notification);
Run Code Online (Sandbox Code Playgroud)
但我不想替换通知,我想将其添加为新通知.
Amr*_*ngh 21
您还可以使用System.currentTimeMillis()为通知分配唯一ID.
int id = (int) System.currentTimeMillis();
mNotificationManager.notify(id, notification);
Run Code Online (Sandbox Code Playgroud)
您每次都需要提供不同的ID作为通知ID.最好的方法是将一个ID字段发送到GCM,然后可以通过Intent.getExtras().getInt()GCMIntentService的onMessage()方法访问该字段.
如果这是不可能的,我建议使用类似的(int)Math.random()*10方法生成随机整数作为通知ID.这将(部分)确保您的通知不会互相替换.
| 归档时间: |
|
| 查看次数: |
7113 次 |
| 最近记录: |