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

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


        
5条回答
  •  Happy的楠姐
    2021-02-05 21:59

    You should read the whole things not just a part, buddy. Please re-read carefully step-by-step.

    // this
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
    
    int icon = R.drawable.icon4;        
    CharSequence tickerText = "Hello"; // ticker-text
    long when = System.currentTimeMillis();         
    Context context = getApplicationContext();     
    CharSequence contentTitle = "Hello";  
    CharSequence contentText = "Hello";      
    Intent notificationIntent = new Intent(this, Example.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    Notification notification = new Notification(icon, tickerText, when);
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    
    // and this
    private static final int HELLO_ID = 1;
    mNotificationManager.notify(HELLO_ID, notification);
    

提交回复
热议问题