Display Notification Text in Status Bar - Android

前端 未结 2 611
情深已故
情深已故 2021-02-09 05:17

In my application I need to display notification to the user. The following code snippet worked great by display the icon and content title in the Android device title bar.

2条回答
  •  被撕碎了的回忆
    2021-02-09 05:34

    Below code worked for me. Just a few corrections in the second snippet.

    Intent resultIntent = new Intent(this, TestService.class); 
    
        PendingIntent pi = PendingIntent.getActivity(this, 0, resultIntent, 0);
    
        NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
            .setContentIntent(pi) 
            .setAutoCancel(true) 
            .setContentTitle("title")
            .setSmallIcon(R.drawable.ic_notif) //add a 24x24 image to the drawable folder
                                               // and call it here 
            .setContentText("desc"); 
    
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.notify(0, builder.build());
    

提交回复
热议问题