android 同时发送几条通知

简介:  android 同时发送几条通知 ======= 下面是转载的文章。  同时发送几条通知把ID添加,接收的时候找到这个id就可以出来多条了。 还是不太明白或者编码实现不了的可以加我QQ。
 android 同时发送几条通知

=======

下面是转载的文章。

 同时发送几条通知把ID添加,接收的时候找到这个id就可以出来多条了。

还是不太明白或者编码实现不了的可以加我QQ。

博客很少上了。

========

注意通知中PendingIntent.getActivity(Context context, int requestCode,Intent intent, int flags)这几个参数:

context The Context in which this PendingIntent should start the activity.
requestCode    Private request code for the sender (currently not used).
intents Array of Intents of the activities to be launched.
flags May be FLAG_ONE_SHOTFLAG_NO_CREATEFLAG_CANCEL_CURRENTFLAG_UPDATE_CURRENT, or any of the flags as supported by Intent.fillIn() to control which unspecified parts of the intent that can be supplied when the actual send happens.
google API中介绍requestCode中说明是当前未使用,一般都会赋值为0,但是当你发送多个通知,且每个通知都包含Extras时,这个就有用了。这个值可以用来标识不同通知中的Intent,主要是结合后面的flags来使用,比如,发送两个通知,id分别是1和2,当第二次发送1、2的通知时,需要更新前一次通知中的intent内容,如果不用requestCode来标识一下,则默认用最后一次发的通知覆盖前几次的通知intent。

正确是使用方法是:PendingIntent.getActivity(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);  requestCode来标识不同通知,flags中的PendingIntent.FLAG_UPDATE_CURRENT用来使用后面通知更新前面通知,使用这个flag要注意,intent一定不能为null,否则会报空指针错误。

另外,当我们把Activity 启动模式设置为 singleTask 之后 当我们下次 再去 用Intent 启动 这个 Activity 的时候 就不会去调用 onCreate方法 也不能在onRestart()方法中取,而是去调用onNewIntent()方法 然后把Intent中的数据传给它;

<activityandroid:name="TestActivity" android:launchMode="singleTask"/>      示例:

       @Override
  protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);

int hasMsgNotifyFlag = 0;
if(intent!=null){
Bundle bundle = intent.getExtras();
if(bundle!=null){
hasMsgNotifyFlag = bundle.getInt("id");
}
Log.i( "main tab msg=>", hasMsgNotifyFlag + "");
}
}

 

/** 
   * 添加一个notification 
   */  
  public void addNotification(Context context, int id, boolean flag){  
      NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);  
      Notification notification = new Notification(R.drawable.icon, "hello,here", System.currentTimeMillis());  
      notification.flags = Notification.FLAG_AUTO_CANCEL; 
      Intent intent = new Intent(context, TestActivity.class);  
      Bundle bundle = new Bundle();
      bundle.putInt("id", id);
      intent.putExtras(bundle);
      PendingIntent contentIntent = PendingIntent.getActivity(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);  
      notification.setLatestEventInfo(context, "有新消息", id + "", contentIntent);  
      nm.notify(id, notification);  
  } 




相关文章
|
Android开发
Android WebView选择图片、发送图片
Android WebView选择图片、发送图片
573 0
|
存储 JavaScript 安全
教你如何用一行命令:Android打包->上传->发测试包通知
教你如何用一行命令:Android打包->上传->发测试包通知
218 0
教你如何用一行命令:Android打包->上传->发测试包通知
|
Android开发
Android Socket通讯 分离服务端和客户端、发送表情消息
Android Socket通讯 分离服务端和客户端、发送表情消息
113 0
Android Socket通讯 分离服务端和客户端、发送表情消息
|
存储 消息中间件 API
下沉式通知的一种实现 | Android悬浮窗Window应用
当你浏览公众号时来了一条新消息,通知在屏幕顶部会以自顶向下动画的形式入场,而且它是跨界面的全局浮窗(效果如下图)。虽然上一篇中抽象的浮窗工具类已经能实现这个需求。但本文在此基础上再封装一些更加友好的
313 0
|
XML 安全 API
你真的懂android通知消息吗?
你真的懂android通知消息吗?
379 0
你真的懂android通知消息吗?
|
Android开发
Android发送验证码倒计时,时间倒计时
Android发送验证码倒计时,时间倒计时
122 0
Android发送验证码倒计时,时间倒计时
|
开发工具 Android开发
RxBus 一个简易、非反射的Android事件通知库
RxBus 一个简易、非反射的Android事件通知库
690 0
RxBus 一个简易、非反射的Android事件通知库
|
设计模式 安全 Java
Android事件通知工具:RxBus在Eclipse和AS中的实践
Android事件通知工具:RxBus在Eclipse和AS中的实践
209 0
Android事件通知工具:RxBus在Eclipse和AS中的实践
|
存储 消息中间件 Android开发
Android源码分析--广播的注册、发送和接收
Android源码分析--广播的注册、发送和接收
245 0
Android源码分析--广播的注册、发送和接收
|
Android开发
Android如何给通知channel静音
目前各个市场都要求targetsdkversion要不低于26,也就是android 8.0。 相应的影响很多功能,比如通知。 当targetsdkversion >= 26,需要为通知添加channel
241 0