Display Notification Text in Status Bar - Android

瘦欲@ 提交于 2019-12-03 15:03:18
Michael Kniskern

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

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());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!