Java 如何将按钮添加到 Android 中的推送通知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34377899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add Buttons to a push notification in Android
提问by Sachin Parashar
I want my push notification to have two buttons but I couldn't find anywhere how to do so. Can anyone explain how to add buttons to a Push Notification in Android?
我希望我的推送通知有两个按钮,但我找不到任何方法。谁能解释一下如何在 Android 中向推送通知添加按钮?
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");
}
I am using MediaSessionCompat instead of MediaSession class. Please check if there is a mistake.
我正在使用 MediaSessionCompat 而不是 MediaSession 类。请检查是否有错误。
采纳答案by Ruchir Baronia
You want something like this, right?
你想要这样的东西,对吧?
For notifications, just use the .addAction(R.drawable.MYDRAWABLE, "BUTTON", INTENT)
method.
对于通知,只需使用该.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();
Take a look at this:
看看这个:
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
Just remember this, in order to customize your notifications, use the .build
method. That's what we do in the code I gave you. Let me know if this helped :)
请记住这一点,为了自定义您的通知,请使用该.build
方法。这就是我们在我给你的代码中所做的。让我知道这是否有帮助:)