【发布时间】:2017-02-09 02:51:03
【问题描述】:
我找不到创建“持续”通知的方法 - 实际上我不确定这种通知是否被称为这种方式 - 如下图所示:
我如何通过我的通知 (Pinger) 实现这一目标?我的通知是为前台服务创建的,所以如果我的要求是可能的,我不会拒绝。
感谢您的帮助。
编辑:您可以在这里找到的答案 (Android: How to create an "Ongoing" notification?) 并没有解决我的问题。这个问题不是重复的。
我的代码:
Intent showTaskIntent = new Intent(getApplicationContext(), BackingService.class);
showTaskIntent.setAction(Intent.ACTION_MAIN);
showTaskIntent.addCategory(Intent.CATEGORY_LAUNCHER);
showTaskIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent detailsIntent = new Intent(BackingService.this, DetailsActivity.class);
detailsIntent.putExtra("EXTRA_DETAILS_ID", 42);
PendingIntent contentIntent = PendingIntent.getActivity(
getApplicationContext(),
0,
showTaskIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent replyPendingIntent = null;
if (Build.VERSION.SDK_INT < 24) {
replyPendingIntent = contentIntent;
} else {
replyPendingIntent = PendingIntent.getBroadcast(
BackingService.this,
0,
new Intent(BackingService.this, FromNotifSender.class),
PendingIntent.FLAG_UPDATE_CURRENT
);
}
RemoteInput remoteInput = new RemoteInput.Builder(KEY_NOTIFICATION_REPLY)
.setLabel("Tapez votre message ici")
.build();
android.support.v4.app.NotificationCompat.Action replyAction = new android.support.v4.app.NotificationCompat.Action.Builder(
0, "Envoyer à "+usernamefriend, replyPendingIntent)
.addRemoteInput(remoteInput)
.build();
android.support.v4.app.NotificationCompat.Builder miBuilder = new NotificationCompat.Builder(BackingService.this)
.setSmallIcon(R.drawable.logo4)
.setContentTitle(getString(R.string.app_name))
.setContentText(contentText)
.setWhen(System.currentTimeMillis())
.setContentIntent(contentIntent)
.setAutoCancel(false)
.setOngoing(true)
.setColor(getResources().getColor(R.color.colorPrimaryDark));
if (Build.VERSION.SDK_INT < 24) {
} else {
if(usernamefriend.equalsIgnoreCase("un ami")){
}else{
miBuilder.addAction(replyAction);
}
}
Intent resultIntent = new Intent(BackingService.this, SearchActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(BackingService.this);
stackBuilder.addParentStack(SearchActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
miBuilder.setContentIntent(resultPendingIntent);
startForeground(002, miBuilder.build());
如您所见,.setOngoing(true) 对我不起作用。
解决方案
您只需将通知的优先级设置为 Notification.PRIORITY_MIN,如下面的代码所示:
android.support.v4.app.NotificationCompat.Builder miBuilder = new NotificationCompat.Builder(BackingService.this)
.setSmallIcon(R.drawable.logo4)
.setContentTitle(getString(R.string.app_name))
.setContentText(contentText)
.setWhen(System.currentTimeMillis())
.setContentIntent(contentIntent)
.setAutoCancel(false)
.setOngoing(true)
.setColor(getResources().getColor(R.color.colorPrimaryDark))
.setPriority(Notification.PRIORITY_MIN);
【问题讨论】:
-
请确保在您的问题中发布一些代码,不要过于宽泛,我建议您访问 Stackoverflow 帮助中心以了解有关提问的更多信息
-
喜欢这个问题是如何被标记为重复的,即使这个“重复”问题已经 6 岁了,而且移动开发是一个非常不稳定的领域。
标签: java android notifications