【发布时间】:2020-07-15 16:46:03
【问题描述】:
以下代码是项目user-interface-samples中关于Notifications的示例代码。
Image A会在我启动BIG_PICTURE_STYLE通知时显示,Image B会在我点击地球图像和通知图标时显示顶部工具栏上的将关闭。
1:如果我点击Image A上的“否”按钮,顶部工具栏上的通知图标将关闭,此通知视图也将关闭。
我很奇怪 BigPictureSocialIntentService.java 中的函数private void handleActionComment(CharSequence comment) 重新创建了通知,为什么通知图标会消失?
2:我修改了BigPictureSocialIntentService.java,你可以看到BigPictureSocialIntentService_Modified.java,然后我点击Image A上的“否”按钮, 我发现通知图标消失了,但是通知视图一直显示,你可以看到Image C,为什么?
顺便说一句,我在 API 30 中对其进行了测试。
MainActivity.java
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
...
private void generateBigPictureStyleNotification() {
Log.d(TAG, "generateBigPictureStyleNotification()");
MockDatabase.BigPictureStyleSocialAppData bigPictureStyleSocialAppData =
MockDatabase.getBigPictureStyleData();
String notificationChannelId =
NotificationUtil.createNotificationChannel(this, bigPictureStyleSocialAppData);
BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle()
// Provides the bitmap for the BigPicture notification.
.bigPicture(
BitmapFactory.decodeResource(
getResources(),
bigPictureStyleSocialAppData.getBigImage()))
// Overrides ContentTitle in the big form of the template.
.setBigContentTitle(bigPictureStyleSocialAppData.getBigContentTitle())
// Summary line after the detail section in the big form of the template.
.setSummaryText(bigPictureStyleSocialAppData.getSummaryText());
Intent mainIntent = new Intent(this, BigPictureSocialMainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(BigPictureSocialMainActivity.class);
stackBuilder.addNextIntent(mainIntent);
PendingIntent mainPendingIntent =
PendingIntent.getActivity(
this,
0,
mainIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
String replyLabel = getString(R.string.reply_label);
RemoteInput remoteInput =
new RemoteInput.Builder(BigPictureSocialIntentService.EXTRA_COMMENT)
.setLabel(replyLabel)
// List of quick response choices for any wearables paired with the phone
.setChoices(bigPictureStyleSocialAppData.getPossiblePostResponses())
.build();
PendingIntent replyActionPendingIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Intent intent = new Intent(this, BigPictureSocialIntentService.class);
intent.setAction(BigPictureSocialIntentService.ACTION_COMMENT);
replyActionPendingIntent = PendingIntent.getService(this, 0, intent, 0);
} else {
replyActionPendingIntent = mainPendingIntent;
}
NotificationCompat.Action replyAction =
new NotificationCompat.Action.Builder(
R.drawable.ic_reply_white_18dp,
replyLabel,
replyActionPendingIntent)
.addRemoteInput(remoteInput)
.build();
NotificationCompat.Builder notificationCompatBuilder =
new NotificationCompat.Builder(getApplicationContext(), notificationChannelId);
GlobalNotificationBuilder.setNotificationCompatBuilderInstance(notificationCompatBuilder);
notificationCompatBuilder
.setStyle(bigPictureStyle)
.setContentTitle(bigPictureStyleSocialAppData.getContentTitle())
.setContentText(bigPictureStyleSocialAppData.getContentText())
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(
getResources(),
R.drawable.ic_person_black_48dp))
.setContentIntent(mainPendingIntent)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
.setSubText(Integer.toString(1))
.addAction(replyAction)
.setCategory(Notification.CATEGORY_SOCIAL)
.setPriority(bigPictureStyleSocialAppData.getPriority())
.setVisibility(bigPictureStyleSocialAppData.getChannelLockscreenVisibility());
for (String name : bigPictureStyleSocialAppData.getParticipants()) {
notificationCompatBuilder.addPerson(name);
}
Notification notification = notificationCompatBuilder.build();
mNotificationManagerCompat.notify(NOTIFICATION_ID, notification);
}
...
}
BigPictureSocialMainActivity.java
public class BigPictureSocialMainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_big_picture_main);
// Cancel Notification
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(MainActivity.NOTIFICATION_ID);
// TODO: Handle and display social post from your database
}
}
BigPictureSocialIntentService.java
public class BigPictureSocialIntentService extends IntentService {
private static final String TAG = "BigPictureService";
public static final String ACTION_COMMENT =
"com.example.android.wearable.wear.wearnotifications.handlers.action.COMMENT";
public static final String EXTRA_COMMENT =
"com.example.android.wearable.wear.wearnotifications.handlers.extra.COMMENT";
public BigPictureSocialIntentService() {
super("BigPictureSocialIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
final String action = intent.getAction();
if (ACTION_COMMENT.equals(action)) {
handleActionComment(getMessage(intent));
}
}
}
private void handleActionComment(CharSequence comment) {
Log.d(TAG, "handleActionComment(): " + comment);
if (comment != null) {
// Retrieves NotificationCompat.Builder used to create initial Notification
NotificationCompat.Builder notificationCompatBuilder =
GlobalNotificationBuilder.getNotificationCompatBuilderInstance();
// Recreate builder from persistent state if app process is killed
if (notificationCompatBuilder == null) {
// Note: New builder set globally in the method
notificationCompatBuilder = recreateBuilderWithBigPictureStyle();
}
// Updates active Notification
Notification updatedNotification = notificationCompatBuilder
// Adds a line and comment below content in Notification
.setRemoteInputHistory(new CharSequence[]{comment})
.build();
// Pushes out the updated Notification
NotificationManagerCompat notificationManagerCompat =
NotificationManagerCompat.from(getApplicationContext());
notificationManagerCompat.notify(MainActivity.NOTIFICATION_ID, updatedNotification);
}
}
private CharSequence getMessage(Intent intent) {
Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
if (remoteInput != null) {
return remoteInput.getCharSequence(EXTRA_COMMENT);
}
return null;
}
/*
* This recreates the notification from the persistent state in case the app process was killed.
* It is basically the same code for creating the Notification from MainActivity.
*/
private NotificationCompat.Builder recreateBuilderWithBigPictureStyle() {
...
}
}
BigPictureSocialIntentService_Modified.java
public class BigPictureSocialIntentService extends IntentService {
....
private void handleActionComment(CharSequence comment) {
Log.d(TAG, "handleActionComment(): " + comment);
if (comment != null) {
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(MainActivity.NOTIFICATION_ID);
}
}
}
图片 A
图片 B
图片 C
【问题讨论】:
标签: android