`

android Notification通知消息学习(NotificationManager)

 
阅读更多
Notification
  Notification,俗称通知,是一种具有全局效果的通知,它展示在屏幕的顶端,首先会表现为一个图标的形式,当用户向下滑动的时候,展示出通知具体的内容。

  注意:因为一些Android版本的兼容性问题,对于Notification而言,Android3.0是一个分水岭,在其之前构建Notification推荐使用Notification.Builder构建,而在Android3.0之后,一般推荐使用NotificationCompat.Builder构建。本文的所有代码环境均在4.3中完成,如果使用4.1一下的设备测试,请注意兼容性问题。

  通知一般通过NotificationManager服务来发送一个Notification对象来完成NotificationManager是一个重要的系统级服务,该对象位于应用程序的框架层中,应用程序可以通过它像系统发送全局的通知。这个时候需要创建一个Notification对象,用于承载通知的内容。但是一般在实际使用过程中,一般不会直接构建Notification对象,而是使用它的一个内部类NotificationCompat.Builder来实例化一个对象(Android3.0之下使用Notification.Builder),并设置通知的各种属性,最后通过NotificationCompat.Builder.build()方法得到一个Notification对象。当获得这个对象之后,可以使用NotificationManager.notify()方法发送通知

  NotificationManager类是一个通知管理器类,这个对象是由系统维护的服务,是以单例模式获得,所以一般并不直接实例化这个对象。在Activity中,可以使用Activity.getSystemService(String)方法获取NotificationManager对象,Activity.getSystemService(String)方法可以通过Android系统级服务的句柄,返回对应的对象。在这里需要返回NotificationManager,所以直接传递Context.NOTIFICATION_SERVICE即可。

  虽然通知中提供了各种属性的设置,但是一个通知对象,有几个属性是必须要设置的,其他的属性均是可选的,必须设置的属性如下:

小图标:使用setSamllIcon()方法设置。--不下拉时显示的图标
大图标:使用setLargeIcon()方法设置。--下拉时显示的图标
标题:使用setContentTitle()方法设置。--下拉时显示的标题
文本内容:使用setContentText()方法设置。 --下拉时显示的内容
不下拉时的消息:使用setTicker()方法设置。--不下拉时显示的消息


更新与移除通知
  在使用NotificationManager.notify()发送通知的时候,需要传递一个标识符,用于唯一标识这个通知。对于有些场景,并不是无限的添加新的通知,有时候需要更新原有通知的信息,这个时候可以重写构建Notification,而使用与之前通知相同标识符来发送通知这个时候旧的通知就被被新的通知所取代,起到更新通知的效果。

  对于一个通知,当展示在状态栏之后,但是使用过后,如何取消呢?Android为我们提供两种方式移除通知,一种是Notification自己维护,使用setAutoCancel()方法设置是否维护,传递一个boolean类型的数据。另外一种方式使用NotificationManager通知管理器对象来维护,它通过notify()发送通知的时候,指定的通知标识Id来操作通知,可以使用cancel(int)来移除一个指定的通知,也可以使用cancelAll()移除所有的通知。

使用NotificationManager移除指定通知示例:
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(0);


案例
Bitmap btm = BitmapFactory.decodeResource(getResources(),
					R.drawable.arrow);
			NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
					NotificationActivity.this).setSmallIcon(R.drawable.arrow)
					.setContentTitle("5 new message")
					.setContentText("twain@android.com");
			// 第一次提示消息的时候显示在通知栏上
			mBuilder.setTicker("New Message");
                        // 下拉时内容右边显示的数字
			mBuilder.setNumber(12);
			// 设置图片
			mBuilder.setLargeIcon(btm);
			// 自己维护通知的消失
			mBuilder.setAutoCancel(true);
			// 构建一个Intent
			Intent resultIntent = new Intent(NotificationActivity.this,
					ListViewActivity.class);
			resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
					| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

			// 封装一个PendingIntent
			PendingIntent resultPendingIntent = PendingIntent.getActivity(
					NotificationActivity.this, 0, resultIntent,
					PendingIntent.FLAG_UPDATE_CURRENT);
			// 设置通知主题的意图
			mBuilder.setContentIntent(resultPendingIntent);
			// 获取通知管理器对象
			manager.notify(0, mBuilder.build());


跳转到的ListViewActivity配置
<activity
            android:name=".activity.ListViewActivity"
            android:label="@string/app_name"
        	android:taskAffinity=""
       	    android:excludeFromRecents="true">
            <intent-filter>
                <!-- <action android:name="android.intent.action.MAIN" /> -->
                <!-- <category android:name="android.intent.category.LAUNCHER" /> -->
            </intent-filter>
        </activity>










  • 大小: 3.3 KB
分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics