简体   繁体   中英

Pending Intent is not opening activity

I'm new to Android development. I'm able to see the ticker message "Did you reach to office", but when I click it, it's not opening my main activity.

Below is my code:

private class ViewUpdater implements Runnable{
  private Context contextFromView;
  public ViewUpdater(Context context) {
    this.contextFromView = context;
  }
  @Override
  public void run() {
    CancelNotification(contextFromView, notificationId);
    Random r=  new Random();
    notificationId =r.nextInt();
    Intent intent = new Intent();
    FragmentActivity activity = getActivity();
    PendingIntent pi = PendingIntent.getActivity(getActivity(),1000,intent,0);
    String body = "congratulations you made it today";
    String title = "Office reached";
    NotificationCompat.Builder mBuilder =
      new NotificationCompat.Builder(getActivity())
      .setSmallIcon(R.drawable.abc_ic_go)
      .setContentTitle(title)
      .setTicker("Did you reach office?")
      .setContentText(body);
    mBuilder.setContentIntent(pi);
    nm.notify(notificationId, mBuilder.build());
    timerHandler.postDelayed(this, 200000);
  }
}

You have to give an Intent to start the right Activity when creating your PendingIntent . Also the FLAG_ACTIVITY_NEW_TASK is necessary because you'll start the Activity outside the context of an existing Activity (source: Android doc ).

Intent = new Intent(getActivity(), YourActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getActivity(getActivity(),1000,intent,0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM