在API 23
中,由于addAction(int icon, CharSequence title, PendingIntent intent)
已被弃用,因此添加通知动作的正确方法是什么?我找不到任何示例,请谢谢。
我的旧动作:.addAction(R.drawable.ic_prev, "Previous", prevPendingIntent)
在API 23
中,由于addAction(int icon, CharSequence title, PendingIntent intent)
已被弃用,因此添加通知动作的正确方法是什么?我找不到任何示例,请谢谢。
我的旧动作:.addAction(R.drawable.ic_prev, "Previous", prevPendingIntent)
使用以下方法替换原有的方法:
而不是这个:
addAction (int icon, CharSequence title, PendingIntent intent)
此方法已在API级别23中弃用。
所有内容均在开发人员文档中!
因此,要使用此功能:
首先使用NotificationCompat.Action.Builder构建您的操作。
NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.ic_prev, "Previous", prevPendingIntent).build();
注意:使用NotificationCompat.Action。
然后将其添加到您的通知中:
yournotification.addAction(action);
Notification.Action action
。 - Marian Pavelnew Notification.Action.Builder()
改为 new Action()
,结果:new Notification.Action(.....)
仍然已被弃用或者反之亦然。 - Marian PavelNotification.Action
似乎并没有被弃用。 - ben_joseph如果API级别大于等于23(棉花糖),请在Drawable的第一个参数中使用Icon
类。
https://developer.android.com/reference/android/app/Notification.Action.Builder.html
https://developer.android.com/sdk/api_diff/23/changes/android.app.Notification.Action.Builder.html
例子
Notification.Action action = new Notification.Action.Builder(
Icon.createWithResource(this, R.drawable.ic_prev),
"action string",
pendingIntent).build();
//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);
}
您只需使用NotificationCompat.Builder构建器代替Notification.Builder构建器,因为NotificationCompat.Builder允许您在Android 4.1以下版本中构建应用程序
通过使用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());
Actually, the only option to use all non-deprecated methods is by Using Icon for Action.Builder.
Notification.Action action = new Notification.Action.Builder(Icon.createWithResource(this, R.drawable.act_hike),
getString(R.string.finish_recording), pActionFinish).build();
//Create a notification
Notification notification = new Notification.Builder(this, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.atlas)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.foreground_notification))
.addAction(action)
.setContentIntent(pIntent)
.build();
使用特殊的构建器Notification.Action.Builder。
Notification.Action.Builder builder = new Notification.Action.Builder( Icon.createWithResource( this, R.drawable.ic_pause), getString( R.string.pause ), PendingIntent.getBroadcast( this, 1, new Intent( TIMER_PAUSE ), 0 ) );
Notification.Action action = builder.build();
...
notification_builder.addAction( action ); // or setActions( action );
addAction(Notification.Action action)
。 - TiggerNotification.Action action
。 - Marian Pavel