Display Notification Text in Status Bar - Android

前端 未结 2 610
情深已故
情深已故 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());
    
    0 讨论(0)
  • 2021-02-09 05:46

    I need to add .setTicker() to the second code snippet to have text display in the Android device status bar

    0 讨论(0)
提交回复
热议问题