通知Notification

简单Notification

  • 创建Builder

    1
    2
    3
    4
    5
    NotificationCompat.Builder mBuilder =
    new NotificationCompat.Builder(this)
    .setSmallIcon(R.mipmap.ic_launcher)
    .setContentTitle("My notification")
    .setContentText("Hello World!");
  • 添加Active

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    // 2. 创建点击回调动作
    Intent resultIntent = new Intent(this, ResultActivity.class);
    // Because clicking the notification opens a new ("special") activity, there's
    // no need to create an artificial back stack.
    PendingIntent resultPendingIntent =
    PendingIntent.getActivity(
    this,
    0,
    resultIntent,
    PendingIntent.FLAG_UPDATE_CURRENT
    );
    mBuilder.setContentIntent(resultPendingIntent);
  • 通知

    1
    2
    // Builds the notification and issues it.
    mNotifyMgr.notify(101, mBuilder.build());
  • 后台调用

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    // Creates an Intent for the Activity
    Intent resultIntent = new Intent(this, ResultActivity.class);
    // Sets the Activity to start in a new, empty task
    resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
    Intent.FLAG_ACTIVITY_CLEAR_TASK);
    // Creates the PendingIntent
    notifyIntent = PendingIntent.getActivity(
    this,
    0,
    resultIntent,
    PendingIntent.FLAG_UPDATE_CURRENT
    );

移除Notification

  1. 调用后自动清除
    1
    builder.setAutoCancel(true);
  2. 指定的 notification ID调用了cancel()
  3. 指定的 notification ID调用了cancelAll()

接下来的几个样式就不说了,简单,先贴图再看代码

BigView之Text

  • 调用bigTextNotification

BigView之Picture

  • 调用bigPictureNotification

BigView之Inbox

  • 调用bigInboxNotification

BigView之Custom

  • 调用customNotification
    • 大图
    • 小图

Code

代码部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package com.xuie.notificationdemo;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Binder;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews;

public class MyService extends Service {
PendingIntent notifyIntent;
NotificationManager mNotificationManager;

@Override
public void onCreate() {
super.onCreate();
// Creates an Intent for the Activity
Intent resultIntent = new Intent(this, ResultActivity.class);
// Sets the Activity to start in a new, empty task
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
// Creates the PendingIntent
notifyIntent = PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);

// Notifications are issued by sending them to the
// NotificationManager system service.
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Builds an anonymous Notification object from the builder, and
// passes it to the NotificationManager


IntentFilter filter = new IntentFilter();
filter.addAction(REMOTE_PLAY);
filter.addAction(REMOTE_NEXT);
filter.addAction(REMOTE_PREVIOUS);
registerReceiver(receiver, filter);
}

public void startNotification() {
// Instantiate a Builder object.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

// Puts the PendingIntent into the notification builder
builder.setContentIntent(notifyIntent);

builder.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Service Notification")
.setContentText("Hello World!");

// 设置自动清除Notification。其它方法:指定的 notification ID调用了cancel(),或cancelAll()
builder.setAutoCancel(true);

mNotificationManager.notify(101, builder.build());
}

public void bigTextNotification() {
NotificationCompat.BigTextStyle textStyle = new NotificationCompat.BigTextStyle()
.setBigContentTitle("BigTextContentTitle")
.setSummaryText("SummaryText")
.bigText("I am Big Text!");

Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("show big view text") // 第一次提示消息的时候显示在通知栏上
.setContentInfo("content info")
.setContentTitle("ContentTitle").setContentText("ContentText")
.setStyle(textStyle)
.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
.setContentIntent(notifyIntent)
.setAutoCancel(true)
.build();
mNotificationManager.notify(101, notification);
}

public void bigPictureNotification() {
NotificationCompat.BigPictureStyle pictureStyle = new NotificationCompat.BigPictureStyle()
.setBigContentTitle("BigPictureContentTitle")
.setSummaryText("SummaryText")
.bigPicture(resource2Bitmap(this, R.mipmap.ic_big_view));

Notification notification = new NotificationCompat.Builder(this)
.setLargeIcon(resource2Bitmap(this, R.mipmap.ic_big_view))// 这个与setSmallIcon相冲? - 测试手机Nexus5
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("show big view picture")
.setContentInfo("content info")
.setContentTitle("ContentTitle")
.setContentText("ContentText")
.setStyle(pictureStyle)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.build();
mNotificationManager.notify(101, notification);
}

