Android notification .addAction deprecated in api 23

后端 未结 4 2117
隐瞒了意图╮
隐瞒了意图╮ 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:39

    You just have to use NotificationCompat.Builder builder in place of Notification.Builder builder because NotificationCompat.Builder allows you to build your application below Android Version 4.1

    Solved by using NotificationCompat.builder:

        String strTitle = getString(R.string.new_message);
        String strText = getString(R.string.hi_whats_up);
        Intent intent = new Intent(this, NotificationView.class);
        intent.putExtra("title", strTitle);
        intent.putExtra("text", strText);
    
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
               .setSmallIcon(R.mipmap.ic_launcher)
               .setTicker("Notification Ticker")
               .setContentTitle("Notification Title")
               .setContentText("Notification Text")
               .addAction(R.mipmap.ic_launcher, "Notification Action", pendingIntent)
               .setContentIntent(pendingIntent)
               .setAutoCancel(true);
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
               notificationManager.notify(0, builder.build());
    

提交回复
热议问题