notification 详解

创建notification步骤:

1.内容标题

    2.大图标

    3.内容

    4.内容附加信息

    5.小图标

    6.时间


1>实例化一个NotificationCompat.Builder对象;如builder

     2>调用builder的相关方法对notification进行上面提到的各种设置

    3>调用builder.build()方法此方法返回一个notification对象。

     4>实例化一个NotificationManager对象;如:manager

     5>调用manager的notify方法。

注:

   一个notification不必对上面所有的选项都进行设置,但有3项是必须的:

   小图标, set by setSmallIcon()

     内容标题, set by setContentTitle()

     内容, set by setContentText()


普通notification

Notification notification = new NotificationCompat.Builder(context)
                 .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
                 .setTicker("showNormal").setContentInfo("contentInfo")
                 .setContentTitle("ContentTitle").setContentText("ContentText")
                 .setNumber(++messageNum)
                 .setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
                 .build();
         manager.notify(NOTIFICATION_ID_1, notification);


大布局Text类型notification

  NotificationCompat.BigTextStyle textStyle = new BigTextStyle();
         textStyle
                 .setBigContentTitle("BigContentTitle")
                 .setSummaryText("SummaryText")
                 .bigText(
                         "I am Big Texttttttttttttttttttttttttttttttttttttttttttt!!!!!!!!!!!!!!!!!!!......");
         Notification notification = new NotificationCompat.Builder(context)
                 .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
                 .setTicker("showBigView_Text").setContentInfo("contentInfo")
                 .setContentTitle("ContentTitle").setContentText("ContentText")
                 .setStyle(textStyle)
                 .setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
                 .build();
         manager.notify(NOTIFICATION_ID_2, notification);


 大布局Picture类型notificatio

  NotificationCompat.BigPictureStyle pictureStyle = new BigPictureStyle();
         pictureStyle.setBigContentTitle("BigContentTitle")
                 .setSummaryText("SummaryText").bigPicture(icon);
         Notification notification = new NotificationCompat.Builder(context)
                 .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
                 .setTicker("showBigView_Pic").setContentInfo("contentInfo")
                 .setContentTitle("ContentTitle").setContentText("ContentText")
                 .setStyle(pictureStyle)
                 .setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
                 .build();
         manager.notify(NOTIFICATION_ID_3, notification);

大布局Inbox类型notification

NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
         inboxStyle.setBigContentTitle("BigContentTitle").setSummaryText(
                 "SummaryText");
         for (int i = 0; i < 5; i++)
             inboxStyle.addLine("news:" + i);
         Notification notification = new NotificationCompat.Builder(context)
                 .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
                 .setTicker("showBigView_Inbox").setContentInfo("contentInfo")
                 .setContentTitle("ContentTitle").setContentText("ContentText")
                 .setStyle(inboxStyle)
                 .setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
                 .build();
         manager.notify(NOTIFICATION_ID_4, notification);


自定义notification


RemoteViews remoteViews = new RemoteViews(getPackageName(),
                 R.layout.custom_notification);
         Intent intent = new Intent(this, TestMusicControl.class);
         PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
                 intent, 0);
         remoteViews.setOnClickPendingIntent(R.id.paly_pause_music,
                 pendingIntent);
         NotificationCompat.Builder builder = new Builder(context);
         builder.setContent(remoteViews).setSmallIcon(R.drawable.music_icon)
                 .setLargeIcon(icon).setOngoing(true)
                 .setTicker("music is playing");
         manager.notify(NOTIFICATION_ID_8, builder.build());

http://www.jb51.net/article/36567.htm





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值