Java 如何在Android中向推送通知添加按钮
我希望我的推送通知有两个按钮,但我找不到任何地方如何做到这一点。有人能解释一下如何在Android中向推送通知添加按钮吗Java 如何在Android中向推送通知添加按钮,java,android,Java,Android,我希望我的推送通知有两个按钮,但我找不到任何地方如何做到这一点。有人能解释一下如何在Android中向推送通知添加按钮吗 NotificationCompat.Builder notification; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_verify); notif
NotificationCompat.Builder notification;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verify);
notification=new NotificationCompat.Builder(this);
notification.setAutoCancel(true);
/* blah blah blah */
}
public void onVerify(View v)
{
/*
some code
*/
if(example){ /* another piece of code */ }
else{
String num=data.getString("Phone");
this.defineNotification(num);
//Log.i(TAG,"here ");
Intent i=new Intent(this,VendorDetails.class);
i.putExtra("Phone",num);
startActivity(i);
}
}
public void defineNotification(String num)
{
notification.setContentTitle("Successful");
notification.setContentText("Hi, I am being displayed now");
notification.setWhen(System.currentTimeMillis());
notification.setSmallIcon(R.mipmap.ic_launcher);
Intent i=new Intent(this,OrderTypes.class);
PendingIntent pi=PendingIntent.getActivity(this, 0, i, 0);
notification.addAction(R.mipmap.ic_launcher,"Accept",pi);
notification.addAction(R.mipmap.ic_launcher, "Decline", pi);
NotificationCompat.MediaStyle mediaStyle=new NotificationCompat.MediaStyle();
MediaSessionCompat mediaSession=new MediaSessionCompat(this,TAG);
mediaStyle.setShowActionsInCompactView(1);
mediaStyle.setMediaSession(mediaSession.getSessionToken());
notification.setStyle(mediaStyle);
Intent intent=new Intent(this,VendorDetails.class);
intent.putExtra("Phone",num);
PendingIntent pendingIntent=PendingIntent.getActivity(Verify.this,0,intent,0);
notification.setContentIntent(pendingIntent);
NotificationManager nm=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(uniqueID,notification.build());
//Log.i(TAG, "coming here in notification");
}
我正在使用MediaSessionCompat而不是MediaSession类。请检查是否有错误 你想要这样的东西,对吗 对于通知,只需使用
.addAction(R.drawable.MYDRAWABLE,“BUTTON”,INTENT)
方法
Notification notification = new Notification.Builder(context)
// Show controls on lock screen even when user hides sensitive content.
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.NotIcon)
//HERE ARE YOUR BUTTONS
.addAction(R.drawable.ic_prev, "BUTTON 1", myIntentToButtonOneScreen) // #0
.addAction(R.drawable.ic_pause, "BUTTON 2", myIntentToButtonTwoScreen) // #1
.addAction(R.drawable.ic_next, "BUTTON 3", myIntentToButtonThreeScreen) // #2
// Apply the media style template
.setStyle(new Notification.MediaStyle()
.setShowActionsInCompactView(1)
.setMediaSession(mMediaSession.getSessionToken())
.setContentTitle("Example for you")
.setContentText("Example for you")
.setLargeIcon(ButtonExampleIcon)
.build();
看看这个:
请记住,要自定义通知,请使用
.build
方法。这就是我给你的密码如果有帮助,请告诉我:)Android接收推送通知。它不发送它们。此外,如果您不知道如何在Android中创建按钮和使用按钮,您基本上需要从初学者教程开始。学习android编程没有捷径可走。你需要先学习基础知识。首先,你需要正确理解这个问题,然后将其标记为不清楚或没有用处。我的问题是“如何将按钮添加到按钮通知”而不是活动。我非常了解如何将按钮添加到活动中。但我们在这里讨论的是推送通知,没错。我希望显示这样的按钮。我会试试看,然后告诉你是否有效。嗨,鲁希尔。非常感谢你的关心。我刚刚尝试实现它,但应用它时出现错误。我正在问题描述中粘贴错误和代码。谢谢Ruchir。现在很好用。刚刚删除了MediaSession代码,它正在工作。@SachinParashar太棒了!很高兴能帮上忙。:-)嘿Ruchir你能帮我回答这个问题吗