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

前端 未结 5 883
独厮守ぢ
独厮守ぢ 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:49

    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    NotificationManager nm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    Intent intent=new Intent(context,MainActivity.class);
    PendingIntent  pending=PendingIntent.getActivity(context, 0, intent, 0);
    Notification notification;
        if (Build.VERSION.SDK_INT < 11) {
            notification = new Notification(icon, "Title", when);
            notification.setLatestEventInfo(
                    context,
                    "Title",
                    "Text",
                    pending);
        } else {
            notification = new Notification.Builder(context)
                    .setContentTitle("Title")
                    .setContentText(
                            "Text").setSmallIcon(R.drawable.ic_launcher)
                    .setContentIntent(pending).setWhen(when).setAutoCancel(true)
                    .build();
        }
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.defaults |= Notification.DEFAULT_SOUND;
    nm.notify(0, notification);
    

    Or you can Download direct tutorial from here : http://www.demoadda.com/demo/android/how-to-create-local-notification-notification-manager-demo-with-example-android-source-code_26

提交回复
热议问题