public void bigInboxNotification() {
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle("BigInboxContentTitle").setSummaryText("SummaryText");
for (int i = 0; i < 5; i++) {
inboxStyle.addLine("news" + i);
}

Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("show big inbox picture")
.setContentInfo("content info")
.setContentTitle("ContentTitle")
.setContentText("ContentText")
.setStyle(inboxStyle)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.build();
mNotificationManager.notify(101, notification);
}

public void customNotification() {
PendingIntent playIntent = PendingIntent.getBroadcast(this, 0, new Intent(REMOTE_PLAY), PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent nextIntent = PendingIntent.getBroadcast(this, 0, new Intent(REMOTE_NEXT), PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent previousIntent = PendingIntent.getBroadcast(this, 0, new Intent(REMOTE_PREVIOUS), PendingIntent.FLAG_UPDATE_CURRENT);

RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.custom_notification);
remoteViews.setOnClickPendingIntent(R.id.play_pause, playIntent);
remoteViews.setImageViewResource(R.id.play_pause, R.mipmap.pause);
remoteViews.setOnClickPendingIntent(R.id.next, nextIntent);
remoteViews.setOnClickPendingIntent(R.id.previous, previousIntent);

RemoteViews remoteViews_expand = new RemoteViews(getPackageName(), R.layout.custom_expanded_notification);
remoteViews_expand.setOnClickPendingIntent(R.id.play_pause, playIntent);
remoteViews_expand.setOnClickPendingIntent(R.id.next, nextIntent);
remoteViews_expand.setOnClickPendingIntent(R.id.previous, previousIntent);
remoteViews_expand.setTextViewText(R.id.new_text, "New Add Layout");

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
// .setContentIntent(notifyIntent)
// .setOngoing(true)
.setAutoCancel(true)
.setTicker("music is playing");

Notification notification = builder.build();
notification.contentView = remoteViews;
notification.bigContentView = remoteViews_expand;

mNotificationManager.notify(102, notification);
}


public static final String REMOTE_PLAY = "remote play";
public static final String REMOTE_PREVIOUS = "remote previous";
public static final String REMOTE_NEXT = "remote next";

private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(REMOTE_PLAY)) {
System.out.println("REMOTE_PLAY");
} else if (action.equals(REMOTE_NEXT)) {
System.out.println("REMOTE_NEXT");
} else if (action.equals(REMOTE_PREVIOUS)) {
System.out.println("REMOTE_PREVIOUS");
}
}
};

@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}

class MyBinder extends Binder {
MyService getService() {
return MyService.this;
}
}

@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}


public static Bitmap resource2Bitmap(Context context, int drawId) {
return BitmapFactory.decodeResource(context.getResources(), drawId);
}

}
  • custom_notification.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <ImageView
    android:id="@+id/singer_pic"
    android:layout_width="64dp"
    android:layout_height="64dp"
    android:src="@mipmap/ic_big_view"/>

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <ImageView
    android:id="@+id/previous"
    android:layout_width="0dp"
    android:layout_height="48dp"
    android:layout_weight="1"
    android:src="@mipmap/previous"/>

    <ImageView
    android:id="@+id/play_pause"
    android:layout_width="0dp"
    android:layout_height="48dp"
    android:layout_weight="1"
    android:src="@mipmap/pause"/>

    <ImageView
    android:id="@+id/next"
    android:layout_width="0dp"
    android:layout_height="48dp"
    android:layout_weight="1"
    android:src="@mipmap/next"/>
    </LinearLayout>
    </LinearLayout>
  • custom_expanded_notification.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#323232"
android:orientation="horizontal">

<ImageView
android:id="@+id/singer_pic"
android:layout_width="144dp"
android:layout_height="144dp"
android:src="@mipmap/ic_big_view"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">

<TextView
android:id="@+id/new_text"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:padding="8dp"
android:text="Add Text Or Layout"
android:textSize="18sp"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal">

<ImageView
android:id="@+id/previous"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:src="@mipmap/previous"/>

<ImageView
android:id="@+id/play_pause"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:src="@mipmap/pause"/>

<ImageView
android:id="@+id/next"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:src="@mipmap/next"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
文章作者: 二十I邊界
文章链接: https://xuie0000.com/post/2016-01-29-2019/通知Notification.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 二十I邊界