Android 实现通知栏显示多个notification

1、notifcation要显示多条,那么NotificationManager.notify( id, notify);  中的要保持不一样;

如果id值一样,那么就把之前的notify覆盖更新为当期最新的;

2、点击notification 进入到activity, 使用到pendingIntent类方法,PengdingIntent.getActivity()的第二个参数,即请求参数,

实际上是通过该参数来区别不同的Intent的,如果id相同,就会覆盖掉之前的Intent了,

 

只要每个不同的Intent对应传递一个独立的ID就可以,实现每个Notification点击进入对应的activity中:

Java代码  
  1. private Notification genreNotification(Context context, int icon, String tickerText, String title, String content, Intent intent, int id){  
  2.         Notification notification = new Notification(icon, tickerText, System.currentTimeMillis());  
  3.         // 问题就在这里的id了  
  4.         PendingIntent pendIntent = PendingIntent.getActivity(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);  
  5. // 使用了原始布局
  6.         notification.setLatestEventInfo(context, title, content, pendIntent);  
  7.         notification.flags |= Notification.FLAG_AUTO_CANCEL;  
  8.         return notification;  
  9.     }  
  10.   
  11. ...  
  12. mNotificationManager.notify(ID_1,   
  13.                     genreNotification(mContext, ICON_RES,   
  14.                             notifyText1, notifyTitle1, notifyText1, intent_1, ID_1));  
  15. ...  
  16. mNotificationManager.notify(ID_2,   
  17.                     genreNotification(mContext, ICON_RES,   
  18.                             notifyText2, notifyTitle2, notifyText2, intent_2, ID_2));  
  19.   
  20. ...  
  21. mNotificationManager.notify(ID_3,   
  22.                     genreNotification(mContext, ICON_RES,   
  23.                             notifyText3, notifyTitle3, notifyText3, intent_3, ID_3));  

3、notification使用自定义布局:
notification = new Notification(R.drawable.app_icon, content,
System.currentTimeMillis());
notification.defaults = Notification.DEFAULT_ALL; // 使用默认设置,比如铃声、震动、闪灯
notification.flags = Notification.FLAG_AUTO_CANCEL; // 但用户点击消息后,消息自动在通知栏自动消失
notification.flags |= Notification.FLAG_NO_CLEAR;// 点击通知栏的删除,消息不会依然不会被删除


Intent intent = new Intent(ctx, ContentNotifyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);

intent.putExtra("content", content);
intent.putExtra("number", title);
intent.putExtra("date", date);
intent.putExtra("title", title);
RemoteViews remoteView = PushApplication.getInstance()
.getRemoteViewsView();
remoteView.setImageViewResource(R.id.notification_icon,
R.drawable.app_icon);
//标题
remoteView.setTextViewText(R.id.notification_title, title);
//内容
remoteView.setTextViewText(R.id.notification_content, content);
// 设置notification的contentView为 remoteView
notification.contentView = remoteView;


pi = PendingIntent.getActivity(ctx, NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);

// 原始样式
// notification.setLatestEventInfo(ctx, title, content, pi);


notification.contentIntent = pi;

// 将消息推送到状态栏
manager.notify(NOTIFICATION_ID++, notification);

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值