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 同时发送几条通知的更多相关文章

    1. Android种使用Notification实现通知管理以及自定义通知栏(Notification示例四)

      示例一:实现通知栏管理 当针对相同类型的事件多次发出通知,作为开发者,应该避免使用全新的通知,这时就应该考虑更新之前通知栏的一些值来达到提醒用户的目的.例如我们手机的短信系统,当不断有新消息传来时,我 ...

    2. Android 自学之进度条ProgressBar

      进度条(ProgressBar)也是UI界面中的一种非常使用的组件,通常用于向用户显示某个耗时完成的百分比.因此进度条可以动态的显示进度,因此避免长时间地执行某个耗时操作时,让用户感觉程序失去了响应, ...

    3. Android之发送短信和接收验证码

        最近项目需求需要发送短信和接收验证码并将验证码显示在输入框中 以下是我的记录    前提---权限     <uses-permission android:name="andro ...

    4. 【转】24. android dialog ——ProgressDialog 进度条对话框详解

      原文网址:http://blog.csdn.net/jamesliulyc/article/details/6375598 首先在onCreateDialog方法里创建一个ProgressDialog ...

    5. Android开发的16条小经验总结

      Android开发的16条小经验总结,希望对各位搞Android开发的朋友有所帮助. 1. TextView中的getTextSize返回值是以像素(px)为单位的, 而setTextSize()是以 ...

    6. android:Notification实现状态栏的通知

      在使用手机时,当有未接来电或者新短消息时,手机会给出响应的提示信息,这些提示信息一般会显示到手机屏幕的状态栏上. Android也提供了用于处理这些信息的类,它们是Notification和Notif ...

    7. Android如何使用Notification进行通知

      有两张图片素材会放在末尾 activity代码,和XML布局 package com.example.myapplication; import androidx.appcompat.app.AppC ...

    8. PHP版微信公共平台消息主动推送,突破订阅号一天只能发送一条信息限制

      2013年10月06日最新整理. PHP版微信公共平台消息主动推送,突破订阅号一天只能发送一条信息限制 微信公共平台消息主动推送接口一直是腾讯的私用接口,相信很多朋友都非常想要用到这个功能. 通过学习 ...

    9. Android View 之进度条+拖动条+星级评论条....

      PS:将来的你会感谢现在奋斗的自己.... 学习内容: 1.进度条 2.拖动条 3.星级评论条 1.进度条...       进图条这东西想必大家是很熟悉的...为了使用户不会觉得应用程序死掉了,因此 ...

    随机推荐

    1. asp.net textbox keyup事件触发后台的textchange事件

      textbox文本框text_change事件,失去焦点才会执行. 通过keyup事件,js控制失去焦点. <asp:TextBox runat="server" ID=&q ...

    2. js 中的闭包

      /** *闭包就是在一个函数的外面访问函数内部的变量 **/ var name = "xiao A"; var obj = { name : "xiao B" ...

    3. Unity5UGUI 官方教程学习笔记(四)UI Image

      Image Source image:源图片  需要显示的图片 Color:颜色  会与图片进行颜色的混合 Material:材质 Image Type:  Simple   精灵只会延伸到适合Rec ...

    4. VirtualBox扩展磁盘空间

      进入VB的安装目录, 输入命令 VBoxManage list hdds获得当前所有虚拟机的uuid 选择需要扩展的磁盘, 输入 VBoxManage modifyhd uuid –resize 81 ...

    5. web server服务器

      使用最多的 web server服务器软件有两个:微软的信息服务器(iis),和Apache. 通俗的讲,Web服务器传送(serves)页面使浏览器可以浏览,然而应用程序服务器提供的是客户端应用程序 ...

    6. return和break的区别

      /* Name:return和break的区别 Copyright: By.不懂网络 Author: Yangbin Date:2014年2月25日 02:13:22 Description:以下代码 ...

    7. XSS CSRF

      XSS CSRF XSS 参考 https://zh.wikipedia.org/wiki/%E8%B7%A8%E7%B6%B2%E7%AB%99%E6%8C%87%E4%BB%A4%E7%A2%BC ...

    8. MySQL Select 优化

      准备: create table t(x int primary key,y int unique,z int); insert into t(x,y,z) values(1,1,1),(2,2,2) ...

    9. 7_Table Views

      7 // // ViewController.swift // Table Views // // Created by ZC on 16/1/9. // Copyright © 2016年 ZC. ...

    10. java selenium webdriver实战 应用小结

      部分api 1.访问网站 driver.get("http://www.baidu.com"); 或者 driver.navigate().to("http://www. ...