Android notification .addAction deprecated in api 23

后端 未结 4 2101
隐瞒了意图╮
隐瞒了意图╮ 2020-12-01 12:45

What is the proper way to add an action to the notification in API 23 since addAction(int icon, CharSequence title, PendingIntent intent) is deprec

4条回答
  •  借酒劲吻你
    2020-12-01 13:23

    //Exemple of notification with Button
    private void scheduleNotificationWithButton(String message) {
    
        int notifReCode = 1;
    
        //What happen when you will click on button
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_ONE_SHOT);
    
        //Button
        NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.mipmap.ic_launcher, "Go", pendingIntent).build();
    
        //Notification
        Notification notification = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentText("Back to Application ?")
                .setContentTitle("Amazing news")
                .addAction(action) //add buton
                .build();
    
        //Send notification
        NotificationManager notificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, notification);
    }
    

提交回复
热议问题