0

I can't find a way to create an "ongoing" notification - I actually am not sure this kind of notification is even called that way - like in the photo below :

Grey Notification Messenger

How could I achieve that with my notification (Pinger)? My notification is created for a foreground service so I don't no if what I'm asking is possible.

Thanks for your help.

Edit : The answers that you can find herre (Android: How to create an "Ongoing" notification?) did not solve my problem. This question is not a duplicate.

My code :

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());

As you can see, .setOngoing(true) doesn't work for me.

SOLUTION

You only have to set the priority of your notification to Notification.PRIORITY_MIN as in the code following :

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);
Community
  • 1
  • 1
  • please make sure to post some code along your question do not make it too broad, I recommend you to visit Stackoverflow help center to learn more about asking questions – Thorvald Feb 08 '17 at 16:21
  • Love how this question is marked duplicate even though the "duplicate" question is 6 years older and mobile development is an exceptional volatile field. – Prime624 Jan 16 '19 at 23:56

2 Answers2

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);
}

UPDATE

Assign Notification.FLAG_ONGOING_EVENT flag to your Notification like so :

notif.flags = Notification.FLAG_ONGOING_EVENT;

UPDATE #2

This is used to set priorities to your notification:

.setPriority(Notification.PRIORITY_LOW);
 //other priorities: MIN, HIGH, DEFAULT, MAX

To hide the icon just try to remove the .setSmallIcon() and only use a .setLargeIcon()

Thorvald
  • 3,424
  • 6
  • 40
  • 66
  • The .setOngoing(true) did not solve my problem, I'm still having the same notification – Jerome Dumilieu Feb 08 '17 at 17:36
  • does the notification dismiss if you swipe it ? – Thorvald Feb 08 '17 at 17:41
  • check the updated answer – Thorvald Feb 08 '17 at 17:44
  • No it does not, but my notification was not getting dismissed when I was swiping it. What I'm looking for is a way to make my notification as the Messenger Notification (in the picture). – Jerome Dumilieu Feb 08 '17 at 17:49
  • The updated answer doesn't work either. I'm still having the same notification. Maybe this is not possible ? – Jerome Dumilieu Feb 08 '17 at 17:57
  • an `OnGoing` notification is a type of notifications that you cannot dismiss, try to clarify your question – Thorvald Feb 08 '17 at 18:00
  • That's just it : I have no idea of how this type of notification is called. All I can say is that what I'm after is the type of notification of Messenger on this [photo](https://i.stack.imgur.com/tttMG.jpg) – Jerome Dumilieu Feb 08 '17 at 18:06
  • the only remarkable thing about that notification is it is you cant dismiss it and it automatically closes after you close the chat bubble, is that what you want ? – Thorvald Feb 08 '17 at 18:12
  • There are 2 remarkable things about that notification that I want : the fact that when the notification center is closed, we can't see the icon on the notification status bar, and the fact that all the other notifications are above this one, even if this notification is created after the other notifications, that are now above. – Jerome Dumilieu Feb 08 '17 at 18:15
  • check my last update – Thorvald Feb 08 '17 at 18:40
  • Using PRIORITY_MIN worked for me, thanks – Jerome Dumilieu Feb 08 '17 at 18:52
  • you are welcome, if that helped you make sure to select it as an answer and voted up to make it easy for the others to find it – Thorvald Feb 08 '17 at 18:53
0

Here is what I did. When you are running a foreground service it forces you to have a persistent/ongoing notification. If you dont, then your not running a foreground service. The trick is the onStartCommand() override and startsticky command. This should be within your service. use onStartCommand instead of onCreate. and be sure to 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;
}
John Bravado
  • 137
  • 4
  • 10
  • I'm sorry, I'm a beginner... Could you tell me what are MyConstants and receiver ? – Jerome Dumilieu Feb 08 '17 at 17:59
  • My service was holding a receiver on to battle against something that i ended up not needing. I revised the post for you. MyConstants is my singleton which holds my constants because notifications need a notification ID. you can replace `MyConstants.NOTIF_SERVICE' with any number like `001` or whatever. So long as you have start foreground and the linked notification for it, whatever you started in `onStartCommand` should remain active. I say should because i dont know what your service is doing. – John Bravado Feb 08 '17 at 18:12
  • I guess a better question would be what exactly do you want this notification to do. – John Bravado Feb 08 '17 at 18:14
  • I want this notification to be below all the others, even if it is created after, and I'd like to be able to hide it's icon on the notificaiton statusbar – Jerome Dumilieu Feb 08 '17 at 18:27
  • But this also has to be a service you are running in the foreground? – John Bravado Feb 08 '17 at 18:30
  • Yes... I know it doesn't make a lot of sense but since some apps are using this kind of notifications I wanted to give it a try – Jerome Dumilieu Feb 08 '17 at 18:32
  • updated script. Try setting notification priority level to LOW as shown in above code. That should put your notification at the bottom of the list at all times. As far as i am aware a 'foreground service', which is what I started in my example cannot dismiss the notification icon. You have to run tricky code to dismiss it. Now if you are not really running a foreground service that is something different. – John Bravado Feb 08 '17 at 18:37
  • Thanks for the LOW priority level trick. And I need this foreground service, sadly. But how would you create a notification as I described if you don't need it to be a foreground service ? Because I couldn't even find that – Jerome Dumilieu Feb 08 '17 at 18:43
  • THANK YOU ! To achieve such a notification you need to set the priority to PRIORITY.MIN . And then, foreground service or not, there is no icon in the statusbar and the notification is below every other notifications – Jerome Dumilieu Feb 08 '17 at 18:46
  • If you like my answer can you check it as the answer. i am trying to get to 50 rep to post comments instead of just answers. – John Bravado Feb 08 '17 at 18:51