codingWang / Issue

private space .Do NOT look at it.
3 stars 0 forks source link

doc-android-notification #1

Open codingWang opened 7 years ago

codingWang commented 7 years ago

Notification

Notification 概述

创建通知

NotificationCompat.Builder.build()

发出通知

NotificationManager.notify()

必须的通知内容

通知的操作

通知的优先级 NotificationCompat.Builder.setPriority()

创建简单的通知

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("ContentTitle")
        .setContentText("ContentText!");

Intent resultIntent = new Intent(this, ResultActivity.class);
// The stack builder object will contain an artificial back stack for the started Activity.
// This ensures that navigating backward from the Activity leads out of your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId 用于之后更新Notification
mNotificationManager.notify(mId, mBuilder.build());

将扩展布局应用于通知

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle("Event tracker")
    .setContentText("Events received")
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
String[] events = new String[6];
// Sets a title for the Inbox in expanded layout
inboxStyle.setBigContentTitle("Event tracker details:");
...
// Moves events into the expanded layout
for (int i=0; i < events.length; i++) {
    inboxStyle.addLine(events[i]);
}
// Moves the expanded layout object into the notification object.
mBuilder.setStyle(inBoxStyle);
...
// Issue the notification here.

更新通知

mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
mNotifyBuilder = new NotificationCompat.Builder(this)
    .setContentTitle("New Message")
    .setContentText("You've received new messages.")
    .setSmallIcon(R.drawable.ic_notify_status)
numMessages = 0;
// Start of a loop that processes data and then notifies the user
...
    mNotifyBuilder.setContentText(currentText).setNumber(++numMessages);
    // Because the ID remains unchanged, the existing notification is
    // updated.
    mNotificationManager.notify(notifyID,mNotifyBuilder.build());
...

删除通知的方式

锁屏通知

Notification.Style

Notification.Style的子类 (Since API 16)

详解各部分

1.BigPictureStyle
Notification notif = new Notification.Builder(mContext)
     .setContentTitle("New photo from " + sender.toString())
     .setContentText(subject)
     .setSmallIcon(R.drawable.new_post)
     .setLargeIcon(aBitmap)
     .setStyle(new Notification.BigPictureStyle().bigPicture(aBigBitmap))
     .build();
2.BigTextStyle
Notification notif = new Notification.Builder(mContext)
     .setContentTitle("New mail from " + sender.toString())
     .setContentText(subject)
     .setSmallIcon(R.drawable.new_mail)
     .setLargeIcon(aBitmap)
     .setStyle(new Notification.BigTextStyle().bigText(aVeryLongString))
     .build();
3.DecoratedCustomViewStyle

使用下面三个方法设置合适的自定义View

 setCustomContentView(RemoteViews)
 setCustomBigContentView(RemoteViews)
 setCustomHeadsUpContentView(RemoteViews)
Notification noti = new Notification.Builder()
     .setSmallIcon(R.drawable.ic_stat_player)
     .setLargeIcon(albumArtBitmap))
     .setCustomContentView(contentView);
     .setStyle(new Notification.DecoratedCustomViewStyle())
     .build();
4.InboxStyle

包含字符串列表的通知(最多5段字符串)

Notification notif = new Notification.Builder(mContext)
     .setContentTitle("5 New mails from " + sender.toString())
     .setContentText(subject)
     .setSmallIcon(R.drawable.new_mail)
     .setLargeIcon(aBitmap)
     .setStyle(new Notification.InboxStyle()
         .addLine(str1)
         .addLine(str2)
         .setContentTitle("")
         .setSummaryText("+3 more"))
     .build();
5.MediaStyle

使用MediaStyle创建的Notification有自己的分类,可以使用setCategory()方法设置分类

Notification noti = new Notification.Builder()
     .setSmallIcon(R.drawable.ic_stat_player)
     .setContentTitle("Track title")
     .setContentText("Artist - Album")
     .setLargeIcon(albumArtBitmap))
     .setStyle(new Notification.MediaStyle().setMediaSession(mySession))
     .build();
6.MessageStyle

包含多次往返消息类型的notification,只有大notification才有效果,

Notification noti = new Notification.Builder()
     .setContentTitle("2 new messages wtih " + sender.toString())
     .setContentText(subject)
     .setSmallIcon(R.drawable.new_message)
     .setLargeIcon(aBitmap)
     .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
         .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
         .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
     .build();

RemoteInput (Since API 20)