我想根据订单状态更改显示通知状态。截至目前,我能够显示它。现在接下来的任务是,当用户点击通知时,我更愿意将其带到跟踪订单片段,用户可以在其中详细查看订单状态。
这是我的代码:
public static void createNotification(FragmentActivity activity, boolean isLoggedIn, String selectedOrderId, String order_status, String status_desc)
{
NotificationCompat.Builder builder =
new NotificationCompat.Builder(activity)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Lugmah Order Status")
.setContentText("The Order Status of Order Id: "+selectedOrderId+ "is: "+status_desc)
.setDefaults(NotificationCompat.DEFAULT_SOUND)
.setAutoCancel(true);
int NOTIFICATION_ID = 12345;
Intent targetIntent = new Intent(activity, TrackOrderFragment.class);
targetIntent.putExtra("isTrackOrder", false);
Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
TaskStackBuilder stackBuilder = TaskStackBuilder.create(activity);
if(isLoggedIn)
{
if(status_desc.equals("Rejected"))
{
targetIntent.putExtra("isLoggedIn", true);
targetIntent.putExtra("status_desc", status_desc);
fragment.setArguments(bundle);
bundle.putString("status_desc", status_desc);
bundle.putString("selectedOrderId", selectedOrderId);
targetIntent.putExtras(bundle);
stackBuilder.addParentStack(TrackOrderFragment.class);
stackBuilder.addNextIntent(targetIntent);
}
}
else
{
targetIntent.putExtra("isLoggedIn", false);
}
PendingIntent contentIntent = stackBuilder
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_ONE_SHOT);
//PendingIntent contentIntent = PendingIntent.getActivity(activity, 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
NotificationManager nManager = (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(NOTIFICATION_ID, builder.build());
}
我想在两个片段之间传递 Status_desc和SelectedOrderId 的数据。
要接收数据,这是我写的代码。
status_desc = getArguments().getString("status_desc");
rejectedOrderId = getArguments().getString("rejectedOrderId");
此处我在管理员更改订单状态时收到通知。但是,我无法在点击通知时导航到 TrackOrderFragment.class 。
帮助将不胜感激。
答案 0 :(得分:1)
您可以点击按钮
尝试以下代码 btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Fragment fragment = new tasks();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
答案 1 :(得分:0)
可能正在做
Intent targetIntent = new Intent(activity, TrackOrderActivity.class);
代替Intent targetIntent = new Intent(activity, TrackOrderFragment.class);
会对您有所帮助。您可以发送标志作为意图,以了解活动是否是根据通知创建的。用该活动附加片段。
答案 2 :(得分:0)
我主要谈论的是这个代码块。
NotificationCompat.Builder builder =
new NotificationCompat.Builder(activity)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Lugmah Order Status")
.setContentText("The Order Status of Order Id: "+selectedOrderId+ "is: "+status_desc)
.setDefaults(NotificationCompat.DEFAULT_SOUND)
.setAutoCancel(true);
int NOTIFICATION_ID = 12345;
Intent targetIntent = new Intent(activity, TrackOrderActivity.class);
targetIntent.putExtra("isTrackOrder", false);
Bundle bundle = new Bundle();
if(isLoggedIn)
{
if(status_desc.equals("Rejected"))
{
targetIntent.putExtra("isFromNotification", true);
bundle.putString("status_desc", status_desc);
bundle.putString("sejectedOrderId", selectedOrderId);
targetIntent.putExtra("orderBundle",bundle);
}
}
else
{
targetIntent.putExtra("isLoggedIn", false);
}
//PendingIntent
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
targetIntent, 0);
builder.setContentIntent(contentIntent);
NotificationManager nManager = (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(NOTIFICATION_ID, builder.build());
现在在TrackOrderActivity onCreate方法内部执行此操作。
setContentView(R.layout.activity_track_order);
if(getIntent().getBooleanExtra("isFromNotification",true)){
TrackOrderFragment fragment = new TrackOrderFragment();
fragment.setArguments(getIntent().getBundleExtra("orderBundle"));
getSupportFragmentManager().beginTransaction().
replace(R.id.container,fragment).commit();
}
如果您调用getArguments(),现在在您的TrackOrderFragment上,我认为您将获得您的捆绑数据。