Android通知Notification

       使用Android手机的人,都遇到过这样子的情况,有些应用会推送消息,会在手机屏幕的上方弹一个消息出来,点击会跳转到一个页面中,让用户查看消息,这个在Android中称为通知(Notification)。自己要做一个类似的通知需要一下几个步骤:

       1、获取通知管理类

mNotificationManager = (NotificationManager) getSystemService (Context.NOTIFICATION_SERVICE);
       2、构建通知对象

Intent intent = new Intent (this,NotifacationActivity.class);
PendingIntent pi = PendingIntent.getActivity (this, 0, intent, 0);
Notification notification = new NotificationCompat.Builder (this)
        .setContentTitle ("This is content title")//设置标题
        .setContentInfo ("This is content text")//设置内容信息
        .setWhen (System.currentTimeMillis ())//设置通知时间
        .setLargeIcon (BitmapFactory.decodeResource (getResources (), R.mipmap.ic_launcher))//设置通知大图标
        .setSmallIcon (R.mipmap.ic_launcher)//设置通知小图标
        .setContentIntent (pi)//点击通知后的操作
        .setAutoCancel (true)//设置当点击通知后,通知不会继续在系统栏中
        .setSound (Uri.fromFile (new File ("/system/media/audio/ringtones/MI.ogg")))//设置通知来时的声音
        .setVibrate (new long[]{0,1000,1000,1000})//设置通知来时振动,要权限
        .setLights (Color.YELLOW,1000,1000)//设置通知来时呼吸灯闪烁
        .setPriority (NotificationCompat.PRIORITY_MAX)//设置通知的重要程度
        .setStyle (new NotificationCompat.BigPictureStyle().bigPicture (BitmapFactory.decodeResource (getResources (),R.mipmap.ic_launcher)))//在内容中添加图片
        .build ();//构建对象
        以下对构建中使用的方法即参数做个详细的介绍

        (1)创建一个NotificationCompat.Builder的对象。

                new NotificationCompat.Builder(this)

        (2)设置通知的标题。

                setContentTitle(CharSequence title);

        (3)设置通知的内容信息。

                setContentInfo(CharSequence info);

        (4)设置通知的时间,设置当时发送:System.currentTimeMillis()。

                setWhen(long when);

        (5)设置通知的大图标,下拉系统栏时,可以看到。

                setLargeIcon(Bitmap icon);

        (6)设置通知的小图标,当通知来时会出现在状态栏上,参数是图片的ID。

               setSmallIcom(int icon);

        (7)设置通知意图,点击通知后,做哪些操作,参数是PendingIntent,这里点击通知后可以是启动一个页面,可以发送一条广播,可以启动一个服务。

               setContentIntent(PendingIntent intent);

        (8)设置通知点击后,是否在下拉系统栏中显示了。

               setAutoCancel(boolean autoCancel);

        (9)设置当通知来时播放的声音,参数是Uri,可以调用Uri.fromFile(File file)来获取声音。

               setSound(Uri sound);

        (10)设置当通知来时手机要震动,手机震动需要申请权限,参数是个数组,第一个表示手机静止时的时长,第二标识手机震动的时长,单位是ms,后边以此类推。

               <uses-permission android:name="android.permission.VIBRATE"/>

               setVibrate(long[] pattern);

        (11)设置当通知到来时,呼吸灯闪烁,参数1呼吸灯的颜色ID,参数2呼吸灯亮的时长,参数3呼吸灯灭的时长。

               setLights(int color,int onMs,int offMs);

        (12)设置通知的优先顺序。

               setPriority(int priority)

               参数可取的值即含义

               PRIORITY_DEFAULT 默认的优先级

               PRIORITY_MIN  最低的优先级

               PRIORITY_LOW 较低的优先级

               PRIORITY_HIGH 较高的优先级

               PRIORITY_MAX 最高的优先级

        (13)设置长文字,设置大图片。

              setStyle(NotificationCompat.Style style);

              NotificationCompat.BigTextStyle().bigText(CharSequence text);

              NotificationCompat.BigPictureStyle().bigPicture(Bitmap b);

        (14)构建Notification对象。

              build();

      3、使用通知管理类发送通知

mNotificationManager.notify (1,notification);
           参数1:通知的ID

           参数2:通知对象。 

发布了78 篇原创文章 · 获赞 7 · 访问量 9万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章