我无法找到一种方法来创建一个正在进行的"通知 - 我实际上不确定这种通知是否就是这种方式 - 如下图所示:
我如何通过我的通知(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);
答案 0 :(得分:0)
public void showNotification() {
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager nMN = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification n = new Notification.Builder(this)
.setContentTitle("Notification Title")
.setContentText("Notification content")
.setSmallIcon() //set your icon
.setVibrate(new long[] { 1000, 1000 })
.setLights(Color.BLUE, 700, 500)
.setSound(alarmSound)
.setOngoing(true)
//.setStyle(new NotificationCompat.BigTextStyle(NorProv))
.build();
nMN.notify(NOTIFICATION_ID, n);
}
<强>更新强>
将Notification.FLAG_ONGOING_EVENT
标记分配给您的通知,如下所示:
notif.flags = Notification.FLAG_ONGOING_EVENT;
更新#2
这用于设置通知的优先级:
.setPriority(Notification.PRIORITY_LOW);
//other priorities: MIN, HIGH, DEFAULT, MAX
要隐藏图标,只需尝试删除.setSmallIcon()
并仅使用.setLargeIcon()
答案 1 :(得分:0)
这就是我所做的。当您运行前台服务时,它会强制您进行持久/持续通知。如果你没有,那么你没有运行前台服务。诀窍是onStartCommand()重写和startsticky命令。这应该在您的服务范围内。使用onStartCommand而不是onCreate。并确保startForeground。
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Let it continue running until it is stopped.
/*
Do Work
*/
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_image)
.setContentText("Display Text")
.setPriority(Notification.PRIORITY_LOW).build();
startForeground(MyConstants.NOTIF_SERVICE, notification);
return START_STICKY;
}