How to create notifications that don't go away when clicked in Android?

前端 未结 5 881
独厮守ぢ
独厮守ぢ 2021-02-05 21:18
int icon = R.drawable.icon4;        
CharSequence tickerText = \"Hello\"; // ticker-text
long when = System.currentTimeMillis();         
Context context = getApplicatio         


        
5条回答
  •  时光说笑
    2021-02-05 21:54

    Here is code

     Intent intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                    PendingIntent.FLAG_ONE_SHOT);
    
            Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("Push Notification")
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);
    
            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
            notificationManager.notify(0, notificationBuilder.build());
    

    Here if you want notifications that don't go away when clicked in Android? so set

    setAutoCancel(false);
    

提交回复
热议问题