Java onClick通知到Android
我正在处理通知部分。我能够在单击通知消息时将用户导航到活动。我的实际任务是将用户导航到片段 下面是工作正常的通知代码Java onClick通知到Android,java,android,android-fragments,Java,Android,Android Fragments,我正在处理通知部分。我能够在单击通知消息时将用户导航到活动。我的实际任务是将用户导航到片段 下面是工作正常的通知代码 NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext()) .setSmallIcon(R.mipmap.ic_launcher) .setConten
NotificationCompat.Builder builder =
new NotificationCompat.Builder(getApplicationContext())
.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(getApplicationContext(), MainActivity.class);
targetIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
targetIntent.putExtra("isTrackOrder", false);
targetIntent.putExtra("orderNotification", "orderNotification");
targetIntent.putExtra("isLoggedIn", true);
targetIntent.putExtra("status_desc", status_desc);
targetIntent.putExtra("selectedOrderId", selectedOrderId);
PendingIntent contentIntent = PendingIntent
.getActivity(getApplicationContext(), 0, targetIntent, PendingIntent.FLAG_ONE_SHOT);
builder.setContentIntent(contentIntent);
NotificationManager nManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(NOTIFICATION_ID, builder.build());
我在intent中添加了MainActivity.java,因为这是我加载所有片段的地方。在这次活动中,我做了这样的事情
以下是onCreate()中的代码:
我们将不胜感激 您的通知代码运行良好,您需要在创建时对代码进行一些调整
if(getIntent().getExtras() != null) {
String orderNotification = getIntent().getStringExtra("orderNotification");
if (orderNotification.equals("orderNotification"))
{
TrackOrderFragment trackOrderFragment = new TrackOrderFragment();
fragmentTransaction.replace(android.R.id.content, trackOrderFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
您需要检查每次运行“活动”onCreate时是否收到任何额外信息,因为可能会发生空指针异常。您的通知代码工作正常。您需要在创建时对代码进行一些调整
if(getIntent().getExtras() != null) {
String orderNotification = getIntent().getStringExtra("orderNotification");
if (orderNotification.equals("orderNotification"))
{
TrackOrderFragment trackOrderFragment = new TrackOrderFragment();
fragmentTransaction.replace(android.R.id.content, trackOrderFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
您需要在每次运行活动onCreate时检查是否收到任何额外信息,因为可能会发生空指针异常。检查
如果(!TextUtils.isEmpty(orderNotification))
而不是如果(orderNotification!=null)
您还需要覆盖onNewIntent
@Michal\u 196 onNewIntent。。我应该把它放在哪里?放在你的活动课上。这是一种覆盖方法。移动其中的所有代码,而不是onCreate()method@Piyush让我试试这个检查if(!TextUtils.isEmpty(orderNotification))
而不是if(orderNotification!=null)
您还需要覆盖onNewIntent
@Michal\u 196 onNewIntent。。我应该把它放在哪里?放在你的活动课上。这是一种覆盖方法。移动其中的所有代码,而不是onCreate()method@Piyush让我试试这个如果你觉得有帮助,你能把答案标为正确的吗。谢谢。如果你觉得有帮助,你能把答案标为正确答案吗。谢谢